Reputation: 15
I am building an application which have a main menu and some side menus/forms. Those forms are created in separate files as following:-
from tkinter import *
import tkinter as tk
class side_test():
def __init__(self):
self.window = Tk()
self.window.geometry("500x500+100+100")
self.my_image = tk.PhotoImage(file='req_hv.png')
self.my_button = Button(self.window, image=self.my_image)
self.my_button.image = self.my_image
self.my_button.pack()
def run(self):
self.window.mainloop()
a main menu is created in a separate file as:-
from tkinter import *
import tkinter as tk
from side_test import side_test
class main_test():
def __init__(self):
self.window = Tk()
self.window.geometry("300x300")
self.main_image = tk.PhotoImage( file='req.png')
self.my_button = Button(window, image=self.main_image, command=self.side_from)
self.my_button.image = self.main_image
self.my_button.pack()
def side_form(self):
my_obj = side_test()
my_obj.run()
def run(self):
self.window.mainloop()
if __name__ == '__main__':
obj = main_test()
obj.run()
as u can see I am calling side_test class. upon execution it through following error:-
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__return self.func(*args)
File "f:\Docs\OneDrive\Python Projects\test\main_test.py", line 18, in side_form
my_obj = side_test()
File "f:\Docs\OneDrive\Python Projects\test\side_test.py", line 11, in __init__
self.my_button = Button(self.window, image=self.my_image)
File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2679, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2601, in __init__
self.tk.call(
_tkinter.TclError: image "pyimage2" doesn't exist
but if i run side_test file separately, it run correctly. I dont know why tcl do not locate picture file while called from another file.
Upvotes: 0
Views: 315
Reputation: 15
For more than one instance of Tk(), master option shall be used e.g.
tk.PhotoImage(file='example.png', master=self)
Upvotes: 0
Reputation: 4551
Don't use wildcard from tkinter import *
for both modules.
Snippet modified the code to improve readability:
side_test.py.
side_test
import tkinter as tk
class side_test():
def __init__(self):
self.window = Tk()
self.window.geometry("500x500+100+100")
self.my_image = tk.PhotoImage(file='p1.png')
self.my_button = Button(self.window, image=self.my_image)
self.my_button.image = self.my_image
self.my_button.pack()
self.window.mainloop()
def run():
side_test()
main.py.
import tkinter as tk
from side_test import side_test
class main_test():
def __init__(self):
self.window = Tk()
self.window.geometry("300x300")
self.main_image = tk.PhotoImage( file='p2.png')
self.my_button = tk.Button(self.window, image=self.main_image, command=self.side_from)
self.my_button.image = self.main_image
self.my_button.pack()
self.window.mainloop()
def side_form():
side_test()
my_obj.run()
def run():
side_form()
if __name__ == '__main__':
run()
Screenshot:
Upvotes: 0