1 回答
TA贡献1789条经验 获得超8个赞
下面的代码应该做你想做的。无需破坏标签,您只需使用 .configure() 方法重新配置相同的标签即可。我怀疑你真正想要的方法是 .pack_forget() 所以我也包含了它。我没有运行这段代码,所以如果您有任何问题,请发表评论,以便我进行更正。
from tkinter import *
from pynput.keyboard import Key, Controller
import time
root = Tk()
messages = 0
label = Label(root) # create your widgets early
root.geometry('500x1400')
def startn():
global messages
global label
message = "Read the Channel"
spam = int(input('How many sentences will you send?'))
label.pack() # only need to pack it once
for num in range(0, int((spam))):
messages = int(messages + 1)
label.configure(text= messages) # you should configure instead of making new label
def stopn():
label.pack_forget() # the label reference still exists, but it is no longer packed. You can destroy the label, but I just leave it for the garbage collector.
startn()
root.after(10000, stopn) # will run stopn callback after 10 seconds
root.mainloop() # in my opinion should always be at the end.
添加回答
举报