Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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 tkinter输入除以optionMenu中的标签输入_Python_Math_Tkinter - Fatal编程技术网

python tkinter输入除以optionMenu中的标签输入

python tkinter输入除以optionMenu中的标签输入,python,math,tkinter,Python,Math,Tkinter,我需要一点帮助。。 在下面的示例中,我有两个选项菜单、两个条目和一些标签 我要做的是,将输入内容除以标签值,从选项菜单中选择,然后在下一列中显示新值。但我现在有点卡住了,不能让它工作 from tkinter import * class App(Frame): def __init__(self, root=None): Frame.__init__(self, root) self.materialPrice = {'Brick': 70, 'Ro

我需要一点帮助。。 在下面的示例中,我有两个选项菜单、两个条目和一些标签

我要做的是,将输入内容除以标签值,从选项菜单中选择,然后在下一列中显示新值。但我现在有点卡住了,不能让它工作

from tkinter import *

class App(Frame):
    def __init__(self, root=None):
        Frame.__init__(self, root)

        self.materialPrice = {'Brick': 70, 'Rockwool': 50, 'Concrete': 20}

        materialvariable1 = StringVar(self, root)
        materialvariable1.set("Choose material")
        materialvariable2 = StringVar(self, root)
        materialvariable2.set("Choose materiale")

        self.w1 = OptionMenu(root, materialvariable1, *self.materialPrice, command=self.displayPrice).grid(row=2,
                                                                                                           column=0,
                                                                                                           columnspan=1,
                                                                                                           sticky='WE')
        self.w2 = OptionMenu(root, materialvariable2, *self.materialPrice, command=self.displayPrice2).grid(row=3,
                                                                                                            column=0,
                                                                                                            columnspan=1,
                                                                                                            sticky='WE')

        self.var = IntVar()
        self.var.set(float(0.00))
        self.var2 = IntVar()
        self.var2.set(float(0.00))

        self.entry1 = Entry(root, textvariable=self.var).grid(row=2, column=1)
        self.entry2 = Entry(root, textvariable=self.var2).grid(row=3, column=1)

        self.priceVarLabel1 = IntVar()
        self.priceVarLabel1.set(float(0.00))

        self.priceVarLabel2 = IntVar()
        self.priceVarLabel2.set(float(0.00))

        self.priceVarValue1 = Label(root, textvariable=self.priceVarLabel1, relief='sunken').grid(row=2,
                                                                                                  column=2,
                                                                                                  columnspan=1,
                                                                                                  sticky='WE')
        self.priceVarValue2 = Label(root, textvariable=self.priceVarLabel2, relief='sunken').grid(row=3,
                                                                                                  column=2,
                                                                                                  columnspan=1,
                                                                                                  sticky='WE')

        self.label1 = Label(root, textvariable=self.displayResult).grid(row=2, column=3)
        self.label2 = Label(root, textvariable=self.displayResult2).grid(row=3, column=3)

    def displayPrice(self, value):
        self.priceVarLabel1.set(self.materialPrice[value])

    def displayPrice2(self, value):
        self.priceVarLabel2.set(self.materialPrice[value])

    def displayResult(self):
        self.label1.set(self.entry1 / self.priceVarValue1)

    def displayResult2(self):
        self.label1.set(self.entry1 / self.priceVarValue1)


root = Tk()
app = App(root)
root.title("help")
root.mainloop()

只需将除法添加到您的函数中:

def displayPrice(self, value):
    self.priceVarLabel1.set(self.materialPrice[value] / self.var.get())
您可能希望将起始值更改为1,这样您就不会立即出现ZeroDivision错误

顺便说一句,初始化小部件并将其放在同一行是一个众所周知的错误源。始终使用两行

# very bad:
self.entry1 = Entry(root, textvariable=self.var).grid(row=2, column=1)

# good:
self.entry1 = Entry(root, textvariable=self.var)
self.entry1.grid(row=2, column=1)

不能让它工作意味着什么?它是否计算了错误的值?它会崩溃吗?它会出错吗?谢谢你的输入,我真的很感激!现在它工作了,就像我想要的!但是,如果我想用标签中的值除以条目中的0.200怎么办?我得到的结果是0.0。@Math那么您需要DoubleVar,而不是IntVar double是float的另一个名称。