1

My application uses CustomTkinter and reads data on start up:

import customtkinter as ctk
from CTkMessagebox import CTkMessagebox


class App(ctk.CTk):
    def __init__(self) -> None:
        super().__init__()
        self.wnd_root = ctk.CTk()
        self.after_idle(self.on_startup)

    def on_startup(self) -> None:
        # Load required data here, assume it fails
        mbox = CTkMessagebox()
        self.wait_window(mbox)
        self.destroy()


if __name__ == '__main__':
    app = App()
    app.mainloop()

If this fails it outputs a message box and waits for it to close then destroys the window.

This throws a "TclError: can't invoke "wm" command: application has been destroyed" error:

invalid command name "2759755045376check_dpi_scaling"
    while executing
"2759755045376check_dpi_scaling"
    ("after" script)
invalid command name "2759665803776update"
    while executing
"2759665803776update"
    ("after" script)
Traceback (most recent call last):
  File "C:\Users\danie\PycharmProjects\THCompetencyTracker\source\close_test.py", line 20, in <module>
    app.mainloop()
  File "C:\Users\danie\PycharmProjects\THCompetencyTracker\.venv\Lib\site-packages\customtkinter\windows\ctk_tk.py", line 157, in mainloop
    self._windows_set_titlebar_color(self._get_appearance_mode())
  File "C:\Users\danie\PycharmProjects\THCompetencyTracker\.venv\Lib\site-packages\customtkinter\windows\ctk_tk.py", line 319, in _windows_set_titlebar_color
    self.state(self._state_before_windows_set_titlebar_color)  # other states
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danie\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2290, in wm_state
    return self.tk.call('wm', 'state', self._w, newstate)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: can't invoke "wm" command: application has been destroyed
can't invoke "winfo" command: application has been destroyed

Any help?

1
  • You appear to have two separate instances of CTk - you'll likely want to remove this: self.wnd_root = ctk.CTk() Commented Jun 25 at 13:55

1 Answer 1

1

If you create own class based on ctk.CTk then don't use self.wnd_root = ctk.CTk() because it creates second main window with own event loop (mainloop) and later widgets have problem to work correctly.

Application should have only one main window.

For other windows you should use tk.TopLevel() (in tkinter) or ctk.CTkToplevel() (in customtkinter) (without another mainloop())


Code works correctly when I delete self.wnd_root = ctk.CTk()

import customtkinter as ctk
from CTkMessagebox import CTkMessagebox


class App(ctk.CTk):
    def __init__(self) -> None:
        super().__init__()
        #self.wnd_root = ctk.CTk()   # <--- remove it
        self.after_idle(self.on_startup)

    def on_startup(self) -> None:
        # Load required data here, assume it fails
        mbox = CTkMessagebox()
        self.wait_window(mbox)
        self.destroy()


if __name__ == '__main__':
    app = App()
    app.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.