Home > Net >  How to create a software loading window with TKinter?
How to create a software loading window with TKinter?

Time:11-28

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)
    

CodePudding user response:

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()

CodePudding user response:

Try something like this. Then continue yourself, it's easy

from your_folder import load.py

def wait_and_go():
    if time.sleep(7):
        load.py.form_load.py
  • Related