【发布时间】:2020-04-07 02:33:44
【问题描述】:
好吧,我正在编写一个程序来为应该在循环中显示字母的文本设置动画: T
日
寿
汤姆
托马
托马斯
托马斯
托马斯苏
托马斯成功了……
以此类推,直到它重置然后再次循环。 问题是,tkinter 主循环似乎只运行一次然后退出。 代码如下:
from tkinter import *
import time
def setting():
global thoms
if thoms.get() == "":
thoms.set("T")
return
if thoms.get() == "T":
thoms.set("Th")
return
if thoms.get() == "Th":
thoms.set("Tho")
return
if thoms.get() == "Tho":
thoms.set("Thom")
return
if thoms.get() == "Thom":
thoms.set("Thoma")
return
if thoms.get() == "Thoma":
thoms.set("Thomas")
return
if thoms.get() == "Thomas":
thoms.set("Thomas s")
return
if thoms.get() == "Thomas s":
thoms.set("Thomas su")
return
if thoms.get() == "Thomas su":
thoms.set("Thomas suc")
return
if thoms.get() == "Thomas suc":
thoms.set("Thomas suck")
return
if thoms.get() == "Thomas suck":
thoms.set("Thomas sucks")
return
if thoms.get() == "Thomas sucks":
thoms.set("")
return
window = Tk()
thoms = StringVar()
lbl = Label(window, textvariable=thoms)
lbl.grid(row=1, column=1)
setting()
time.sleep(1)
print("Run")
window.mainloop()
它第一次将变量设置为 T 然后停止,所以我输入 print 以查看它是否正在循环,它只打印到控制台一次。我该如何解决这个问题?
【问题讨论】:
-
mainloops()一直运行,但你的函数只执行一次(甚至在主循环开始之前),所以它只设置一个值。你必须以完全不同的方式去做。您必须使用root.after(millisiecond, settings)定期运行您的函数。 -
@furas 为什么函数只运行一次?它在主循环中,所以它不应该在每次主循环运行时运行吗?
-
因为你只执行一次。
mainloop不知道你想运行它很多次。它甚至不知道有一些setting()函数。