Search code examples
pythonpython-3.xtkintererror-handlingdestroy

How to handle Invalid command name error, while executing ("after" script) in tkinter python


I know this question has been raised multiple times here and I have gone through all of them. But I didn't find a clear solution for the problem. I know the reasons for the occurrence of this error. I know that after using root.destroy(), there are still some jobs left to be completed and all that stuff. But I want to know how to stop those "after" jobs? One of the guys asked to use try/accept in the code. But he didn't show how to use that. So could you please give a clear solution for this case? Is there any way to remove this error? I request you not to mark this question duplicate and don't remove this question please. It's important and I don't have other sources to get my answer.

invalid command name "2272867821888time"
    while executing
"2272867821888time"
    ("after" script)

Solution

  • This error occurs when destroying the window before a callback scheduled with after is executed. To avoid this kind of issue, you can store the id returned when scheduling the callback and cancel it when destroying the window, for instance using protocol('WM_DELETE_WINDOW', quit_function).

    Here is an example:

    import tkinter as tk
    
    def callback():
        global after_id
        var.set(var.get() + 1)
        after_id = root.after(500, callback)
    
    def quit():
        """Cancel all scheduled callbacks and quit."""
        root.after_cancel(after_id)
        root.destroy()
    
    root = tk.Tk()
    root.pack_propagate(False)
    var = tk.IntVar()
    tk.Label(root, textvariable=var).pack()
    callback()
    root.protocol('WM_DELETE_WINDOW', quit)
    root.mainloop()
    

    Also, Tcl/Tk has an after info method which is not directly accessible through the python wrapper but can be invoked using root.tk.eval('after info') and returns a string of ids: 'id1 id2 id3'. So an alternative to keeping track of all ids is to use this:

    import tkinter as tk
    
    def callback():
        var.set(var.get() + 1)
        root.after(500, callback)
    
    def quit():
        """Cancel all scheduled callbacks and quit."""
        for after_id in root.tk.eval('after info').split():
            root.after_cancel(after_id)
        root.destroy()
    
    root = tk.Tk()
    root.pack_propagate(False)
    var = tk.IntVar()
    tk.Label(root, textvariable=var).pack()
    callback()
    root.protocol('WM_DELETE_WINDOW', quit)
    root.mainloop()