python - Error when clicking the button again -
i trying make g.u.i. application multiply each entry's input specific value , set label's text grand total.
this current code:
import tkinter 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 produce , return 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 total value. calc = calculator() app = application() app.mainloop()
this fine except when click calculate button , works, if click again receive 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 not callable
you changing self.grandtotal
attribute of calculator
class float inside grandtotal
function, in line -
self.grandtotal = self.labourtotal + self.traveltotal + self.plastictotal + self.coppertotal + self.chrometotal
hence, after line executed method grandtotal
overwritten float, , hence when try click on button again, tries call calc.grandtotal()
grandtotal
float, , hence errors out.
you should use different name float. example -
self.complete_total = self.labourtotal + self.traveltotal + self.plastictotal + self.coppertotal + self.chrometotal return app.totallabel.config(text=self.complete_total) # return total value.
Comments
Post a Comment