- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to stop Tkinter after function?
Tkinter functions can be created with the help of threading concept where we define, when a function should run or stop. A Tkinter function can be scheduled using the after(time, callback) function.
Let us suppose that we have created a callback function that forces the main window to be closed after some time. There might be times when we need to stop the scheduling of the function. In order to cancel or stop a particular schedule of a callback function, we can use after_cancel(widget) function.
In the given example, the script will close the main window after 3 seconds but after initializing after_cancel (parent), it will stop the after function to be executed.
#Import the required Libraries from tkinter import * from tkinter import ttk #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter frame win.geometry("750x270") #Initialize a Label widget Label(win, text= "This window will get closed after 3 seconds...", font=('Helvetica 20 bold')).pack(pady=20) #Automatically close the window after 3 seconds win.after_cancel(win) win.after(3000,lambda:win.destroy()) win.mainloop()
Running the above code will display a window that will show some message on the screen.
If we will remove the line win.after_cancel(win), the window will close automatically after 3 seconds.
- Related Articles
- How to stop a loop with a stop button in Tkinter?
- How to stop Tkinter Message widget from resizing?
- How to stop Tkinter Frame from shrinking to fit its contents?
- How to stop copy, paste, and backspace in text widget in tkinter?
- How do you create a Tkinter GUI stop button to break an infinite loop?
- How to hide a widget after some time in Python Tkinter?
- Why does a moving ball stop after sometime?
- after method in Python Tkinter
- How to connect a progress bar to a function in Tkinter?
- How to clear the Entry widget after a button is pressed in Tkinter?
- Function to close the window in Tkinter
- How to call a function using the OptionMenu widget in Tkinter?