如何删除和重新显示 tkinter 标签
How to delete and redisplay a tkinter label
我有一个 Dropbox,我想打印出它可以工作的选定水果选项,但我希望它删除以前显示的水果并将新选择的水果放在原处,但它只显示新的第一个
这是程序
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() 创建标签 class 的新实例。最简单的解决方案是将变量作为文本变量添加到标签中。
Display = Label(root, textvariable=clicked)
我有一个 Dropbox,我想打印出它可以工作的选定水果选项,但我希望它删除以前显示的水果并将新选择的水果放在原处,但它只显示新的第一个
这是程序
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() 创建标签 class 的新实例。最简单的解决方案是将变量作为文本变量添加到标签中。
Display = Label(root, textvariable=clicked)