0

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

The GUI of the TKwindow with the SerialClass

The second frame of class MenuFrame, the Go Back button, is which is not working, and i have the same problem on the 3rd class who has the same button

  • Have you created more than one root window? Have you searched this site for the exact error message? – Bryan Oakley Feb 06 '23 at 13:12
  • @BryanOakley hi, no i dont create more than one root window, as you see, i only create it at the start of the application and i send it by the methods, and i think that in that way i dont need to create more root window. – Pinkurauchin Feb 06 '23 at 13:15
  • @BryanOakley and yes i have already searched many posts but any of the solutions that i see works on my code, because i already tried many of them :( that's why i'm asking – Pinkurauchin Feb 06 '23 at 13:16
  • I don't think there's any way we can reproduce this with the code snippets you provided. There are indentation errors and missing code. Please create a [mcve] specifically for this question that can reproduce the problem. – Bryan Oakley Feb 06 '23 at 13:31
  • Okay, i made it https://github.com/pinkurauchin/examples I see that the problem is that is creating again a new TK() but i just want to create a new SerialFrame object :( – Pinkurauchin Feb 06 '23 at 13:50
  • Please do not link to code on another site. [edit] your question to include the [mcve]. This is a perfect illustration of why creating such an example is so valuable - the act of creating the example will often help you to find the error by yourself. – Bryan Oakley Feb 06 '23 at 14:02
  • @BryanOakley i should delete the all original code and write the mre, or just add the new code? Thank you – Pinkurauchin Feb 06 '23 at 14:09

1 Answers1

0

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.

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • This is the right answer, but i modified a little bit the app, and i created a new file for the SerialFrame class too :) Thank you! – Pinkurauchin Feb 06 '23 at 21:41