简体   繁体   English

为什么我的按钮对Tkinter计算器不起作用?

[英]Why my buttons don't work for Tkinter Calculator?

#Calculator codes
from Tkinter import*

class Calculator():
    def __init__ (self):
        self.total = 0
        self.current = ""
        self.newnumber = True
        self.operation = ""
        self.equal = False

    def PressedNumber(self, number):
        self.equal = False
        t = text_box.get()
        n = str(number)
        if self.newnumber:
            self.current = n
            self.newnumber = False
        else:
            if n == '.':
                if n in t:
                    return
                self.current = t + n
        self.Display(self.current)

    def TotalCalculated(self):
        self.equal = True
        self.current = float(self.current)
        if self.operation_pending == True:
            self.calc_sum()
        else:
            self.total = float(text_box.get())

    def Display(self, value):
        text_box.delete(0, END)
        text_box.insert(0, value)

    def calc_sum(self):
        if self.operation == "subtract":
            self.total -= self.current
        if self.operation == "add":
            self.total += self.current
        if self.operation == "divide":
            self.total /= self.current
        if self.operation == "multiply":
            self.total *= self.current
        self.newnumber = True
        self.operation_pending = False
        self.Display(self.total)

    def operation(self, operation):
        self.current = float(self.current)
        if self.operation_pending:
            self.calc_sum()
        elif not self.equal:
            self.total = self.current
        self.newnumber = True
        self.operation_pending = True
        self.operation = operation
        self.equal = False

    def cancel(self):
        self.equal = False
        self.current = "0"
        self.Display(0)
        self.newnumber = True

    def Cancelation_forEverything(self):
        self.cancel()
        self.total = 0

    def sign(self):
        self.equal = False
        self.current = -(float(text_box.get()))
        self.Display(self.current)

summ = Calculator()
root = Tk()
Calculator = Frame(root)
Calculator.grid()

root.title("Calculator")
root.configure(bg="Khaki")
root.minsize(width=220, height=20)
root.resizable(width=FALSE, height= FALSE)
text_box = Entry(Calculator, justify=RIGHT)
text_box.grid(row = 0, column = 0, columnspan=3, pady = 8, sticky=W+E)
text_box.insert(0, "0")

Numbers = "789456123"
a = 0
bttn = []
for r in range(1,4):
    for c in range(3):
        bttn.append(Button(Calculator, text = Numbers[a], font="Candara,20"))
        bttn[a].grid(row = r, column = c, padx= 15, pady = 15)
        bttn[a]["command"] = lambda x = Numbers[a]: summ.PressedNumber(x)
        a += 1

bttn_0 = Button(Calculator, text = "     0      ", font="Candara,20")
bttn_0["command"] = lambda: summ.PressedNumber(0)
bttn_0.grid(columnspan = 5, sticky=N+W, padx= 20, pady = 20)

bttn_division = Button(Calculator, text = chr(247), font="Candara,20")
bttn_division["command"] = lambda: summ.operation("divide")
bttn_division.grid(row = 1, column = 3, pady = 10)

bttn_multiply = Button(Calculator, text = "x", font="Candara,20")
bttn_multiply["command"] = lambda: summ.operation("multiply")
bttn_multiply.grid(row = 2, column = 3, sticky=N, pady = 10)

bttn_subtract = Button(Calculator, text = "-", font="Candara,20")
bttn_subtract["command"] = lambda: summ.operation("subtract")
bttn_subtract.grid(row = 3, column = 3, pady = 10)

bttn_point = Button(Calculator, text = ".", font="Candara,20")
bttn_point["command"] = lambda: summ.PressedNumber(".")
bttn_point.grid(row = 4, column = 1, padx = 10, pady = 10)

bttn_addition = Button(Calculator, text = "+", font="Candara,20")
bttn_addition["command"] = lambda: summ.operation("add")
bttn_addition.grid(row = 4, column = 3, pady = 10)

bttn_neg = Button(Calculator, text = "+/-", font="Candara,20")
bttn_neg["command"] = summ.sign
bttn_neg.grid(row = 5, column = 0, pady = 10)

clear = Button(Calculator, text = "C", font="Candara,20")
clear["command"] = summ.cancel
clear.grid(row = 5, column = 1, pady = 10)

all_clear = Button(Calculator, text = "AC", font="Candara,20")
all_clear["command"] = summ.Cancelation_forEverything
all_clear.grid(row = 5, column = 2, pady = 10)

equals = Button(Calculator, text = "=", font="Candara,20")
equals["command"] = summ.TotalCalculated
equals.grid(row = 5, column = 3, pady = 10)

root.mainloop()

Once I add colour codes, the calculating signs don't want to work and get me an error ...Here is the error message when i press one of the calculating signs: 添加颜色代码后,计算符号不再起作用,并给我一个错误...这是当我按计算符号之一时出现的错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
  File "C:\Users\HANY\Documents\Nona CS\Calculator3.py", line 108, in <lambda>
    bttn_multiply["command"] = lambda: summ.operation("multiply")
TypeError: 'str' object is not callable

Can someone please tell me what to do in order for the codes to work? 有人可以告诉我怎么做才能使代码生效吗?

I think the problem is that in you Calculator class you define operation as a string variable: 我认为问题在于,在您的Calculator类中,您将operation定义为字符串变量:

 self.operation = ""

But then you also define it as a method: 但随后您也将其定义为方法:

def operation(self, operation)

Thus, when you summ.operation("multiply") python calls the string variable, not the method. 因此,当您summ.operation("multiply") python会调用字符串变量,而不是方法。 Which, off course, results in an error that strings are not callable. 当然,这会导致无法调用字符串的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM