Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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 再次单击按钮时出错_Python_Tkinter - Fatal编程技术网

Python 再次单击按钮时出错

Python 再次单击按钮时出错,python,tkinter,Python,Tkinter,我正在尝试制作一个G.U.I.应用程序,将每个条目的输入乘以一个特定值,然后将标签的文本设置为总计 这是我当前的代码: import Tkinter as tk # Classes class Application(tk.Frame): def __init__(self): # Create G.U.I. Framework self.root = tk.Tk() tk.Frame.__init__(self) se

我正在尝试制作一个G.U.I.应用程序,将每个条目的输入乘以一个特定值,然后将标签的文本设置为总计

这是我当前的代码:

import Tkinter as tk

# Classes
class Application(tk.Frame):
    def __init__(self):

        # Create G.U.I. Framework
        self.root = tk.Tk()
        tk.Frame.__init__(self)
        self.root.title("Job Estimator")
        self.root.geometry("280x200")

        # Create G.U.I. Widgets
        tk.Label(self.root, text="Labour:    "  + unichr(163) + "40.00 x Hours")  .grid(row=0, column=0, sticky=tk.W)
        tk.Label(self.root, text="Travel:     " + unichr(163) + "1.00   x Miles") .grid(row=1, column=0, sticky=tk.W)
        tk.Label(self.root, text="Plastic:    " + unichr(163) + "2.00   x Metres").grid(row=2, column=0, sticky=tk.W)
        tk.Label(self.root, text="Copper:  "    + unichr(163) + "3.00   x Metres").grid(row=3, column=0, sticky=tk.W)
        tk.Label(self.root, text="Chrome: "     + unichr(163) + "4.00  x Metres") .grid(row=4, column=0, sticky=tk.W)
        tk.Label(self.root, text="Total: "      + unichr(163))                    .grid(row=5, column=0, sticky=tk.W)

        self.totalLabel = tk.Label(self.root, text="0.00")
        self.totalLabel.grid(row=5, column=1, sticky=tk.W)

        self.LabourInput = tk.Entry(self.root)
        self.LabourInput.grid(row=0, column=1, sticky=tk.S)

        self.TravelInput = tk.Entry(self.root)
        self.TravelInput.grid(row=1, column=1, sticky=tk.S)

        self.PlasticInput = tk.Entry(self.root)
        self.PlasticInput.grid(row=2, column=1, sticky=tk.S)

        self.CopperInput = tk.Entry(self.root)        
        self.CopperInput.grid(row=3, column=1, sticky=tk.S)

        self.ChromeInput = tk.Entry(self.root)        
        self.ChromeInput.grid(row=4, column=1, sticky=tk.S)     

        CalculateButton = tk.Button(self.root, text="Calculate", command=lambda: calc.GrandTotal()).grid(row=6, column=0, sticky=tk.W)

class Calculator():
    def __init__(self):
        pass

    def Multiply(self, number, rate):

        NumFloat = float(number)
        RateFloat = float(rate)
        return NumFloat * RateFloat

    def GrandTotal(self): # Adds each entry field to produce and return a grand total. 

        # Set Variables

        self.LabourTotal  = self.Multiply(app.LabourInput.get(), 40)
        self.TravelTotal  = self.Multiply(app.TravelInput.get(), 1)
        self.PlasticTotal = self.Multiply(app.PlasticInput.get(), 2)
        self.CopperTotal  = self.Multiply(app.CopperInput.get(), 3)
        self.ChromeTotal  = self.Multiply(app.ChromeInput.get(), 4)

        self.GrandTotal   = self.LabourTotal + self.TravelTotal + self.PlasticTotal + self.CopperTotal + self.ChromeTotal

        return app.totalLabel.config(text=self.GrandTotal) # Return the total value.

calc = Calculator()
app = Application()
app.mainloop()
这很好,但单击“计算”按钮时除外,如果再次单击该按钮,则会收到一条错误消息:

>>> 
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\Stephen\Desktop\test.py", line 39, in <lambda>
    CalculateButton = tk.Button(self.root, text="Calculate", command=lambda: calc.GrandTotal()).grid(row=6, column=0, sticky=tk.W)
TypeError: 'float' object is not callable
>
Tkinter回调中的异常
回溯(最近一次呼叫最后一次):
文件“C:\Python27\lib\lib tk\Tkinter.py”,第1410行,在uu调用中__
返回self.func(*args)
文件“C:\Users\Stephen\Desktop\test.py”,第39行,在
CalculateButton=tk.Button(self.root,text=“Calculate”,command=lambda:calc.GrandTotal()).grid(行=6,列=0,粘性=tk.W)
TypeError:“float”对象不可调用

您正在将
计算器
类的
self.GrandTotal
属性更改为
GrandTotal
函数中的一个浮点数,在-

self.GrandTotal   = self.LabourTotal + self.TravelTotal + self.PlasticTotal + self.CopperTotal + self.ChromeTotal
因此,在执行这一行之后,
GrandTotal
方法会被一个浮点数覆盖,因此当您再次尝试单击按钮时,它会尝试调用
calc.GrandTotal()
,但
GrandTotal
现在是一个浮点数,因此会出错

您应该为浮动使用不同的名称。范例-

self.complete_total = self.LabourTotal + self.TravelTotal + self.PlasticTotal + self.CopperTotal + self.ChromeTotal

return app.totalLabel.config(text=self.complete_total) # Return the total value.

可能重复的@MorganThrapp我不认为重复的问题回答了这个问题。谢谢!这非常有效。这是个愚蠢的错误,但我完全是个新手:)