from tkinter import *
import time
root = Tk()
label = Label(root, text="enter Something below")
label.pack()
textbox = Entry(root)
textbox.pack()
label2 = Label(root, text = textbox.get())
def save():
if label2.winfo_exists == 1:
label2.pack_forget()
label2.pack()
else:
label2.pack_forget()
label2.pack()
button = Button(root, text="press for repetition", command=save)
button.pack()
root.mainloop()
当我第二次按下该按钮时,什么也不会删除,所以我想做的就是删除已经用同一按钮打包的内容。所以,如果我输入Hello,那么你好吗?
结果如下:
Enter Something below
(this is the textbox)
Hello
How are you?
我希望删除Hello
,并且只显示How are you?
。
答案 0 :(得分:0)
您不需要销毁标签并重新创建标签。只需创建一次标签,然后在save()
函数中更新其文本:
from tkinter import *
root = Tk()
label = Label(root, text="enter Something below")
label.pack()
textbox = Entry(root)
textbox.pack()
label2 = Label(root) # create the hidden label
def save():
# update the label text
label2.config(text=textbox.get())
# show the label. remove 'before=button' to show it below the button
label2.pack(before=button)
button = Button(root, text="press for repetition", command=save)
button.pack()
root.mainloop()