What's the difference between "update" and "update_idletasks" in Tkinter?


Update method processes all the pending idle tasks, unvisited events, calling functions, and callbacks. The method is applicable for updating and processing all the events or tasks such as redrawing widgets, geometry management, configuring the widget property, etc.

It also ensures that if the application has any pending tasks, then it will just update or refresh the value that affects the whole part of the application. Using update for a single pending task would be nasty, thus Tkinter also provides the update_idletasks() method. It only updates the idle pending task that is stable or not updating in the application for some reason. It calls all the events that are pending without processing any other events or callback.

The update() and update_idletask() methods are useful for processing any pending or idle tasks. However, the only difference between update() and update_idletasks() is that update() processes all the events present in the application, while update_idletasks() only processes those events which are not running or stable.

Example

We can understand the use and application of update_idletasks() method through this example.

# Import the tkinter library
from tkinter import *
from tkinter import ttk
import time

# Create an instance of tkinter frame
win= Tk()

# Set the size of the Tkinter window
win.geometry("700x350")
def add_Text():
   for i in range(10):
      label.config(text= "The loops starts from 1 to "+ str(i))
      # Wait for two seconds
      win.update_idletasks()
      time.sleep(2)
      label.config(text= i)

# Add a label text
label= Label(win, text="Original Text", font= ('Aerial 16'))
label.pack(pady= 30)

# Add a button to update the Label text
ttk.Button(win, text="Change Text", command= add_Text).pack(pady= 40)
win.mainloop()

Output

Running the above code will display a window with a Label widget and a button.

When we press the button, the Label widget gets updated automatically in the given range of the loop.

Updated on: 07-Jun-2021

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements