4

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.

1

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the response but i really want to make it as dynamic as possible and avoid timed scenario.
I'm not exactly sure what you mean, but this doesn't depend on any of the timing used in my example. The sleep call is just a dummy replacement for the computation during which you want the window to stay open. The 200 ms delay in the queueing in just to give tk some time to render the window because the sleep would otherwise hang it.
It worked. I had to learn how to use it and didn't understand it at first. Thanks
You don't really need the ´.mainloop()´ and a ´.destroy()´ call within the function. As the loading screen is static and does not wait for user input, you can just use ´root.update()´ before running the task and ´root.destroy()´ after the task. So, the task does not have to know the name of the loading screen widget.
1
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()

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.