Bind function to Kivy button
I don't think any of the answers are very clear. Neither explains that problem is that the callback given to on_press
gets called with a parameter, the instance of button, so LoginScreen.auth
must accept a parameter after the self
:
def auth(self, button):
print('button pressed:', instance)
The problem is not that on_press
must be given via Button.bind
or that the callback must be a function, it can be a bound method, and the docs cited by other answer and by comments link to ButtonbBhavior
which indicates that OP use of on_press
in constructor was fine:
self.hello = Button(text="hello", on_press=self.auth)
would have worked if auth
had been as described above.
If you read the Button documentation, the key seems to be to use the bind
function:
def callback(instance):
print('The button <%s> is being pressed' % instance.text)
btn1 = Button(text='Hello world 1')
btn1.bind(on_press=callback)