Tkinter – Refresh Window

Tkinter – Refresh Window

In Tkinter, you can refresh or redraw the entire window. It forces the window to redraw and update any pending tasks, thus providing a visual refresh.

Tkinter - Refresh Window - GUI Example
Tkinter – Refresh Window

To refresh the window in Tkinter, call update() method on the Tk window object, followed by the update_idletasks() method.

window.update()
window.update_idletasks()

where

  • window is the Tk window object.

In this tutorial, you will learn how to refresh the Tk window in Tkinter, with an example program.

Example

In this example, we create a simple GUI application with a label and button. When user clicks on the button, an event handler function is called, in which we update or refresh the window.

Python Program

import tkinter as tk

def refresh_window():
    # Redraw the window
    window.update()
    window.update_idletasks()
    print("Refresh completed.")

# Create the main window
window = tk.Tk()
window.geometry("300x300")
window.title("PythonExamples.org")

label = tk.Label(window, text="Click the below button to refresh the window.")
label.pack()

button = tk.Button(window, text="Refresh", command=refresh_window)
button.pack()

# Run the application
window.mainloop()

Output in Windows

Output in MacOS

Tkinter - Refresh Window - GUI Example
Tkinter – Refresh Window

Click on the Refresh button.

Tkinter - Refresh Window - Terminal Output

Summary

In this Python Tkinter tutorial, we learned how to redraw the window and update any idle tasks by refreshing the window using udate() and update_idletasks() methods of the Tk class in Tkinter, with examples.

Code copied to clipboard successfully 👍