+ 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()

24th Aug 2023, 10:19 PM
Reza nj
Reza nj - avatar
6 Respostas
+ 3
S2XPHOENIX🇮🇳 Alexey Kopyshev Thank You my brothers finally it worked:)))))))))))) 🤍🤍
26th Aug 2023, 8:25 PM
Reza nj
Reza nj - avatar
+ 2
show your code. without seeing people can only guess
24th Aug 2023, 10:45 PM
Lisa
Lisa - avatar
+ 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
24th Aug 2023, 11:44 PM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 1
Reza nj look at this thread https://python-forum.io/thread-34264.html
25th Aug 2023, 11:15 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 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
26th Aug 2023, 7:10 PM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
+ 1
Reza nj mention not 😌
27th Aug 2023, 4:28 AM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar