
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
after method in Python Tkinter
Tkinter is a python library to make GUIs. It has many built in methods to create and manipulate GUI windows and other widgets to show the data and GUI events. In this article we will see how the after method is used in a Tkinter GUI.
Syntax
.after(delay, FuncName=FuncName) This method calls the function FuncName after the given delay in milisecond
Displaying Widget
Here we make a frame to display a list of words randomly. We use the random library along with the after method to call a function displaying a given list of text in a random manner.
Example
import random from tkinter import * base = Tk() a = Label(base, text="After() Demo") a.pack() contrive = Frame(base, width=450, height=500) contrive.pack() words = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri','Sat','Sun'] #Display words randomly one after the other. def display_weekday(): if not words: return rand = random.choice(words) character_frame = Label(contrive, text=rand) character_frame.pack() contrive.after(500,display_weekday) words.remove(rand) base.after(0, display_weekday) base.mainloop()
Running the above code gives us the following result:
On running the same program again we get the result showing different sequence of the words.
Stopping Processing
In the next example we will see how we can use the after method as a delay mechanism to wait for a process to run for a certain amount of time and then stop the process. We also use the destroy method to stop the processing.
Example
from tkinter import Tk, mainloop, TOP from tkinter.ttk import Button from time import time base = Tk() stud = Button(base, text = 'After Demo()') stud.pack(side = TOP, pady = 8) print('processing Begins...') begin = time() base.after(3000, base.destroy) mainloop() conclusion = time() print('process destroyed in % d seconds' % ( conclusion-begin))
Running the above code gives us the following result:
processing Begins... process destroyed in 3 seconds
- Related Articles
- destroy() method in Tkinter - Python
- Python - geometry method in Tkinter
- Change command Method for Tkinter Button in Python
- How to hide a widget after some time in Python Tkinter?
- Tkinter bell() method
- How to stop Tkinter after function?
- Automatically close window after a certain time in Tkinter
- Tkinter Programming in Python
- jQuery after() method
- Different messages in Tkinter - Python
- Collapsible Pane in Tkinter Python
- askopenfile() function in Python Tkinter
- Binding function in Python Tkinter
- Progressbar widget in Python Tkinter
- asksaveasfile() function in Python Tkinter
