你好,我在做计时器。这是我的密码:
from tkinter import*
import time
Screen=Tk()
Screen.resizable(0,0)
myText=Label(Screen,text="Welcome To X Timer!",font=(None,50),bg="green")
myText.pack()
aText=Label(Screen,text="0:0",font=(None,30))
aText.pack()
def start_timer():
x=1
while(True):
time.sleep(1)
x=x+1
itemconfigure(aText,text=x)
strBTN=Button(Screen,text="Start",bg="purple",font=
("Helvetica",45),command=start_timer)
strBTN.pack()
Screen.mainloop()
但第14行写着:Error:itemconfigure is 未定义。请帮帮我
现在还不清楚你到底想做什么,但是你的
start_timer
函数是一个无限繁忙的循环,它会挂起你的GUI,所以我假设这是而不是它!也许你想打电话给Tk.after
我冒昧地说你希望
itemconfigure(aText, text=x)
改变标签上的文字?您应该使用:要更改标签的文本,必须使用Label的方法
config()
。所以,代替itemconfigure(aText,text=x)
,做aText.config(text=x)
。我认为itemconfigure()
函数不存在此外,还有其他问题。例如,如果将具有无限循环的函数定义为按钮回调,则按钮将始终保持按下状态(按钮保持按下状态直到回调完成)。这就是为什么我建议您在回调结束时使用Screen的方法
after()
,并使其执行相同的函数。after()
在输入的毫秒数之后执行函数,因此Screen.after(1000, function)
将在一秒钟内暂停执行并执行函数。 还可以使用s
变量来存储秒数。当s
等于60时,它将重置为0,并在1分钟内增加(m
)。 这里有代码:这个应该能用(在我的电脑里它能正常工作)。如果你不懂什么,就问吧
相关问题 更多 >
编程相关推荐