i'm working with classes on tkinter and i have this problem:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 549, in _clicked
self._command()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\input_frame.py", line 88, in go_back
from main import SerialFrame
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 126, in <module>
SerialFrame(root).place(x=25, y=50)
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 20, in __init__
self.createWidgetsMain()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\main.py", line 101, in createWidgetsMain
refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 106, in __init__
self._draw()
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 261, in _draw
self._update_image() # set image
File "D:\PYCHARM\pycharmprojects\lumacol_frontend\venv\lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 172, in _update_image
self._image_label.configure(image=self._image.create_scaled_photo_image(self._get_widget_scaling(),
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1675, in configure
return self._configure('configure', cnf, kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1665, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
This is the code on my application and explanation about how it should work:
First of all, i have a file with the class SerialFrame, and the creation of the window and the frame:
class SerialFrame(customtkinter.CTkFrame):
# CONSTRUCTOR FOR THE FRAME
def __init__(self, master, *args, **kwargs):
super(SerialFrame, self).__init__(master)
self.master = master
self.serial_port = ""
self.configure(width=400, height=400)
self.createWidgetsMain()
# METHOD TO CREATE ALL WIDGETS
def createWidgetsMain(self):
...
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
And i have another 2 files with other diferent classes for other frames in similar way.
The problem is when i press a button to go back to the first frame, here is the code in the other classes:
def go_back():
self.destroy()
btn_back.destroy()
from main import SerialFrame
SerialFrame(self.master).place(x=25, y=50)
btn_back = customtkinter.CTkButton(self.master, text="Go Back",
command=go_back, cursor="hand2")
btn_back.place(x=465, y=400)
Obviously, while coding the app i had many different problems and if you see something that shouldn't be work well, you can tell me.
I think that probably the error would come here. This code is on def createWidgetsMain, on the main file, and the SerialFrame class.
my_image = customtkinter.CTkImage(light_image=Image.open("images/refresh.png"),
dark_image=Image.open("images/refresh.png"),
size=(20, 20))
# CREATE REFRESH BUTTON
refresh_serials = customtkinter.CTkButton(master=self, command=refresh_menu, image=my_image, width=20,
text="")
I think that when i press the go_back button, on the other classes, it should create a new object of SerialFrame class and place in the root. Obviously, when i create the other frames, i always send the root, the Tk().
Here is the code of the button to go create the other classes (it's inside the createWidgedsMain method):
def segmented_button_callback(value):
if value == "Inputs":
self.destroy()
input_frame.InputFrame(self.master, back).place(x=75, y=75)
if value == "Menu":
try:
connection = back.get_connection()
self.destroy()
menu_frame.MenuFrame(self.master, back).place(x=25, y=75)
except:
self.destroy()
SerialFrame(self.master).place(x=25, y=50)
segemented_button = customtkinter.CTkSegmentedButton(master=self,
values=["Menu", "Inputs"],
command=segmented_button_callback)
All application works well, my only problem is that, thank you. Here are some pics of the app
Since you have the following code inside main.py
:
...
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
back = backend.MyAppBackend()
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
So when the line from main import SerialFrame
inside go_back()
is executed, another instance of customtkinter.CTk()
will be created which causes the exception.
You need to put the code block inside a if __name__ == "__main__"
block as below:
...
if __name__ == "__main__":
# CREATING THE APP
root = customtkinter.CTk()
root.geometry("700x500")
root.title("Lumalcol Conf")
# CREATING THE FIRST FRAME CALLING THE CLASS MY APP
SerialFrame(root).place(x=25, y=50)
root.mainloop()
Then the code block will not be executed when main
is imported.