python tkinter条目输入除以optionMenu的标签输入

时间:2018-05-22 16:36:04

标签: python math tkinter

我需要一点帮助.. 在下面的示例中,我有两个optionsMenus,两个条目和一些标签。

我尝试做的是将输入从条目中除以标签值,从optionsMenu中选择,然后在下一列中显示新值。但我现在有点卡住了,无法让它发挥作用。

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()

1 个答案:

答案 0 :(得分:1)

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

[TestClass]
public class TestClass1
{
    [TestMethod]
    public void Test1()
    {
        LoadDLL(1);
        Assert.AreEqual(1, ReadDLL());
    }

    [TestMethod]
    public void Test2()
    {
        Assert.AreEqual(1, ReadDLL());
    }
}

您可能希望将起始值更改为1,这样您就不会立即获得ZeroDivisionError。

BTW,初始化一个小部件并将其放在同一行上是一个众所周知的bug源。总是使用2行。

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