I'm implementing after()
method to my code after finding out tkinter windows are not multithread-friendly. Back then I couldn't pop up my Toplevel with threading.Thread()
, but seems like after()
method is not working either. When you pressed the button, the Toplevel window (splash_screen) still gets laggy, you can't ever move nor resize it and it eventually freezes. I'm not sure why does it behave like this? Any ideas?
Thanks in advance!
import time
from tkinter import *
from PIL import Image, ImageTk
def splash_screen():
global is_top_level_ready
is_top_level_ready = False
splash_screen = Toplevel(screen)
frame = Frame(splash_screen, width=427, height=250)
frame.place(x=0, y=0)
label1 = Label(splash_screen, text='Please wait...')
label1.place(x=122, y=120)
image_a = ImageTk.PhotoImage(Image.open('dot_light.png'))
image_b = ImageTk.PhotoImage(Image.open('dot_dark.png'))
while True:
if is_top_level_ready == False:
l1 = Label(splash_screen, image=image_a).place(x=180, y=165)
l2 = Label(splash_screen, image=image_b).place(x=215, y=165)
l3 = Label(splash_screen, image=image_b).place(x=250, y=165)
l4 = Label(splash_screen, image=image_b).place(x=285, y=165)
splash_screen.update_idletasks()
time.sleep(0.2)
l1 = Label(splash_screen, image=image_b).place(x=180, y=165)
l2 = Label(splash_screen, image=image_a).place(x=215, y=165)
l3 = Label(splash_screen, image=image_b).place(x=250, y=165)
l4 = Label(splash_screen, image=image_b).place(x=285, y=165)
splash_screen.update_idletasks()
time.sleep(0.2)
l1 = Label(splash_screen, image=image_b).place(x=180, y=165)
l2 = Label(splash_screen, image=image_b).place(x=215, y=165)
l3 = Label(splash_screen, image=image_a).place(x=250, y=165)
l4 = Label(splash_screen, image=image_b).place(x=285, y=165)
splash_screen.update_idletasks()
time.sleep(0.2)
l1 = Label(splash_screen, image=image_b).place(x=180, y=165)
l2 = Label(splash_screen, image=image_b).place(x=215, y=165)
l3 = Label(splash_screen, image=image_b).place(x=250, y=165)
l4 = Label(splash_screen, image=image_a).place(x=285, y=165)
splash_screen.update_idletasks()
time.sleep(0.2)
else:
splash_screen.destroy()
break
the_toplevel = Toplevel(screen)
the_toplevel.minsize(width=650, height=415)
def checkloop():
global is_splash_screen_ready
if is_splash_screen_ready:
splash_screen()
else:
screen.after(100, checkloop)
def splash_is_go():
global is_splash_screen_ready
is_splash_screen_ready = True
screen = Tk()
global is_splash_screen_ready
is_splash_screen_ready = False
button = Button(screen, text="Button", command=splash_is_go)
button.pack(pady=30, padx=30)
screen.after(100, checkloop)
screen.mainloop()
Image sources: