1

I am trying to make a G.U.I. application that will multiply each entry's input by a specific value and then set a label's text to the grand total.

This is my current code:

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

This is fine except when I click the calculate button and it works, If I click it again then I receive an error message:

>>> 
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
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • @MorganThrapp I do not think that duplicate question answers this. – Anand S Kumar Sep 30 '15 at 14:58
  • Assigning `self.GrandTotal` within the `GrandTotal` method is the same kind of problem as trying to use a global variable named `str` and then call the built-in `str` again, as in the linked duplicate. – Karl Knechtel Sep 05 '22 at 07:32

1 Answers1

1

You are changing the self.GrandTotal attribute of your Calculator class to a float inside your GrandTotal function, in the line -

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

Hence, after this line is executed the method GrandTotal is overwritten with a float, and hence when you try to click on the button again, it tries to call calc.GrandTotal() but GrandTotal is now a float, and hence it errors out.

You should use a different name for the float. Example -

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.
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176