Darrius
Darrius

Reputation: 177

Simple loading screen in python Tkinter

I am a beginner python learner, tkinter in particular.

I want to make a 'loading screen' of a simple python script and closes after the script ends.

But making a window requires a mainloop function which means that it will loops infinitely or wait for a user interaction(or so i think) and it will eliminate the idea of a 'loading' screen.

I tried something but ends up with (Put Loading Screen) -> (Loading screen still have mainloop) -> (Can't Run Script because of waiting)

What i wanted in detail was (Put Loading Script) -> (Run Script) -> (Script ends) -> (Loading Screen destroy)

I got a lot of experience in other languages especially Java but java can just declare a frame -> run other things afterwards -> call a frame.dispose() and that's just it. Any tips or suggestions for a learner?

EDIT: The script actually is a image processing algorithm that connects to a database and I can't just put a timed wait or sleep since the database can be expanded and it might be take longer than the allocated time.

Upvotes: 4

Views: 21168

Answers (2)

Dadmehr
Dadmehr

Reputation: 11

import tkinter as tk
from tkinter import ttk

def loading_page():

    def close_loading():
        root.destroy()
        
    root = tk.Tk()
    root.title("Loading Page")
    root.geometry("300x200")
    root.resizable(False, False)

    # Create a label for the loading message
    loading_label = ttk.Label(root, text="Loading...", font=("Arial", 14))
    loading_label.pack(pady=50)

    # Create a progress bar
    progress_bar = ttk.Progressbar(root, length=200, mode='indeterminate')
    progress_bar.pack()

    # Schedule closing the loading window after 5 seconds
    root.after(5000, close_loading)

    # Start the progress bar animation
    progress_bar.start(10)

    root.mainloop()

Upvotes: 1

Dronir
Dronir

Reputation: 975

Something along these lines might work for you. This creates the window root, and defines a function task which destroys root as the last thing it does. In this example, task just sleeps for two seconds, but you'd replace that sleep call with whatever code you want to run.

You put the task function into the main loop event queue with root.after(200, task). This means the code will first create the root window, wait 200 milliseconds, then call task(), which sleeps for two seconds and destroys the window. At least for this example you need the 200 millisecond delay so that the main loop has enough time to draw the window before the sleep call halts everything (the number might be different for you; increase it if the window doesn't draw properly).

import tkinter as tk
from time import sleep

def task():
    # The window will stay open until this function call ends.
    sleep(2) # Replace this with the code you want to run
    root.destroy()

root = tk.Tk()
root.title("Example")

label = tk.Label(root, text="Waiting for task to finish.")
label.pack()

root.after(200, task)
root.mainloop()

print("Main loop is now over and we can do other stuff.")

Edit: Added a comment to the code.

Upvotes: 12

Related Questions