I have created a python class which inherits Tk from tkinter library. I want to add a label with an image to it but it only works when I create a Photoimage as variable to 'self'.
This code works:
class HMIDrawer(Tk):
def __init__(self):
super().__init__()
self.frame = Frame(self)
self.img = PhotoImage(file='resources/platoontomtom_empty.png')
self.label = Label(self.frame, image=self.img, bg='white')
self.label.pack()
self.frame.pack()
self.mainloop()
And this code doesn't work:
class HMIDrawer(Tk):
def __init__(self):
super().__init__()
self.frame = Frame(self)
img = PhotoImage(file='resources/platoontomtom_empty.png')
self.label = Label(self.frame, image=img, bg='white')
self.label.pack()
self.frame.pack()
self.mainloop()
Can anyone explain why the first code does work and the second code doesn't?