+ 1
Sick Python or being noob?
I wanna add an icon to my virtual app using tkinter library ... By PhotoImage() could load the image with valid size and PNG format with no ERROR. Then assign it to a button but only back ground color is shown and no icon is visible :( I tried it with same commands in an empty python file and it worked well but Idk wtf happens in my main app attempted too much and there is no reason .. also used PIL library and same failure occurred ... plz help guys it is around 30 days working on it and find nothing 😔 code: from tkinter import * t = Tk() img = PhotoImage(file ="image adress.png") b1 = Button(t,image=img) b1.pack() t. mainloop()
6 Respostas
+ 3
S2XPHOENIX🇮🇳
Alexey Kopyshev
Thank You my brothers finally it worked:)))))))))))) 🤍🤍
+ 2
show your code. without seeing people can only guess
+ 2
"When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.
To avoid this, the program must keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute."
Just add this one:
b1.image = img
+ 1
from tkinter import *
t = Tk()
img = PhotoImage(file="image_address.png")
b1 = Button(t, image=img)
b1.image = img # Keep a reference to the image object
b1.pack()
t.mainloop()
Try this
+ 1
Reza nj mention not 😌