Button¶
The Button is a Label with associated actions
that are triggered when the button is pressed (or released after a
click/touch). To configure the button, the same properties (padding,
font_size, etc) and
sizing system
are used as for the Label class:
button = Button(text='Hello world', font_size=14)
To attach a callback when the button is pressed (clicked/touched), use
bind:
def callback(instance, touch):
print('The button <%s> is being pressed' % instance.text)
btn1 = Button(text='Hello world 1')
btn1.bind(on_press=callback)
btn2 = Button(text='Hello world 2')
btn2.bind(on_press=callback)
If you want to be notified every time the button pressed state changes, you can bind
to the Button.pressed property:
def callback(instance, value):
print('My button <%s> pressed state is <%s>' % (instance, value))
btn1 = Button(text='Hello world 1')
btn1.bind(pressed=callback)
Kv Example:
Button:
text: 'press me'
on_press:
print("ouch! More gently please", args[1].pos)
on_release:
print("ahhh", args[1].pos)
on_pressed:
print("my current pressed state is {}".format(self.pressed))
- class kivy.uix.button.Button(**kwargs)¶
Bases:
kivy.uix.behaviors.button.ButtonBehavior,kivy.uix.label.LabelButton class, see module documentation for more information.
Changed in version 1.8.0: The behavior / logic of the button has been moved to
ButtonBehaviors.