1

I am using the customtkinter library to create a button. This button is to close the program.

Here is the definition of the button:

  exit_button = ctk.CTkButton(master=main_menu_frame,
    text="Exit",
    command=root.destroy,
    corner_radius=0,
    width=WIDTH-40-260,
    height=60,
    text_font=("century gothic", 16),
  )

As you can see the command is equal to root.destroy. And it really closes the window when I click this button, but it give an exception too. Here is the exception:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 501, in clicked
    self.on_leave()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 485, in on_leave
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2903, in itemconfigure
    return self._configure(('itemconfigure', tagOrId), cnf, kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1636, in _configure    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!ctkframe2.!ctkbutton3.!canvas"

Here is some code to test:

import tkinter as tk
import customtkinter as ctk

root = tk.Tk()

btn = ctk.CTkButton(master=root, text="EXIT", command=root.destroy).pack()

root.mainloop()

With this code I get the same exception.

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
leech
  • 367
  • 1
  • 4
  • 16
  • Please provide a [mcve]. The error is caused by your definition of `on_leave`, but you don't provide that definition. – Bryan Oakley Dec 29 '21 at 17:55
  • what do you mean? i haven't used `on_leave` in my code anywhere. – leech Dec 29 '21 at 17:57
  • @BryanOakley that method seems to be defined in some module (`customtkinter`) which is located in `site-packages`, meaning that it is likely installed via `pip` – Matiiss Dec 29 '21 at 18:00
  • i installed custom tinker via pip – leech Dec 29 '21 at 18:01
  • have you tried `command=root.quit`, should have the same effect but is likely achieved a bit differently, about the difference you can read [here](https://stackoverflow.com/questions/2307464/what-is-the-difference-between-root-destroy-and-root-quit), from what I understand it shouldn't raise this exception because it doesn't destroy any widgets directly – Matiiss Dec 29 '21 at 18:03
  • i didn't try that. Is it really the same effect? I read that quit doesn't really close the window. Isn't that true? – leech Dec 29 '21 at 18:04
  • [the difference](https://stackoverflow.com/questions/2307464/what-is-the-difference-between-root-destroy-and-root-quit) is that `destroy` destroys all the widgets (which seems why the error was raised), `quit` doesn't immediately destroy everything it just continues with code after `mainloop` so if you have none, it should just end the program, obviously could just use `command=exit` which will immediately exit the program (not suggested in production but I guess you are not doing that) – Matiiss Dec 29 '21 at 18:08
  • oh, so quit also closes the program. i thought that is still in the memory. Thanks. with quit it works fine. – leech Dec 29 '21 at 18:15
  • 2
    This was a bug in the `customtkinter` library, the button was modified after it was already destroyed, its fixed now and the above code should work with version 1.8 and later! – Tom Jan 01 '22 at 21:54

1 Answers1

0

try like this

import tkinter as tk
import customtkinter as ctk

root = tk.Tk()

def close_app():
   root.destroy()

btn = ctk.CTkButton(master=root, text="EXIT", command=close_app).pack()

root.mainloop()
Thisal
  • 64
  • 4
  • Welcome to Stack Overflow! Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Simas Joneliunas Dec 22 '22 at 04:22