Home > Net >  TypeError: btn_add() missing 1 required positional argument: 'first_number'
TypeError: btn_add() missing 1 required positional argument: 'first_number'

Time:11-20

I'm making a calculator in Python using Tkinter, and I'm getting an error im not sure as to why im running into this error but ive legit tried retyping the whole code and cant find anything about it on yt:

`

from tkinter import *

w = Tk()

w.title("Simple Calculator")

ent = Entry()
ent.grid(row=0,column=0,columnspan=3,padx=10,pady=10 )




def button_click(number):
    current = ent.get()
    ent.delete(0,END)
    ent.insert(0,str(current) str(number))
 
def button_clear():
    ent.delete(0, END)

def button_add(first_number):
    first_number = ent.get()
    global f_num
    f_num = int(first_number)
    ent.delete(END)
    
    

# Defining Button

button_1 = Button(w,text="1",padx=40,pady=20,command=lambda:button_click(1))
button_2 = Button(w,text="2",padx = 40,pady = 20,command=lambda:button_click(2))
button_3 = Button(w,text="3",padx = 40,pady = 20,command=lambda:button_click(3))
button_4 = Button(w,text="4",padx = 40,pady = 20,command=lambda:button_click(4))
button_5 = Button(w,text="5",padx = 40,pady = 20,command=lambda:button_click(5))
button_6 = Button(w,text="6",padx = 40,pady = 20,command=lambda:button_click(6))
button_7 = Button(w,text="7",padx = 40,pady = 20,command=lambda:button_click(7))
button_8 = Button(w,text="8",padx = 40,pady = 20,command=lambda:button_click(8))
button_9 = Button(w,text="9",padx = 40,pady = 20,command=lambda:button_click(9))
button_0 = Button(w,text="0",padx = 40,pady = 20,command=lambda:button_click(0))
button_add = Button(w,text=" ",padx=39,pady=20,command=button_add)
button_equal = Button(w,text="=",padx = 91,pady = 20,command=button_click)
button_clear = Button(w,text="CLEAR",padx = 79,pady = 20,command=button_clear)

# Putting button on screen

button_1.grid(row=3,column=0 )
button_2.grid(row=3,column= 1)
button_3.grid(row=3,column= 2)
button_4.grid(row=2,column= 0)
button_5.grid(row=2,column= 1)
button_6.grid(row=2,column= 2)
button_7.grid(row=1,column= 0)
button_8.grid(row=1,column= 1)
button_9.grid(row=1,column= 2)
button_0.grid(row=4,column= 0)
button_clear.grid(row=4,column=1,columnspan=2)
button_add.grid(row=5,column=0)
button_equal.grid(row=5,column=1,columnspan=2)



w.mainloop()

`

i tried everything to fix this error P.S. I don't actually know which line the error is on, because it's saying that the error is on line 1705, even though the code is only 101 lines

CodePudding user response:

The error is where you inherit the Button class to button_add specifically in command=button_add You have to add in 'first_number' parameter to the command parameter

CodePudding user response:

def button_add():
first_number = ent.get()
global f_num
f_num = int(first_number)
ent.delete(END)
  • Related