I'm getting this error after clicking an operator in my Calculator GUI: TypeError: button() missing 1 required positional argument: 'number'

I'm getting this error after clicking an operator in my Calculator GUI: TypeError: button() missing 1 required positional argument: 'number'

我正在开发计算器 GUI。我最近问了同样的问题。 但由于有类似的问题,它被关闭了。 How to pass arguments to a Button command in Tkinter? 我得到的所有答案都有帮助。我能够解决我的问题。我尝试了 运行 程序,它成功了。

我能够 运行 代码没有错误,事实上,它甚至显示了 GUI。问题是当我尝试单击运算符时。

如果我单击加、减、除或乘之类的运算符,就会抛出此错误。

C:\Users\Admin\PycharmProjects\venv\Scripts\python.exe 
"C:/Users/Admin/PycharmProjects/pythonProject/Calculator GUI v1.0.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Admin\Python3.9\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\Admin\PycharmProjects\pythonProject\Calculator GUI v1.0.py", line 40, in 
<lambda>
button_addition =       Button(root_var, text="+", padx=50, pady=10, command=lambda:button())
TypeError: button() missing 1 required positional argument: 'number'

仅当我单击运算符时才会出现此错误。单击数字时没有问题。 我放了一个屏幕截图来显示发生了什么。

这是我的代码:

import tkinter as Tk
from tkinter import *

root_var = Tk()
root_var.title("Calculator")

e = Entry(root_var, width=25, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

def button(number):

e.delete(0, END)
e.insert(0, END)


button_1 = Button(root_var, text="1", padx=50, pady=10, command=lambda:button(1))
button_2 = Button(root_var, text="2", padx=50, pady=10, command=lambda:button(2))
button_3 = Button(root_var, text="3", padx=50, pady=10, command=lambda:button(3))
button_4 = Button(root_var, text="4", padx=50, pady=10, command=lambda:button(4))
button_5 = Button(root_var, text="5", padx=50, pady=10, command=lambda:button(5))
button_6 = Button(root_var, text="6", padx=50, pady=10, command=lambda:button(6))
button_7 = Button(root_var, text="7", padx=50, pady=10, command=lambda:button(7))
button_8 = Button(root_var, text="8", padx=50, pady=10, command=lambda:button(8))
button_9 = Button(root_var, text="9", padx=50, pady=10, command=lambda:button(9))
button_0 = Button(root_var, text="0", padx=50, pady=10, command=lambda:button(0))

button_addition =       Button(root_var, text="+", padx=50, pady=10, command=lambda:button())
button_subtraction =    Button(root_var, text="-", padx=50, pady=10, command=lambda:button())
button_multiplication = Button(root_var, text="*", padx=50, pady=10, command=lambda:button())
button_division =       Button(root_var, text="/", padx=50, pady=10, command=lambda:button())
button_exponents =      Button(root_var, text="^", padx=50, pady=10, command=lambda:button())

button_equals = Button(root_var, text="=", padx=50, pady=10, command=lambda:button())
button_clear =  Button(root_var, text="C", padx=50, pady=10, command=lambda:button())




button_1.grid(row=3, column=2)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=0)

button_4.grid(row=2, column=2)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=0)

button_7.grid(row=1, column=2)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=0)

button_0.grid(row=4, column=0)
button_addition.grid(row=4, column=1)
button_subtraction.grid(row=4, column=2)

button_multiplication.grid(row=5, column=0)
button_division.grid(row=5, column=1)
button_exponents.grid(row=5, column=2)

button_equals.grid(row=5, column=0)
button_clear.grid(row=5, column=1)

root_var.mainloop()

Write def 按钮(数字=None): 您的问题已解决

import tkinter as Tk
from tkinter import *

root_var = Tk()
root_var.title("Calculator")

e = Entry(root_var, width=25, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

def button(number=None):
    e.delete(0, END)
    e.insert(0, END)


button_1 = Button(root_var, text="1", padx=50, pady=10, command=lambda:button(1))
button_2 = Button(root_var, text="2", padx=50, pady=10, command=lambda:button(2))
button_3 = Button(root_var, text="3", padx=50, pady=10, command=lambda:button(3))
button_4 = Button(root_var, text="4", padx=50, pady=10, command=lambda:button(4))
button_5 = Button(root_var, text="5", padx=50, pady=10, command=lambda:button(5))
button_6 = Button(root_var, text="6", padx=50, pady=10, command=lambda:button(6))
button_7 = Button(root_var, text="7", padx=50, pady=10, command=lambda:button(7))
button_8 = Button(root_var, text="8", padx=50, pady=10, command=lambda:button(8))
button_9 = Button(root_var, text="9", padx=50, pady=10, command=lambda:button(9))
button_0 = Button(root_var, text="0", padx=50, pady=10, command=lambda:button(0))

button_addition =       Button(root_var, text="+", padx=50, pady=10, command=lambda:button())
button_subtraction =    Button(root_var, text="-", padx=50, pady=10, command=lambda:button())
button_multiplication = Button(root_var, text="*", padx=50, pady=10, command=lambda:button())
button_division =       Button(root_var, text="/", padx=50, pady=10, command=lambda:button())
button_exponents =      Button(root_var, text="^", padx=50, pady=10, command=lambda:button())

button_equals = Button(root_var, text="=", padx=50, pady=10, command=lambda:button())
button_clear =  Button(root_var, text="C", padx=50, pady=10, command=lambda:button())




button_1.grid(row=3, column=2)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=0)

button_4.grid(row=2, column=2)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=0)

button_7.grid(row=1, column=2)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=0)

button_0.grid(row=4, column=0)
button_addition.grid(row=4, column=1)
button_subtraction.grid(row=4, column=2)

button_multiplication.grid(row=5, column=0)
button_division.grid(row=5, column=1)
button_exponents.grid(row=5, column=2)

button_equals.grid(row=5, column=0)
button_clear.grid(row=5, column=1)

root_var.mainloop()