我的目标是更新标签的内容price
,每次选项菜单中出现新项目时w
被选中。到目前为止,这是我的代码,但它返回了我不知道如何修复的错误。
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
Label(master, text="Ore:").grid(row=0)
Label(master, text="Price:").grid(row=1)
self.price = Label(master, text="0.00").grid(row=1, column=1)
variable = StringVar(master)
variable.set("Select an ore") # default value
def displayPrice(self):
self.price = orePrice[self.w.get()]
self.w = OptionMenu(master, variable, *orePrice, command=displayPrice).grid(row=0, column=1)
# here is the application variable
self.contents = StringVar()
# set it to some value
self.contents.set("this is a variable")
# tell the entry widget to watch this variable
#self.w.bind('<Button-1>', )
您可以假设:
orePrice = {'Gold': 300, 'Silver': 50, 'Bronze': 10} # etc... you can add more if you feel like it.
我是 Python GUI 的新手,因此代码很混乱和/或写得很糟糕。
我修改了你的代码。现在,每当您更改矿石类型时,价格字段都会更新:
from tkinter import *
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
Label(master, text="Ore:").grid(row=0)
Label(master, text="Price:").grid(row=1)
self.priceVar = StringVar()
self.priceVar.set("0.00")
self.price = Label(master, textvariable=self.priceVar).grid(row=1, column=1)
self.orePrice = {'Gold': 300, 'Silver': 50, 'Bronze': 10}
variable = StringVar(master)
variable.set("Select an ore") # default value
self.w = OptionMenu(master, variable, *self.orePrice, command=self.displayPrice).grid(row=0, column=1)
# here is the application variable
self.contents = StringVar()
# set it to some value
self.contents.set("this is a variable")
# tell the entry widget to watch this variable
#self.w.bind('<Button-1>', )
def displayPrice(self, value):
self.priceVar.set(self.orePrice[value])
root = Tk()
app = App(root)
root.mainloop()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)