无法使用Tkinter创建计算器

时间:2019-09-14 18:59:14

标签: python tkinter

我是没有Python的新手,我们有一个创建计算器的项目。我一直在网上观看视频,因为即使这是他们第一次教这堂课,甚至教授都已经迷路了。我在网上关注一些代码,但有时会出现以下错误:

AttributeError:“ Calc”对象没有属性“ input_value”

任何想法,可能是什么问题,谢谢。这是代码片段。

calc = Frame(gui)
calc.grid()

class Calc():
    def _init_(self):
        self.total =0
        self.current =""
        self.input_value = True
        self.checkSum = False
        self.op =""
        self.result = False

    def numberEnter(self, num):
        self.result = False
        firstNumb =txtDisplay.get()
        secondNumb= str(num)
        if self.input_value:
            self.current = secondNumb
            self.input_value = False
        else:
            if secondNumb == '.':
                if secondNumb in firstNumb:
                    return
                self.current = firstNumb + secondNumb
        self.display(self.current)


    def display(self, value):
        txtDisplay.delete(0, END)
        txtDisplay.insert(0, value)


addedValue = Calc() 

txtDisplay = Entry(calc, font = ('arial',20,'bold'), bg ="light blue", bd =30, width =32, justify =RIGHT)
txtDisplay.grid(row=0, column =0, columnspan =5)
txtDisplay.insert(0,"0")

numbersCalc = "789456123"
i = 0
cnp = []
for j in range(2,5):
    for k in range(3):
        cnp.append(Button(calc, width =6, height =2,font = ('arial',20,'bold'),bd =4, text =numbersCalc[i]))
        cnp[i].grid(row =j, column = k, pady =0)
        cnp[i] ["command"] = lambda x =numbersCalc[i]: addedValue.numberEnter(x)
        i += 1


button0 = Button(calc, text ="0", width = 6, height =2, font = ('arial',20,'bold'),bd =4, background ="light blue",command = lambda: addedValue.numberEnter(0))
button0.grid(row =  5, column = 0)

2 个答案:

答案 0 :(得分:0)

您已经写过_init_(在课程中一个下划线),但是应该是__init__

将您的代码更改为def __init__(self):

答案 1 :(得分:0)

您需要在以下内容中减少最后一行的缩进量:

    else:
        if secondNumb == '.':
            if secondNumb in firstNumb:
                return
        self.current = firstNumb + secondNumb # <= this line
            # from here
        # to here is a decrease in indent 

允许它附加其他数字。