如何解决此Python计算器错误?

时间:2020-09-27 18:21:23

标签: python python-3.x tkinter calculator

非常感谢您的帮助。我的计算器需要帮助。谢谢 这是错误: Tkinter回调中的异常 追溯(最近一次通话): 在调用中的第1883行,文件“ C:\ Users \ Owner \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ tkinter_ init _。py” 返回self.func(* args) 文件“ C:/用户/所有者/文档/ Benjamin Stuff / BENS CALCULATOR.py”,第42行,在 lambda e,s = self,storeObj = display:s.calc(storeObj),'+') AttributeError:“ app”对象没有属性“ calc”

代码:

from tkinter import*

def iCalc (source, side):
    storeObj = Frame(source, borderwidth = 1, bd= 4, bg="sky blue")
    storeObj.pack(side=side, expand=YES, fill=BOTH)
    return storeObj

def button (source, side, text, command=None):
    storeObj = Button(source, text=text, command=command)
    storeObj.pack(side=side, expand=YES, fill=BOTH)
    return storeObj
class app(Frame):
    
        def __init__(self):
            Frame.__init__(self)
            self.option_add('*font', 'Comic 20 bold')
            self.pack(expand=YES, fill=BOTH)
            self.master.title('Bens Calculator')
        
            display = StringVar()
            Entry(self, relief=FLAT,
                  textvariable=display,justify='right',bd=30,bg='sky blue').pack(side=TOP, expand=YES,
                          fill=BOTH)
        
            for clearBut in (["CE"], ["C"]):
                erase = iCalc(self,TOP)
                for ichar in clearBut:
                    button(erase, LEFT, ichar,
                           lambda storeObj=display, q=ichar: storeObj.set(''))
        
            for NumBut in ("789%", "456x", "123+", "0.+"):
                FunctionNum = iCalc(self, TOP)
                for char in NumBut:
                    button(FunctionNum, LEFT, char,
                       lambda storeObj=display, q=char: storeObj.set(storeObj.get() + q))
        
            EqualsButton = iCalc(self, TOP)
            for iEquals in '=':
                if iEquals == '=':
                    btniEquals = button(EqualsButton, LEFT, iEquals)
                    btniEquals.bind('<ButtonRelease-1>',
                                    lambda e, s=self, storeObj=display: s.calc(storeObj), '+')
        
                else:
                    btniEquals = button(EqualsButton, LEFT, iEquals,
                       lambda storeObj=display, s='  %s  '%iEquals: storeObj.set(storeObj.get()+s))


if __name__ == '__main__':
    app().mainloop()

def calc(self, display):
     try:
        display.set(eval(display.get()))
     except:
        display.set("ERROR")

如果有帮助,我正在制造一个计算器。当我按下等于按钮时,就会发生这种情况。任何帮助都将是惊人的。谢谢你祝你有美好的一天。

1 个答案:

答案 0 :(得分:1)

该错误表明类“ app”的对象没有方法或属性“ calc”。您需要在app类中创建一个名称为“ calc”的方法。

from tkinter import*

def iCalc (source, side):
    storeObj = Frame(source, borderwidth = 1, bd= 4, bg="sky blue")
    storeObj.pack(side=side, expand=YES, fill=BOTH)
    return storeObj

def button (source, side, text, command=None):
    storeObj = Button(source, text=text, command=command)
    storeObj.pack(side=side, expand=YES, fill=BOTH)
    return storeObj

class app(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.option_add('*font', 'Comic 20 bold')
        self.pack(expand=YES, fill=BOTH)
        self.master.title('Bens Calculator')
    
        display = StringVar()
        Entry(self, relief=FLAT,
              textvariable=display,justify='right',bd=30,bg='sky blue').pack(side=TOP, expand=YES,
                      fill=BOTH)
    
        for clearBut in (["CE"], ["C"]):
            erase = iCalc(self,TOP)
            for ichar in clearBut:
                button(erase, LEFT, ichar,
                       lambda storeObj=display, q=ichar: storeObj.set(''))
    
        for NumBut in ("789%", "456x", "123+", "0.+"):
            FunctionNum = iCalc(self, TOP)
            for char in NumBut:
                button(FunctionNum, LEFT, char,
                   lambda storeObj=display, q=char: storeObj.set(storeObj.get() + q))
    
        EqualsButton = iCalc(self, TOP)
        for iEquals in '=':
            if iEquals == '=':
                btniEquals = button(EqualsButton, LEFT, iEquals)
                btniEquals.bind('<ButtonRelease-1>',
                                lambda e, s=self, storeObj=display: s.calc(storeObj), '+')
    
            else:
                btniEquals = button(EqualsButton, LEFT, iEquals,
                   lambda storeObj=display, s='  %s  '%iEquals: storeObj.set(storeObj.get()+s))
    
    def calc(self, display):
        try:
            display.set(eval(display.get()))
        except:
            display.set("ERROR")

if __name__=='__main__':
    app().mainloop()