python 3.x - Updating Label text after OptionMenu selection changes -
my objective update contents of label price
, every time new item in alternative menu w
selected. code far, returning errors not sure how fix.
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 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 application variable self.contents = stringvar() # set value self.contents.set("this variable") # tell entry widget watch variable #self.w.bind('<button-1>', )
you can assume that:
oreprice = {'gold': 300, 'silver': 50, 'bronze': 10} # etc... can add together more if sense it.
i'm newbie @ python gui, hence messy and/or badly written code.
i ammended code. whenever alter ore type, cost field updated:
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 ore") # default value self.w = optionmenu(master, variable, *self.oreprice, command=self.displayprice).grid(row=0, column=1) # here application variable self.contents = stringvar() # set value self.contents.set("this variable") # tell entry widget watch variable #self.w.bind('<button-1>', ) def displayprice(self, value): self.pricevar.set(self.oreprice[value]) root = tk() app = app(root) root.mainloop()
python-3.x tkinter
No comments:
Post a Comment