Invalid command name while executing ("after" script)

11,217

Solution 1

If you destroy the window, whatever "after" jobs that have already been scheduled may run. If the window is destroyed and this job interacts with a widget that has been deleted, you'll get this error.

You can either put a try around the code and ignore such an error, check that the window exists before trying to configure it, or put a handler in for when the main window is destroyed to delete any pending "after" jobs.

Solution 2

Background

I was getting these 'errors' as well. They're not actual exceptions, they're just annoying to see being spammed in the terminal when running unittests.

I had tried a lot of things, including overriding the after method in tkinter.Tk to keep track of any queued methods and then calling tkinter.Tk.after_cancel() automatically before calling tkinter.Tk.destroy() as @GabrielStaples commented.

So I was getting these errors even though there were no queued after methods at the point of destroy() being called.

My solution

What worked for me was calling tkinter.Tk.quit() to destroy the window instead of destroy(). I read that quit() doesn't stop the mainloop but it seems fine. Any methods queued by after aren't called after quit() has been called.

Perhaps somebody could explain any consequences I'm not aware of if there are any

Share:
11,217
TinnyT
Author by

TinnyT

Updated on July 24, 2022

Comments

  • TinnyT
    TinnyT over 1 year

    As solve this problem? I'm running this code, window is created, but in console appears message on the error. I think problem the fact that is "after" loop not terminate but the window already destroyed.

    Code:

    import Tkinter as tk
    import time
    
    class App():
        def __init__(self):
            self.root = tk.Tk()
            self.label = tk.Label(text="")
            self.label.pack()
            self.update_clock()
            self.root.mainloop()
    
        def update_clock(self):
            now = time.strftime("%H:%M:%S")
            self.label.configure(text=now)
            self.root.after(1000, self.update_clock)
    
    app=App()
    

    A message in console:

    invalid command name "66120320callit"
    while executing
    "66120320callit"
    ("after" script)
    

    Sorry for my small information in first post. I'm using Spyder IDE, and bugs see in spyder console, wherein run repeatedly my code. A description this bugs I find in the python bug tracker as "wait_variable hangs at exit"

    • Kevin
      Kevin over 9 years
      That's strange. It works on my machine. (once I correct the indentation on the lines following def __init__)
    • user3885927
      user3885927 over 9 years
      works for me as well!
  • Gabriel Staples
    Gabriel Staples over 7 years
    If you have a manual quit button, you can use the after_cancel() method to cancel an after method before calling root.destroy() to kill your program. Documentation here: effbot.org/tkinterbook/widget.htm
  • TheLizzard
    TheLizzard about 3 years
    The link above is dead. Found it on the wayback machine: web.archive.org/web/20201112030233/http://effbot.org/…