Python GUI 计算器退格和清除

Python GUI Calculator backspace and clear

我正在使用 tkinter 制作一个简单的计算器。除了我的退格键和清除 buttons.The 错误消息外,我已经设法让所有东西正常工作:'TypeError: backspace() takes 0 positional arguments but 1 was given' 用于退格键,'TypeError: clear() takes 0 positional arguments but 1 was given' 用于清除。我查看了在线教程,但我发现的大多数教程似乎都没有这个问题。

from tkinter import *
root = Tk()
root.geometry("275x300")
root.title("Calculator")

def calculatorTitle():
    labelTitle=Label(root, text="Calculator", bg = "black", fg = "white")
    labelTitle.config(font = ("Verdana", 12, "bold"))
    labelTitle.grid(row=0, column=0, columnspan=4, padx=55, pady=5)

e=Entry(root,width=20,font="Arial 12",justify='right', bg='yellow', fg = 'blue')
e.grid(row=1,column=0,columnspan=4, pady = 5)

def addEntry(ch):
    e.insert(20, ch)

def CalculateEntry(expression):
    expression = e.get()
    expression = eval(expression)
    e.delete(0, END)
    e.insert(20, expression)

def clear():
    e.delete(0, END)
    return

def backspace():
    current = e.get()
    lenght = len(current)-1
    e.delete(lenght, END)


def calculatorBoard():
    b1=Button(root,text='1',width=5,command=lambda:addEntry(1))
    b2=Button(root,text='2',width=5,command=lambda:addEntry(2))
    b3=Button(root,text='3',width=5,command=lambda:addEntry(3))
    bAddition=Button(root,text='+',width=5,command=lambda:addEntry('+'))
    b4=Button(root,text='1',width=5,command=lambda:addEntry(4))
    b5=Button(root,text='5',width=5,command=lambda:addEntry(5))
    b6=Button(root,text='6',width=5,command=lambda:addEntry(6))
    bSubtract=Button(root,text='-',width=5,command=lambda:addEntry('-'))
    b7=Button(root,text='7',width=5,command=lambda:addEntry(7))
    b8=Button(root,text='8',width=5,command=lambda:addEntry(8))
    b9=Button(root,text='9',width=5,command=lambda:addEntry(9))
    bMultiply=Button(root,text='*',width=5,command=lambda:addEntry('*'))
    bClear=Button(root,text='CE',width=5,command=lambda:clear('CE'))
    b0=Button(root,text='0',width=5,command=lambda:addEntry(0))
    bBackspace=Button(root,text='<-',width=5,command=lambda:backspace('<-'))
    bDivide=Button(root,text='/',width=5,command=lambda:addEntry('/'))
    bEqual=Button(root,text='=',width=27,command=lambda:CalculateEntry('='))
    
    b1.grid(row=2,column=0,pady = 10 )
    b2.grid(row=2,column=1,pady = 10 )
    b3.grid(row=2,column=2,pady = 10 )
    bAddition.grid(row=2,column=3,pady = 10 )

    b4.grid(row=3,column=0,pady = 10 )
    b5.grid(row=3,column=1,pady = 10 )
    b6.grid(row=3,column=2,pady = 10 )
    bSubtract.grid(row=3,column=3,pady = 10 )

    b7.grid(row=4,column=0,pady = 10 )
    b8.grid(row=4,column=1,pady = 10 )
    b9.grid(row=4,column=2,pady = 10 )
    bMultiply.grid(row=4,column=3,pady = 10 )

    bClear.grid(row=5,column=0,pady = 10 )
    b0.grid(row=5,column=1,pady = 10 )
    bBackspace.grid(row=5,column=2,pady = 10 )
    bDivide.grid(row=5,column=3,pady = 10 )

    bEqual.grid(row= 6, column= 0, columnspan= 4, pady= 10)

calculatorTitle()
calculatorBoard()
root.mainloop()

编辑:这部分是我必须遵循一些说明的作业。

def clear()def backspace():您用零参数定义函数。

clear('CE')backspace('<-'):你用一个参数调用函数

要解决此问题,要么重新定义函数以获取参数,

def clear(arg):
    # do whatever

def backspace(arg):
    # do whatever

或将调用更改为不传递任何内容。

bClear=Button(root,text='CE',width=5,command=clear)
   
bBackspace=Button(root,text='<-',width=5,command=backspace)

一定不要两者都做 -- 你会 运行 陷入相反的问题。