如何删除和重新显示tkinter标签

问题描述

我有一个保管箱,我想打印出一个可行的选定水果选项,但是我希望它删除先前显示的水果并将新选择的水果放到该位置,但是它只是在第一个下方显示新的水果

这是程序

   fruits=["apple","mango","pear","orange"]
clicked=StringVar()
clicked.set(fruits[0])

drop=OptionMenu(root,clicked,*fruits)
drop.pack()

def Print():         
display=Label(root,text=clicked.get()).pack()

button=Button (root,text="Print",command=Print)

解决方法

当然,您每次调用tk.Label()都会创建Label类的新实例。最简单的解决方案是将变量作为文本变量添加到标签中。

Display = Label(root,textvariable=clicked)

https://stackoverflow.com/a/2603371