1

With Tkinter how can i create a small software loading window? A window whose hard loading for example 7 seconds and then automatically open the software, for example the home.py file.

How can I open another window automatically after 7 seconds? Thanks

from tkinter import *
from tkinter import ttk
import tkinter as tk
import time

root =  Tk()
root.title("load")
root.geometry("300x300")
root.state("normal")

lbl = Label(root, text="EXAMPLE", font=("Calibri 15 bold"))
lbl(x=40, y=40)


def wait_and_go():
    time.sleep(7)
    
3
  • 3
    I suggest you create a ttk.Progressbar and use the universal widget after() method to update it for as long a needed. Commented Nov 28, 2021 at 2:49
  • @Zankaclowski do you mean a loading animation for 7 seconds? Commented Nov 28, 2021 at 6:18
  • tkinter need to run mainloop to work - it creates window, gets mouse/key events from system, sends them to widgets, and redraws widgets in window, and it do it in loop. if you use sleep() then this loop can't work and you can't see anything or it is frozen. You have to use after(7000, func) to run func() after 7 seconds and this func() should do what you want. Commented Nov 28, 2021 at 6:34

1 Answer 1

2

tkinter (similar to all other GUIs in Python and other languages) needs to run special loop to work. This loop creates window, gets mouse/key events from system, sends them to widgets, and redraws widgets in window, and it do it again and again. And you need root.mainloop() for this.

If you use sleep() then this loop can't work and you can't see anything or it is frozen. But tkinter can use root.after(7000, func) to run func() after 7 seconds and this func() should do what you want.

import tkinter as tk  # PEP8: `import *` is not preferred

# --- function ---

def func():
    print('Now I can run something else')
    #root.destroy()
    
# --- main ---

root = tk.Tk()

lbl = tk.Label(root, text="EXAMPLE")
lbl.pack(padx=50, pady=50)  # padx=(50,0), pady=(50,50)

root.after(7000, func)  # function's name without `()`

root.mainloop()

You can also use after() to run again and again function which can change text in label and countdown time.

import tkinter as tk  # PEP8: `import *` is not preferred

# --- function ---

def func():
    print('Now I can run something else')
    root.destroy()  # close window

def update_label():
    global counter  # inform function to assign new value to external/global variable
    
    counter -= 1
    
    lbl['text'] = f'COUNTDOWN: {counter} s'
    
    if counter > 0:
        root.after(1000, update_label)
    
# --- main ---

counter = 7  # this is global variable 

root = tk.Tk()

lbl = tk.Label(root, text=f'COUNTDOWN: {counter} s')
lbl.pack(padx=50, pady=50)  # padx=(50,0), pady=(50,50)

root.after(7000, func)  # function's name without `()`

root.after(1000, update_label)  # function's name without `()`
#update_label()

root.mainloop()

EDIT:

Example with ttk.Progressbar.

import tkinter as tk  # PEP8: `import *` is not preferred
import tkinter.ttk as ttk

# --- function ---

def func():
    print('Now I can run something else')
    root.destroy()  # close window
    
# --- main ---

root = tk.Tk()

pb = ttk.Progressbar(root, maximum=7.001)
pb.pack(padx=50, pady=50)  # padx=(50,0), pady=(50,50)
pb.start(1000)  # start and change every 1000ms
#pb.step(-1)    # move back one step (to 0) because `start()` moves automatically to `1`

root.after(7000, func)  # function's name without `()`

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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