Tkinter 不会因睡眠而停止。为什么?

Tkinter is not being stopped by sleep. Why?

感谢您抽出宝贵时间阅读本文。我正在制作一个 python 项目,它会自动打开 class 的会议链接!一切顺利,但现在我想要一个加载屏幕。想象一下:

弹出新 window 出现标签:“欢迎参加 google 见面会!” 我使用 time.sleep() 慢慢配置标签以降低可见性。 然后标签被我的应用程序 GUI 取代! 在我脑海中听起来很棒!直到我建造它。 Time.sleep 没有工作,它会自动跳到下一阶段:GUI!如果我删除 gui 部分,它就会保持空白。

我的代码:

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

我已经删除了我的会议代码,它与我的 tkinter 代码无关,除了提交按钮。

如其中一条评论所述,time.sleep 锁定了 GUI 并阻止进行任何屏幕更新。如果你想调度GUI函数调用,你必须使用:

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

我使用了 0.2 秒的恒定睡眠持续时间来实现转换。您可以根据自己的喜好修改时长。

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

计算从转换开始的累计时间。

使用 lambda clr = fg_clrs[i]: W.config(fg = clr) 而不是 lambda : W.config(fg = fg_clrs[i]) 的原因在 答案中有解释。


工作代码:

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