In Tkinter, we sometimes have to create a reference to a converted image (for example), so that it will not be destroyed when it goes out of scope.
A common way is to add a variable to its widget instance. An example of this is:
bard = Image.open("bardejov.jpg") bardejov = ImageTk.PhotoImage(bard) label1 = Label(self, image=bardejov) label1.image = bardejov #<<<<<<<<<<<<<<<<<<<<< label1.place(x=20, y=20) This is part of an example published by Jan Bodnar of Zetcode, with my flagging of the example. bardejov is a local variable in a function, and if you comment out the marked line you don't get the image, because it is destroyed when the function returns, and the label just sees 'none'.
I'm new to Tkinter, and this worried me rather, adding new properties to system code, and someone suggested this:
class S(): # To make an object 'accessible', and thus save # it from garbage collection. fred = [] @classmethod def save(cls, x): cls.fred.append(x) This certainly worked in Jan's example:
bard = Image.open("xxxx.jpg") bardejov = ImageTk.PhotoImage(bard) label1 = Label(self, image=bardejov) #label1.image = bardejov S.save(bardejov) But is it OK? Any undesirable side effects?