0

Thanks for taking the time to read this. I'm making a python project that automatically opens my meet links for class! The whole thing went well, but now i want a loading screen. Imagine this:

New window pops up Label appears: "Welcome to google meet!" i slowly config the label using time.sleep() to decrease visibility. then the label gets replaced by my app GUI! Sounded great in my head! Until i built it. The Time.sleep wasnt working, and it automatically skips to the next stage: GUI! and if i remove the gui part, it just stays blank.

My code:

#----------------SETUP-----------------------
from datetime import datetime

import pickle
from tkinter import *
import webbrowser

import datetime
import time
# all my imports
m = Tk()

W = Label(m, text="Welcome to google meet!", fg="#000000")
time.sleep(0.2)
W.pack()
time.sleep(1.2)
W.config(fg="#434343")

time.sleep(0.2)
W.config(fg="#8c8c8c")

time.sleep(0.2)
W.config(fg="#d2d2d2")

time.sleep(0.2)
W.config(fg="#eeeeee")

time.sleep(0.2)
W.config(fg="#ffffff")
W.destroy()


def Load():
    w=Button(m, text="Open meet",bg="#F0F0F0", highlightthickness = 0, bd = 0)
    m.title("Meet for Aparicio by ")
    top = Tk()
    top.title("Club Form")
    copyrightt = Label(m, text="All rights reserved. Copyright by  . Do not distribute as your own, but link to the google docs.")
    L2 = Label(top, text="If this is your first time using this app, please fill out the form. If not, you can just ignore it.")
    L2.pack()
    copyrightt.pack()
    ba = Button(top, text="Submit!") # there was a ,command=submit) but since i deleted the command, i just remvoed this for now, since i only need the GUI.
    L3 = Label(top, text="Academic club link")
    L3.pack()
    E2 = Entry(top, bd =5)
    E2.pack()



    L5 = Label(top, text="Pastoral link")
    L5.pack()
    E4 = Entry(top, bd =5)
    E4.pack()




    L7 = Label(top, text="Interest link")
    L7.pack()
    E6 = Entry(top, bd =5)
    E6.pack()
    ba.pack()
    m.geometry("700x250")
    label = Label(m,text="Press the button below to open the link!")
    img = PhotoImage(file="C:/Users/PKSNFL/Documents/Untitled.png") # make sure to add "/" not "/"

    label.pack()
    w.config(image=img)
    w.pack()
    wekday = datetime.datetime.today().weekday()

    if wekday == 3:
        l2 = Label(m, text="DISCLAIMER: IF YOU RUN THIS TWICE FROM 1:00 PM TO 2:00 PM, MESSAGE    ON HOW TO FIX IT.")
        l3 = Label(m, text="If you don't, the app will give you the wrong link for the rest of its life until you fix it :D")
        l2.pack()
        l3.pack()
Load()
#--------------------------------------------

#
m.mainloop()

I have cut out the code for my meet, and it has no relation to my tkinter code, except for the submit button.

  • Please post a [mcve]. This is missing imports and some definitions to reproduce the issue. – Mark Tolonen Apr 11 '22 at 03:04
  • 1
    The only thing `time.sleep()` does in a Tkinter application is lock up the GUI, preventing any screen updates from happening. Tkinter provides an `.after()` method for scheduling function calls to occur in the future, which is the appropriate approach here. – jasonharper Apr 11 '22 at 03:21
  • Sorry, @MarkTolonen. Will fix! – A Pickacks Apr 11 '22 at 03:47
  • @jasonharper Oh, Now i get it! Thanks! – A Pickacks Apr 11 '22 at 03:52
  • There are many very similar questions on this site, all about how sleep seems to freeze tkinter. Have you done any research before asking this question? – Bryan Oakley Apr 11 '22 at 03:59
  • @BryanOakley... Yes. Not really on this site, but other public sites like tutorialspoint or geeksforgeeks – A Pickacks May 02 '22 at 02:25

1 Answers1

0

As mentioned in one of the comments, time.sleep locks the GUI and prevents any screen updates from taking place. If you want to schedule GUI function calls, you must use:

<widget>.after(<delay-in-milliseconds>, <function>)

I have used a constant sleep duration of 0.2 seconds to achieve the transition. You can modify the durations according to your preference.

time = int(sum(sleep_times[:i+1]) * 1000) 

calculates the cumulated time from the start of the transition.

The reason for using lambda clr = fg_clrs[i]: W.config(fg = clr) instead of lambda : W.config(fg = fg_clrs[i]) is explained in this answer.


Working Code:

from tkinter import *

root = Tk()
root.geometry("600x500")
fg_clrs = ("#000000", "#434343", "#8c8c8c", "#d2d2d2", "#eeeeee", "#ffffff")
sleep_times = (0.2,) * 6
label = Label(root, text="Welcome to google meet!", fg="#000000")
label.pack()

for i in range(len(sleep_times)):
    time = int(sum(sleep_times[:i+1]) * 1000) 
    root.after(time, lambda clr = fg_clrs[i]: label.config(fg = clr))

root.mainloop()
Sriram Srinivasan
  • 650
  • 2
  • 4
  • 14