1

I have tried to solve this on my own for hours now, but I haven't been able to do so. If this question is already posted, please tell me where to find it, because I couldn't. I'll try to explain myself as best as possible and include some parts of the code, but I can't show the entire code. It's rather extensive by now (and in Spanish, I'm in Latin America).

I have a frame on a Tk window. The frame is subdivided in frames to distribute the UI. One of the frames has some buttons with images instead of text.

The problem is that the buttons only appear if and only if there is an exception after I pack them. Here's the related code

imgClose = PhotoImage(file="Images/close.png")
close = Button(cmdBar, image=imgClose, borderwidth=0, highlightthickness=0, cursor="hand2", command=lambda: defClose())
close.config(width=32, height=32)
close.pack(side=RIGHT)

imgSave = PhotoImage(file="Images/save.png")
save = Button(cmdBar, image=imgSave, borderwidth=0, highlightthickness=0, cursor="hand2", command=lambda: defSave())
save.config(width=32, height=32)
save.pack(side=RIGHT)

print(ThisVarDoesNotExist) #<-- Only if there is an error the buttons show on screen

If I leave the last print(), the buttons show on screen, but it throws an exception. If I remove or comment the print(), there is no exception, but the buttons won't appear.

The frame containing the buttons is placed with grid() and everything is on a class that inherits from Frame.

class MyClass(Frame):

This class is created and placed in a frame in a Tk window.

Everything appears on screen where it's supposed to be, but these buttons require an error to occur. Other buttons on other frames that also have images appear and work just fine.

Perhaps it's worth mentioning that the main Tk window is in the project root directory. The frames that I'm showing are in a subdirectory (subfolder) called Modules. The images are in their own folder Images. The directory structure is as follows (consider main.py has the Tk window):

Images
    save.png
    close.png
Modules
    myClass.py
main.py

The file main.py and the folders Images and Modules are at the same level. Notice that when I load PhotoImage, I use "Images/file.png", as if Images were within Modules, but the weird part is that the buttons show with the exception. Python does find them, but if I remove the error, they won't show.

Or could it be that the cmdBar is the one that doesn't show? The frame containing the buttons. They're all it has.

martineau
  • 119,623
  • 25
  • 170
  • 301
MaoMonroy
  • 117
  • 2
  • 12
  • There's nothing obviously wrong based your description. Please provide a runnable [mre]. That said, it occurs to me it might along the lines of [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). – martineau Feb 04 '21 at 00:46

1 Answers1

1

Earlier I faced the same error , and couldn't figure it out , but i have finally solved the problem . After this line ,

....

save = Button(cmdBar, image=imgSave, borderwidth=0, highlightthickness=0, cursor="hand2", command=lambda: defSave())

....

Add ,

save.image = imgSave 

This stores the photo as a reference and doesn't disappear as soon as the class ends

Hope it worked !

  • that did the trick. I'm new in Python, mostly I work with .NET, PHP and others, and it's the first time I come across something like this. The property with the image is added in the constructor, and then it has to be reassigned as a property? Doesn't make sense, but well, if it works... – MaoMonroy Feb 04 '21 at 16:56