Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Python Tkinter Image Problems

+14 votes
asked Jan 7 by Eidnoxon (4,850 points)
closed Jan 21 by Eidnoxon

I'm, trying to make my app right now, but with a lot of errors. I tried to use images as backgrounds for the buttons so that they'll look better. But whenever I run my program, open up another window with the button I made, there is this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\szab9\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
  File "C:\Users\szab9\OneDrive\Asztali Gép\VM\add_element.py", line 21, in addElementFunc
    add_element.iconphoto(False, logo)
  File "C:\Users\szab9\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2183, in wm_iconphoto
    self.tk.call('wm', 'iconphoto', self._w, *args)
_tkinter.TclError: can't use "pyimage18" as iconphoto: not a photo image

I'm using the same logo for every window, and I usually use this logic:
logo = PhotoImage(file="*path*")
root.iconphoto(False, logo)

If I run the first window, it's fine. After I open up the other window, it just gives an error mention previously. If I open the "other" python file, it just runs perfectly fine. (python button1_functionality.py)

Please fix my difficulties, it may be because I'm using the same photo over and over again, but how do I fix that? Please help.

closed with the note: I GOT ANSWER! "Just use Toplevel() instead of Tk()"

2 Answers

0 votes
answered Jan 8 by Aitor Molano Lázaro (140 points)
Maybe you have to select the folder where the file is or say at the beginning to recognise the file.
commented Jan 10 by Eidnoxon (4,850 points)
I can't, since I'm using PhotoImage, it needs a default root.
0 votes
answered Jan 9 by Shubham Dwivedi (140 points)
It seems like you're encountering an issue with reusing the same PhotoImage object for multiple windows in Tkinter. The problem is that once the PhotoImage object is associated with a window, it cannot be reused for another window.

To fix this, you should create a new PhotoImage object for each window. Try modifying your code like this:

from tkinter import PhotoImage

# Create a new PhotoImage object for each window
logo = PhotoImage(file="*path*")

# Use logo for the first window
root.iconphoto(False, logo)

# For the second window, create a new PhotoImage object
second_logo = PhotoImage(file="*path*")
second_window.iconphoto(False, second_logo)

This way, you create separate PhotoImage objects for each window, and it should resolve the issue you're facing.
commented Jan 10 by Eidnoxon (4,850 points)
You may be right, but your assumption seems a little absurd. PhotoImage has a garbage collector (Not sure, correct me if I'm wrong), and that may be the problem. Also, I use seperate .py folders to store button functionalities, so when there is a button that opens up a window, I import that function from the file, and use that function as the button's command. In a nutshell: "logo" variable would not be recognised inside the other file, so it would be irrelevent to make another variable. I also tried that but it didn't work.  I even tried to use PIL's modules: Image and ImageTk, but still failed. Thanks for trying to help though :D
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.