Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在选项菜单选择更改后更新标签文本_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x 在选项菜单选择更改后更新标签文本

Python 3.x 在选项菜单选择更改后更新标签文本,python-3.x,tkinter,Python 3.x,Tkinter,我的目标是在每次选择选项菜单w中的新项目时更新标签price的内容。到目前为止,这是我的代码,但它返回的错误我不知道如何修复 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=

我的目标是在每次选择选项菜单
w
中的新项目时更新标签
price
的内容。到目前为止,这是我的代码,但它返回的错误我不知道如何修复

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>', )

我是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()  
从tkinter导入*
类应用程序(框架):
def uuu init uuu(self,master=None):
帧。\uuuu初始化(自,主)
标签(master,text=“Ore:”).grid(行=0)
标签(主标签,text=“Price:”).grid(行=1)
self.priceVar=StringVar()
自定价变量集(“0.00”)
self.price=Label(master,textvariable=self.priceVar).grid(行=1,列=1)
self.orePrice={'Gold':300,'Silver':50,'brown':10}
变量=StringVar(主)
变量.set(“选择矿石”)#默认值
self.w=OptionMenu(master,variable,*self.orePrice,command=self.displayPrice).grid(行=0,列=1)
#下面是应用程序变量
self.contents=StringVar()
#将它设置为某个值
self.contents.set(“这是一个变量”)
#告诉条目小部件监视此变量
#self.w.bind(“”,)
def显示价格(自身、价值):
self.priceVar.set(self.orePrice[value])
root=Tk()
app=app(根目录)
root.mainloop()
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()