下面是我尝试使用Python 3的图形计算器。所有数字,取消和等于按钮都可以正常工作。我遇到的问题是,当' +'或者' - '按下按钮,我收到以下错误信息:" TypeError:' bool'对象不可调用。"如果有人能够对这个错误消息所指的内容有所了解,我将不胜感激。
from tkinter import *
class functions():
def __init__(self):
self.cumulative = 0
self.input = 0;
self.new_value = True
self.calculation = False
self.operation = ""
self.operation = False
def numpress(self, num):
self.number_equals = False
nostring = display_screen.get()
no2 = str(num)
if self.new_value:
self.input = no2
self.new_value = False
else:
self.input = nostring + no2
self.display(self.input)
def calc_total(self):
self.number_equals = True
self.input = int(self.input)
if self.calculation == True:
self.addition_calc()
else:
self.total = int(display_screen.get())
def display(self, value):
display_screen.delete(0, END)
display_screen.insert(0, value)
def addition_calc(self):
if self.operation == "addition":
self.total += self.input
if self.operation == "subtraction":
self.cumulative -= self.input
self.new_value = True
self.calculation = False
self.display(self.total)
def operation(self, operation):
self.input = int(self.input)
if self.calculation:
self.calc_total()
elif not self.number_equals:
self.total = self.input
self.new_value = True
self.calculation = True
self.operation = operation
self.number_equals = False
def cancel(self):
self.number_equals = False
self.input = "0"
self.display(0)
self.new_value = True
self.total = 0
ops = functions()
root = Tk()
root.resizable(0,0)
frame = Frame(root)
frame.pack()
root.title("Calculator")
screen_num=StringVar()
bgcolour="#000"
fgcolour="#00FF00"
display_screen=Entry (frame, bd=10, insertwidth=1, font=30)
display_screen.pack( side =TOP)
topframe = Frame(root)
topframe.pack(side = TOP)
b1 = Button(topframe,padx=24, pady=8, bd =6, text = "1", fg="black",)
b1.pack(side = LEFT)
b1["command"] = lambda: ops.numpress(1)
b2 = Button(topframe,padx=24, pady=8, bd =6, text = "2", fg="black",)
b2.pack(side = LEFT)
b2["command"] = lambda: ops.numpress(2)
b3 = Button(topframe,padx=24, pady=8, bd =6, text = "3", fg="black",)
b3.pack(side = LEFT)
b3["command"] = lambda: ops.numpress(3)
frame1 = Frame(root)
frame1.pack(side = TOP)
b4 = Button(frame1,padx=24, pady=8, bd =6, text = "4", fg="black",)
b4.pack(side = LEFT)
b4["command"] = lambda: ops.numpress(4)
b5 = Button(frame1,padx=24, pady=8, bd =6, text = "5", fg="black",)
b5.pack(side = LEFT)
b5["command"] = lambda: ops.numpress(5)
b6 = Button(frame1,padx=24, pady=8, bd =6, text = "6", fg="black",)
b6.pack(side = LEFT)
b6["command"] = lambda: ops.numpress(6)
frame2 = Frame(root)
frame2.pack(side = TOP)
b7 = Button(frame2,padx=24, pady=8, bd =6, text = "7", fg="black")
b7.pack(side = LEFT)
b7["command"] = lambda: ops.numpress(7)
b8 = Button(frame2,padx=24, pady=8, bd =6, text = "8", fg="black")
b8.pack(side = LEFT)
b8["command"] = lambda: ops.numpress(8)
b9 = Button(frame2,padx=24, pady=8, bd =6, text = "9", fg="black")
b9.pack(side = LEFT)
b9["command"] = lambda: ops.numpress(9)
frame3=Frame(root)
frame3.pack(side=TOP)
button0=Button(frame3,padx=24, pady=8, bd=6, text="0", fg="black")
button0.pack(side=LEFT)
button0["command"] = lambda: ops.numpress(0)
command=Frame(root)
command.pack(side=TOP)
buttonPlus=Button(command,padx=24, pady=8, bd =6, text = "+", fg="black")
buttonPlus.pack(side=LEFT)
buttonPlus["command"] = lambda: ops.operation("addition")
buttonMinus=Button(command,padx=24, pady=8, bd =6, text = "-", fg="black")
buttonMinus.pack(side=LEFT)
buttonMinus["command"] = lambda: ops.operation("subtraction")
buttonCancel=Button(command,padx=24, pady=8, bd =6, text = "C", fg="black")
buttonCancel.pack(side=LEFT)
buttonCancel["command"] = ops.cancel
command1=Frame(root)
command1.pack(side=TOP)
buttonEquals=Button(command1,padx=24, pady=8, bd =6, text = "=", fg="black")
buttonEquals.pack(side=LEFT)
buttonEquals["command"] = ops.calc_total
root.mainloop()
答案 0 :(得分:2)
在functions
类中,您同时拥有一个名为operation
的方法和实例变量。当您的回调尝试通过ops.operation
调用该方法时,它会获取实例变量,该变量在__init__
运行后将为False
。实例变量" shadow"类变量(如方法)。
要解决此问题,请更改方法或变量的名称。然后你可以毫不含糊地参考每一个。