r/pythonhelp•
181Cade

PhotoImage not working when run from another module.

Firstly, sorry if my wordage in my title is poor; I'm a bit of a programming noob.

Basically I have a module that has a tkinter window and in that I have two buttons that have images instead of text. When I run that module it works perfectly fine. But I have another module with a tkinter window and a button that runs the main function of my other module (with the two picture buttons), and when I press that button, I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
File "_", line 1948, in __call__
  return self.func(*args)
        ^^^^^^^^^^^^^^^^
 File "_", line 10, in run_main
 Button(main_window,image=bin_icon).pack()
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "_", line 2707, in __init__
  Widget.__init__(self, master, 'button', cnf, kw)
 File "_", line 2629, in __init__
   self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist

So I tried to make completely fresh modules and put in the absolute basics.

This is my main module:

from tkinter import *
from test_task_entry import run_main

main_window = Tk()

Button(main_window, text="Click to open secondary window", font=("",20), command=run_main).pack()

main_window.mainloop()

This is my secondary module:

from tkinter import *

def run_main():

	main_window = Tk()

	bin_icon = PhotoImage(file="bin.png")
 	options_icon = PhotoImage(file="gear.png")

 	Button(main_window,image=bin_icon).pack()
	Button(main_window,image=options_icon).pack()

 	main_window.mainloop()

if __name__ == "__main__":
	run_main()

And I get the same error... And yes, my images are in my project folder.

Any help would be greatly appreciated.

Posted Dec 8, 2023, 8:58 PM UTC

11
Loading comments…