I dont show the image in Tkinter

2024/10/13 22:27:07

The image doesn't show in Tkinter. The same code work in a new window, but in my class it does not. What could be the problem ?

import Tkinterroot = Tkinter.Tkclass InterfaceApp(root):def __init__(self,parent):root.__init__(self,parent)self.parent = parentself.initialize()def initialize(self):frPic = Tkinter.Frame(bg='', colormap='new')im = Tkinter.PhotoImage(file="tr.gif")imLabel = Tkinter.Label(frPic, image=im)frPic.grid(row = 4, columnspan = 10, sticky='EW')imLabel.grid(row=3,column=30)if __name__ == '__main__':app = InterfaceApp(None)app.title("P")app.mainloop()
Answer

You must keep a reference to tr.gif . This means you need to add this line:

imLabel.image = im

After these 2 lines:

im = Tkinter.PhotoImage(file="tr.gif")
imLabel = Tkinter.Label(frPic, image=im)

Other notes:

  • Run import Tkinter as Tk instead of what you have done
  • Fix this: root = Tkinter.Tk() (add parentheses)
  • Change app = InterfaceApp(None) to app = InterfaceApp(root)
  • Remove away app.title("P") and write inside __init__() this self.parent.title("P")
  • Change app.mainloop() to root.mainloop()
https://en.xdnf.cn/q/118031.html

Related Q&A

Read temperature with MAX31855 Thermocouple Sensor on Windows IoT

I am working on a Raspberry Pi 2 with Windows IoT. I want to connect the Raspberry Pi with a MAX31855 Thermocouple Sensor which I bought on Adafruit. Theres a Python libary available on GitHub to read …

PIP: How to Cascade Requirements Files and Use Private Indexes? [duplicate]

This question already has an answer here:Installing Packages from Multiple Servers from One or More Requirements File(1 answer)Closed 9 years ago.I am trying to deploy a Django app to Heroku where one …

Python error: TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

Below is a small sample of my dataframe. In [10]: dfOut[10]:TXN_KEY Send_Agent Pay_Agent Send_Amount Pay_Amount 0 13272184 AWD120279 AEU002152 85.99 85.04 1 13272947 ARA03012…

How to remove grey boundary lines in a map when plotting a netcdf using imshow in matplotlib?

Is it possible to remove the grey boundary lines around the in following map? I am trying to plotting a netcdf using matplotlib.from netCDF4 import Dataset # clarify use of Dataset import matplotlib.p…

function that takes one column value and returns another column value

Apologies, if this is a duplicate please let me know, Ill gladly delete.My dataset: Index Col 1 Col 20 1 4, 5, 6 1 2 7, 8, 9 2 3 10, 11, 12 3 …

Python write value from dictionary based on match in range of columns

From my df showing employees with multiple levels of managers (see prior question here), I want to map rows to a department ID, based on a manager ID that may appear across multiple columns:eid, mid…

Merge two dataframes based on a column

I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ? df1place name qty unit…

Python/Pandas - building a new column based in columns comparison

I have this dataframe:df:CNPJ Revenues 2016 Revenues 2015 Revenues 2014 0 01.637.895/0001-32 R$ 12.696.658 NaN R$ 10.848.213 1 02.916.265/0001-60 …

Present blank screen, wait for key press -- how?

lo,I am currently trying to code a simple routine for an experiment we are planning to run. The experiment starts by entering a subject number and creating a bunch of files. I got that part working. Ne…

Images appearing in all but 1 flask page

I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I cant get any images to load using already working code…