我只是想Radiobutton
使用tkinter
. 每次用户选择具有该名称的任何内容时,我都想在其中更改/替换“选择您的浇头” Label
( )。因此,我面临的错误不是替换该标签,而是在下方创建一个新标签,即使我使用的是全局.myLabel
Radiobutton
Radiobutton
Label
from tkinter import *
root = Tk()
Toppings = [
["Pepperoni", "Pepperoni"],
["Cheese", "Cheese"],
["Mushroom", "Mushroom"],
["Onion", "Onion"]
]
pizza = StringVar()
pizza.set("Select your toppings")
for topping, value in Toppings:
Radiobutton(root, text = topping, variable = pizza, value = value). pack(anchor = W)
myLabel = Label(root, text = pizza.get())
myLabel.pack()
def clicked(value):
global myLabel
myLabel.grid_forget()
myLabel = Label(root, text = value)
myLabel.pack()
myButton = Button(root, text="CLick me!", command = lambda: clicked(pizza.get()))
myButton.pack()
root.mainloop()