TypeError: button() missing 1 required positional argument: 'number'
TypeError: button() missing 1 required positional argument: 'number'
我正在为我的计算器项目开发 GUI。我似乎无法弄清楚为什么我不断收到此错误。我看到了 2 个与我正在做的类似的问题,但它们没有帮助。这些是我 saw:1) 2) 的问题
None 这些问题帮助了我。
顺便说一句,这是我的错误:
C:\Users\Admin\PycharmProjects\venv\Scripts\python.exe
"C:/Users/Admin/PycharmProjects/pythonProject/Calculator GUI v1.0.py"
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\pythonProject\Calculator GUI v1.0.py", line 40, in
<module>
button_addition = Button(root_var, text="+", padx=50, pady=10, command=button())
TypeError: button() missing 1 required positional argument: 'number'
Process finished with exit code 1
还有我的代码:
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=button())
button_subtraction = Button(root_var, text="-", padx=50, pady=10, command=button())
button_multiplication = Button(root_var, text="*", padx=50, pady=10, command=button())
button_division = Button(root_var, text="/", padx=50, pady=10, command=button())
button_exponents = Button(root_var, text="^", padx=50, pady=10, command=button())
button_equals = Button(root_var, text="=", padx=50, pady=10, command=button())
button_clear = Button(root_var, text="C", padx=50, pady=10, command=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()
(我怀疑这会有帮助,但我正在使用 PyCharm)
您提供的代码没有那么缩进,但无论如何
从第 27 行到第 34 行:你有这个代码
button_addition = Button(root_var, text="+", padx=50, pady=10, command=button())
button_subtraction = Button(root_var, text="-", padx=50, pady=10, command=button())
button_multiplication = Button(root_var, text="*", padx=50, pady=10, command=button())
button_division = Button(root_var, text="/", padx=50, pady=10, command=button())
button_exponents = Button(root_var, text="^", padx=50, pady=10, command=button())
button_equals = Button(root_var, text="=", padx=50, pady=10, command=button())
button_clear = Button(root_var, text="C", padx=50, pady=10, command=button())
出现错误是因为在传递 command=button()
时您没有传递任何 args
将 args 传递给 button function
,你应该可以开始了
我正在为我的计算器项目开发 GUI。我似乎无法弄清楚为什么我不断收到此错误。我看到了 2 个与我正在做的类似的问题,但它们没有帮助。这些是我 saw:1)
C:\Users\Admin\PycharmProjects\venv\Scripts\python.exe
"C:/Users/Admin/PycharmProjects/pythonProject/Calculator GUI v1.0.py"
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\pythonProject\Calculator GUI v1.0.py", line 40, in
<module>
button_addition = Button(root_var, text="+", padx=50, pady=10, command=button())
TypeError: button() missing 1 required positional argument: 'number'
Process finished with exit code 1
还有我的代码:
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=button())
button_subtraction = Button(root_var, text="-", padx=50, pady=10, command=button())
button_multiplication = Button(root_var, text="*", padx=50, pady=10, command=button())
button_division = Button(root_var, text="/", padx=50, pady=10, command=button())
button_exponents = Button(root_var, text="^", padx=50, pady=10, command=button())
button_equals = Button(root_var, text="=", padx=50, pady=10, command=button())
button_clear = Button(root_var, text="C", padx=50, pady=10, command=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()
(我怀疑这会有帮助,但我正在使用 PyCharm)
您提供的代码没有那么缩进,但无论如何
从第 27 行到第 34 行:你有这个代码
button_addition = Button(root_var, text="+", padx=50, pady=10, command=button())
button_subtraction = Button(root_var, text="-", padx=50, pady=10, command=button())
button_multiplication = Button(root_var, text="*", padx=50, pady=10, command=button())
button_division = Button(root_var, text="/", padx=50, pady=10, command=button())
button_exponents = Button(root_var, text="^", padx=50, pady=10, command=button())
button_equals = Button(root_var, text="=", padx=50, pady=10, command=button())
button_clear = Button(root_var, text="C", padx=50, pady=10, command=button())
出现错误是因为在传递 command=button()
时您没有传递任何 args
将 args 传递给 button function
,你应该可以开始了