1

this code is just an example code:

import tkinter as tk
root = tk.Tk()
photoImageObj = tk.PhotoImage(file="signout.png")
lab = tk.Label(root, image=photoImageObj).pack()
photoImageObjj = tk.PhotoImage(file="signout.png")
signout_button=tk.Button(root,image=photoImageObjj).pack()
root.mainloop()

In this code it is working and output is also working fine. here is the output: enter image description here but in my real project, where I am using this button with the image is not working. code:

photoImageObj = PhotoImage(file="signout.png")
signout_button=Button(stem,image=photoImageObj).pack()

here I haven't pasted my whole code since it is having 50-60 line's of code. Output: enter image description here

so if the same code is working fine, then there is problem in my real project file. so can anyone help me debug that.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Hafeezh
  • 61
  • 6

1 Answers1

1

When an image is inside a function you have to keep reference to the image or it will be garbage collected by python. With the code provided its hard to say, but just try something like:

signout_button = Button(stem,image=photoImageObj)
signout_button.pack() #so that signout_button is not None
signout_button.image=photoImageObj #keeping a reference

Alternatively, you could also say global photoImageObj on top of function, but its not recommended to use global. This question should be marked as a duplicate and closed, but just in-case you dont understand what to do in your "specific case", here is the answer.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46