Python Forum

Full Version: Type error:takes 1 positional argument but 2 required
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
From what i found online, below error should be satisfied if the called method has self included as an argument. To me it appears i have that part satisfied but i still get the error. calling the on_button method from btnAccept.bind(on_press=self.on_button)

Error:
Exception has occurred: TypeError (note: full exception trace is shown but execution is paused at: <module>) on_button() takes 1 positional argument but 2 were given File "/home/mark/Python Environments/FirstEnvProject/ClassTest6.py", line 107, in <module> (Current frame) CreateApp().run()
class MyApp(TabbedPanel):
    def __init__(self,data_base):
        super(MyApp, self).__init__()
      ...
            if str(i)=='Add':
                myTPHM.content=self.enterScreen()
        
       

    def enterScreen(self):    
        stkEnter=StackLayout(orientation='lr-tb',spacing=4)
        lbl1=Label(text='Tag No',size_hint=(.1,.04))
        lbl2=Label(text='Location',size_hint=(.1,.04))
        ...
        stkEnter.add_widget(btnAccept)

        btnAccept.bind(on_press=self.on_button)
        return stkEnter

    def on_button(self):
        self.db.insert_cattledata(self.txtTag.text,self.txtLoc.text,self.txtSex.text,self.txtType.text)
        self.txtTag.text=''
        self.txtLoc.text=''
        self.txtSex.text=''
        self.txtType.text=''
        
...
In your previous thread, in my post https://python-forum.io/thread-35460-pos...#pid149514 I assumed that a button event passes no attributes but obviously, it does.

The docs show a value parameter https://kivy.org/doc/stable/api-kivy.uix.button.html
Resolved! def on_button(self,instance): worked. Thanks again for the direction. Honestly, I did not think to search for this error resolution in Kivy. I will try to broaden my searches.
Whenever I write a callback function and I am not positive about the arguments I start with this:
def callback_function(*args, **kwargs):
    print(args, kwargs)