Creating a Loading/Splash Screen in Python Tkinter Using a GIF with Multithreading for Running a Background Task

Posted by

Python Tkinter Loading/Splash Screen Using a GIF

Python Tkinter Loading/Splash Screen Using a GIF & Multithreading to Run a Task in Background

When developing a Python Tkinter application, you may want to provide a loading or splash screen to give users feedback that the application is performing a task. One way to achieve this is by using a GIF image for the loading animation.

Additionally, if the task being performed requires some processing time, it is recommended to run it in a separate thread to prevent the GUI from freezing. This can be achieved using Python’s threading module.

Code Example:

        
import tkinter as tk
from tkinter import ttk
import threading
import time

def task():
    # Simulating a long-running task
    time.sleep(5)
    
def run_task():
    thread = threading.Thread(target=task)
    thread.start()
    
def splash_screen():
    root = tk.Tk()
    root.title("Loading...")
    
    img = tk.PhotoImage(file="loading.gif")
    loading_label = tk.Label(root, image=img)
    loading_label.pack()
    
    ttk.Button(root, text="Run Task", command=run_task).pack()
    
    root.mainloop()

# Run the splash screen
splash_screen()
        
    

In the code example above, we define a task() function that simulates a long-running task with time.sleep(5). We then create a thread to run this task in the background using threading.Thread(target=task). The run_task() function starts this thread.

The splash_screen() function creates a Tkinter window with a GIF image for the loading animation. When the “Run Task” button is clicked, the run_task() function is called to start the background task without freezing the GUI.

By combining a loading splash screen with multithreading in Python Tkinter, you can provide a better user experience and keep your application responsive during long-running tasks.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@FabioMusanni
8 months ago

💻 Source Code + GIF: https://www.buymeacoffee.com/fabiomusanni/e/187015
⬇️ LEARN ON THE BEST LEARNING PLATFORMS (LINKS BELOW) 😉💪 ⬇️
Buy me a coffee: https://www.buymeacoffee.com/fabiomusanni
❤️ Support me monthly: https://www.patreon.com/FabioMusanni
😍 One-off donation: https://www.paypal.me/FabioMusanni/

SKILLSHARE
(Python, Web Dev, UI/UX Design, Music, Art, Animation and a lot more)
🔗 https://skillshare.eqcm.net/5gxzD2 (Affiliate)

DATACAMP
(Python, ChatGPT, SQL, Power BI, and a lot more)
🔗 https://datacamp.pxf.io/vN1bDj (Affiliate)

COURSERA PYTHON
(For beginners, Data Science, Data Analysis, AI, Cybersecurity and a lot more):
🔗 https://imp.i384100.net/k0Nk60 (Affiliate)

COURSERA WEB DEVELOPMENT
(Full Stack, Front-End, Back-End, Web Design and a lot more):
🔗 https://imp.i384100.net/EKWxBW (Affiliate)

Thank you for the support!❤

🎥All videos about Tkinter: https://www.youtube.com/playlist?list=PLs8qUrmRvaR1M1AatvOUy3eF_yoVEBJXk
🎥All my videos about Python: https://www.youtube.com/playlist?list=PLs8qUrmRvaR0IT4IwJl-LSweAdACW-yLK

@robindeniet
8 months ago

This was worth the wait since the how to play a gif video! Yet again a very explainatory video and I can't wait to implement it to my application.