How to Change Cursor to Loading in Tkinter

1. Import Tkinter

import tkinter as tk

2. Create the Main Window

root = tk.Tk()

3. Set the Window Title and Size

root.title("Loading Cursor Example")
root.geometry("300x200")

4. Define a Function to Start the Loading Cursor

def start_loading():
    root.config(cursor="watch")  # Change cursor to loading
    root.update()  # Refresh the window to apply changes
    root.after(3000, stop_loading)  # Wait 3 seconds, then reset the cursor

5. Define a Function to Restore the Normal Cursor

def stop_loading():
    root.config(cursor="")  # Reset to normal cursor

6. Create a Button to Trigger the Loading Cursor

button = tk.Button(root, text="Start Loading", command=start_loading)
button.pack(pady=20)

7. Run the Tkinter Event Loop

root.mainloop()

8. Complete Code

import tkinter as tk

root = tk.Tk()
root.title("Loading Cursor Example")
root.geometry("300x200")

def start_loading():
    root.config(cursor="watch")  # Change cursor to loading
    root.update()  # Apply changes
    root.after(3000, stop_loading)  # Reset cursor after 3 seconds

def stop_loading():
    root.config(cursor="")  # Restore normal cursor

button = tk.Button(root, text="Start Loading", command=start_loading)
button.pack(pady=20)

root.mainloop()

Leave a Reply

Your email address will not be published. Required fields are marked *