Home > other >  Python - if Tkinter images created in the function, why not show?
Python - if Tkinter images created in the function, why not show?

Time:12-28

This code is valid:

The import tkinter

Root=tkinter. Tk ()
Canvas=tkinter. Canvas (root)
Canvas. The grid (row=0, the column=0)
Photo=tkinter. PhotoImage (file='./test. GIF ')
Canvas. Create_image (0, 0, image=photo)
Root. Mainloop ()

It showed me images.
Now, compile this code but it did not show my image, I don't know why, because it is the same code, in the class:

The import tkinter

The class Test:
Def __init__ (self, master) :
Canvas=tkinter. Canvas (master)
Canvas. The grid (row=0, the column=0)
Photo=tkinter. PhotoImage (file='./test. GIF ')
Canvas. Create_image (0, 0, image=photo)

Root=tkinter. Tk ()
Test=test (root)
Root. Mainloop ()

CodePudding user response:

Variable photo is a local variable, after the instantiation classes will collect rubbish. Save the photo reference, for example:

The self. The photo=tkinter. PhotoImage (... )

If you are "tkinter image cannot show" on Google search, is the first result is as follows:
http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

CodePudding user response:

  • Related