Canceling scheduled actions
Tkinter's scheduling mechanism not only provides methods to delay callback executions, but also to cancel them if they have not been executed yet. Consider an operation that may take too much time to complete, so we want to let users to stop it by pressing a button or closing the application.
Getting ready
We will take the example from the first recipe and add a Stop
button to allow us to cancel the scheduled action.
This button will be enabled only while the action is scheduled, which means that once you click on the left button, the user can wait for 5 seconds, or click on the Stop
button to immediately enable it again:

How to do it...
The after_cancel()
method cancels the execution of a scheduled action by taking the identifier previously returned by calling after()
. In this example, this value is stored in the scheduled_id
attribute:
import time import tkinter as tk class App(tk.Tk): def __init__(self): super().__init__() self.button = tk.Button...