猿问

如何删除标签 Python

我的问题是我想要一个跟踪器来跟踪一个句子发送了多少次以及我何时运行:


from tkinter import *

from pynput.keyboard import Key, Controller

import time


root = Tk()

messages = 0

root.geometry('500x1400')


def startn():

    global messages

    global label

    message = "Read the Channel"

    spam = int(input('How many sentences will you send?'))

    for num in range(0, int((spam))):

        messages = int(messages + 1)

        label = Label(root, text= messages)

        label.pack()

    root.mainloop()



            

            

    

    

startn()

每当我在 label.pack() 之后添加 .destroy 时,它都不会显示任何内容(顺便说一句,spam = 5)


在 label.pack() 之后输出带 .destroy 输出不带 .destroy


弑天下
浏览 153回答 1
1回答

拉丁的传说

下面的代码应该做你想做的。无需破坏标签,您只需使用 .configure() 方法重新配置相同的标签即可。我怀疑你真正想要的方法是 .pack_forget() 所以我也包含了它。我没有运行这段代码,所以如果您有任何问题,请发表评论,以便我进行更正。from tkinter import *from pynput.keyboard import Key, Controllerimport timeroot = Tk()messages = 0label = Label(root) # create your widgets earlyroot.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 labeldef 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 secondsroot.mainloop() # in my opinion should always be at the end.
随时随地看视频慕课网APP

相关分类

Python
我要回答