Python Tkinter after method

In this Python tutorial, we will learn How to create the after method in Python Tkinter and we will also cover different examples related to the after method. And, we will also cover these topics.

  • Python Tkinter after method
  • Python Tkinter after loop
  • Python Tkinter after with arguments
  • Python Tkinter after_cancel

Python Tkinter after method

In this section, we will learn how we can create an after method in Python Tkinter.

The after method is an inbuilt function in Tkinter it calls the callback function once after a delay milliseconds. When the program runs after some seconds the after method call and the window is destroyed.

after() method with the following syntax

after(delay,callback=None)

Code:

In the following code, we import a time library that is used to calculate the time. And also create a window ws=Tk() inside the window add button with the text “Click Here”.

  • Button() can display the text with a single click.
  • start = time()calculating the starting time.
  • ws.after(20000, ws.destroy) methods defined as after 20 sec the main window that is get destroy.
  • end = time()calculating the end time.
from tkinter import *

from time import time

ws = Tk()

button1 = Button(ws, text = 'Click Here')
button1.pack(side = TOP, pady = 5)

print('Welcome to Python Guides Tutorial')

start = time()

ws.after(20000, ws.destroy)

ws.mainloop()

end = time()
print('Destroyed after % d seconds' % (end-start))

Output:

In the following output, we see a window inside the window a button is a place.

Python Tkinter after method
Python Tkinter after method Output

On clicking the button we see the text “Welcome to Python Guides Tutorial” on the command prompt after some sec the after() method calls and the window will destroy.

Python Tkinter after method1
Python Tkinter after method1 Output

Read: Python tkinter label

Python Tkinter after loop

In this section, we will learn how to create an after loop in Python Tkinter.

Loop is a process in which the end is connecting to the beginning. In the after loop, the callback function is called after a delay millisecond and after some time again continues in a loop.

Code:

In the following code, we import the time library as an import time which is used to calculate the time, and also create a class inside the class we create some function.

  • Label() is used to display fields of text or Images.
  • self.label1.after(1000, self.update)method is defined as after 10 sec the main window is getting destroy and schedule another timer.
from tkinter import *
from tkinter import ttk
import time


class digitalwatch(Tk):
    def __init__(self):
        super().__init__()

        
        self.title('Python Guides')
        self.resizable(0, 0)
        self.geometry('300x80')
        self['bg'] = 'white'

        self.style = ttk.Style(self)
        self.style.configure(
            'TLabel',
            background='white',
            foreground='black')

        self.label1 = Label(
            self,
            text=self.time_string(),
            font=('Digital-7', 40))

        self.label1.pack(expand=True)

        self.label1.after(1000, self.update)

    def time_string(self):
        return time.strftime('%H:%M:%S')

    def update(self):
        """ update the label every 1 second """

        self.label1.configure(text=self.time_string())

        
        self.label1.after(1000, self.update)


if __name__ == "__main__":
    ws = digitalwatch()
    ws.mainloop()

Output:

In the following output, we see the digital watch in which time is continuously running after few seconds the start from the beginning and the after the loop in continuation.

Python Tkinter after loop
Python Tkinter after loop Output

Read: Python Tkinter Entry

Python Tkinter after with arguments

In this section, we will learn how to create after with arguments in Python Tkinter.

Tinker using after with argument function in which a function is defined and calling it. Otherwise, it shows an error.

Code:

In the following code we create a window inside the window we add some buttons which defining the function and calling it. After calling it will get the result.

  • starttime = time() calculating the starting time
  • ws.after(50000, ws.destroy) define as after 50 sec the main window will destroyed.
  • endtime = time() calculating the ending time.
from tkinter import *
from time import time
from tkinter.ttk import Button
ws= Tk()
ws.title("Python Guides")
ws.geometry("200x500")
b = Button(ws, text = 'Welcome Here')
b.pack(pady = 77,side = TOP)
blue = Button(ws, text = "blue")
blue.pack( side = TOP)
black = Button(ws, text = "black")
black.pack( side = BOTTOM )
red = Button(ws, text = "red")
red.pack( side = LEFT )
green = Button(ws, text = "green")
green.pack( side = RIGHT)
print('The Python Guides window is running on the screen...')
starttime = time()
ws.after(50000, ws.destroy)
ws.mainloop()
endtime = time()
print('The Tk Widget is closed after % d seconds' % (endtime-starttime))

Output:

After running the above code we get the following output in which the function runs smoothly there is no error.

Python Tkinter after with arguments
Python Tkinter after with arguments Output

The argument is shown on the command prompt and we also see that after 50 seconds the widget gets closed automatically.

Python Tkinter after with arguments1
Python Tkinter after with arguments1 Output

Read: Python Tkinter Button

Python Tkinter after_cancel

In this section, we will learn how to create the add_cancel function in Python Tkinter.

As we are creating a schedule using the after() function that closes the main window which time is given to them to stop this scheduling or we can say that in order to stop a particular schedule we use the after_cancel function.

Code:

In the following code, we use after(5000,lambda:ws.destroy()) function that closes the main window after 5 sec but after applying after_cancel(),it will stop the after function.

after_cancel()function is used to stop the scheduling of the after()function.

lambda function have only one expression it can take any type of argument.

from tkinter import *
from tkinter import ttk

ws = Tk()
ws.title("Python Guides")

ws.geometry("700x300")

Label(ws, text= "Welcome to Python Guides Tutorial",
font=('Helvetica 20 bold')).pack(pady=20)

ws.after_cancel(ws)
ws.after(5000,lambda:ws.destroy())
ws.mainloop()

Output:

After running the above code we will get the following output we see a window that shows some text on the screen. When we remove the after_cancel() function the window will close in 5 sec to remove the schedule of the after() function we use the after_cancel() function.

Python Tkinter after_cancel
Python Tkinter after_cancel Output

You may also like to read the following Tkinter tutorials.

So, in this tutorial, we discussed Python Tkinter after method and we will also cover different examples. Here is the list of examples that we have covered.

  • Python Tkinter after method
  • Python Tkinter after loop
  • Python Tkinter after with arguments
  • Python Tkinter after_cancel