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.