Button¶
Buttons allow users to take actions, and make choices, with a single tap.
KivyMD provides the following button classes for use:
MDIconButton¶
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
Screen:
MDIconButton:
icon: "language-python"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
Example().run()
The icon parameter must have the name of the icon
from kivymd/icon_definitions.py file.
You can also use custom icons:
MDIconButton:
icon: "data/logo/kivy-icon-256.png"
By default, MDIconButton button has a size (dp(48), dp (48)).
Use user_font_size attribute to resize the button:
MDIconButton:
icon: "android"
user_font_size: "64sp"
By default, the color of MDIconButton
(depending on the style of the application) is black or white.
You can change the color of MDIconButton as the text color
of MDLabel:
MDIconButton:
icon: "android"
theme_text_color: "Custom"
text_color: app.theme_cls.primary_color
MDFloatingActionButton¶
The above parameters for MDIconButton apply
to MDFloatingActionButton.
To change MDFloatingActionButton background, use the
md_bg_color parameter:
MDFloatingActionButton:
icon: "android"
md_bg_color: app.theme_cls.primary_color
The length of the shadow is controlled by the elevation_normal parameter:
MDFloatingActionButton:
icon: "android"
elevation_normal: 12
MDFlatButton¶
To change the text color of: class:~MDFlatButton use the text_color parameter:
MDFlatButton:
text: "MDFLATBUTTON"
text_color: 0, 0, 1, 1
Or use markup:
MDFlatButton:
text: "[color=#00ffcc]MDFLATBUTTON[/color]"
markup: True
To specify the font size and font name, use the parameters as in the usual Kivy buttons:
MDFlatButton:
text: "MDFLATBUTTON"
font_size: "18sp"
font_name: "path/to/font"
Warning
You cannot use the size_hint_x parameter for KivyMD buttons
(the width of the buttons is set automatically)!
However, if there is a need to increase the width of the button,
you can use the parameter increment_width:
MDFlatButton:
text: "MDFLATBUTTON"
increment_width: "164dp"
MDRaisedButton¶
This button is similar to the MDFlatButton button except that you
can set the background color for MDRaisedButton:
MDRaisedButton:
text: "MDRAISEDBUTTON"
md_bg_color: 1, 0, 1, 1
MDRectangleFlatButton¶
Button parameters MDRectangleFlatButton are the same as
button MDRaisedButton:
MDRectangleFlatButton:
text: "MDRECTANGLEFLATBUTTON"
text_color: 0, 0, 1, 1
md_bg_color: 1, 1, 0, 1
Note
Note that the frame color will be the same as the text color.
MDRectangleFlatIconButton¶
Button parameters MDRectangleFlatButton are the same as
button MDRectangleFlatButton:
MDRectangleFlatIconButton:
icon: "android"
text: "MDRECTANGLEFLATICONBUTTON"
width: dp(280)
Warning
MDRectangleFlatButton does not stretch to match the
text and is always dp(150). But you should not set the width of the
button using parameter increment_width. You should set the width
instead using the width parameter.
MDRoundFlatButton¶
Button parameters MDRoundFlatButton are the same as
button MDRectangleFlatButton:
MDRoundFlatButton:
text: "MDROUNDFLATBUTTON"
Warning
The border color does not change when using
text_color parameter.
MDRoundFlatButton:
text: "MDROUNDFLATBUTTON"
text_color: 0, 1, 0, 1
MDRoundFlatIconButton¶
Button parameters MDRoundFlatIconButton are the same as
button MDRoundFlatButton:
MDRoundFlatIconButton:
icon: "android"
text: "MDROUNDFLATICONBUTTON"
width: dp(250)
Warning
The border color does not change when using
text_color parameter.
Warning
MDRoundFlatIconButton does not stretch to match the
text and is always dp(150). But you should not set the width of the
button using parameter increment_width. You should set the width
instead using the width parameter.
MDFillRoundFlatButton¶
Button parameters MDFillRoundFlatButton are the same as
button MDRaisedButton.
MDFillRoundFlatIconButton¶
Button parameters MDFillRoundFlatIconButton are the same as
button MDRaisedButton.
Note
Notice that the width of the MDFillRoundFlatIconButton
button matches the size of the button text.
MDFloatingActionButtonSpeedDial¶
Note
See the full list of arguments in the class
MDFloatingActionButtonSpeedDial.
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
Screen:
MDFloatingActionButtonSpeedDial:
data: app.data
rotation_root_button: True
'''
class Example(MDApp):
data = {
'language-python': 'Python',
'language-php': 'PHP',
'language-cpp': 'C++',
}
def build(self):
return Builder.load_string(KV)
Example().run()
Or without KV Language:
from kivy.uix.screenmanager import Screen
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButtonSpeedDial
class Example(MDApp):
data = {
'language-python': 'Python',
'language-php': 'PHP',
'language-cpp': 'C++',
}
def build(self):
screen = Screen()
speed_dial = MDFloatingActionButtonSpeedDial()
speed_dial.data = self.data
speed_dial.rotation_root_button = True
screen.add_widget(speed_dial)
return screen
Example().run()
You can use various types of animation of labels for buttons on the stack:
MDFloatingActionButtonSpeedDial:
hint_animation: True
You can set your color values for background, text of buttons etc:
MDFloatingActionButtonSpeedDial:
bg_hint_color: app.theme_cls.primary_light
See also
API - kivymd.uix.button¶
Bases:
kivymd.uix.button.BaseRoundButton,kivymd.uix.button.BaseFlatButton,kivymd.uix.button.BasePressedButtonButton icon.
iconis anStringPropertyand defaults to ‘checkbox-blank-circle’.
Bases:
kivymd.uix.button.BaseRectangularButton,kivymd.uix.button.BaseFlatButton,kivymd.uix.button.BasePressedButton
Bases:
kivymd.uix.button.BaseRectangularButton,kivymd.uix.behaviors.RectangularElevationBehavior,kivymd.uix.button.BaseRaisedButton,kivymd.uix.button.BasePressedButton
Bases:
kivymd.uix.button.BaseRoundButton,kivymd.uix.behaviors.CircularElevationBehavior,kivymd.uix.button.BaseRaisedButtonButton icon.
iconis anStringPropertyand defaults to ‘android’.
The name of the palette used for the background color of the button.
background_paletteis anStringPropertyand defaults to ‘Accent’.
Bases:
kivymd.theming.ThemableBehavior,kivy.uix.button.ButtonCustom user button color if
rgbaformat.custom_coloris anListPropertyand defaults to [].
Bases:
kivymd.uix.button.BaseFlatIconButton
Bases:
kivymd.uix.button.MDRoundFlatButton,kivymd.uix.button.BaseFlatIconButton
Bases:
kivymd.uix.button.MDFillRoundFlatButtonButton icon.
iconis anStringPropertyand defaults to ‘android’.
Button extra width value.
increment_widthis anNumericPropertyand defaults to ‘80dp’.
Bases:
kivymd.theming.ThemableBehavior,kivy.uix.floatlayout.FloatLayout- Events
Root button icon name.
iconis aStringPropertyand defaults to ‘plus’.
Stack anchor. Available options are: ‘right’.
anchoris aOptionPropertyand defaults to ‘right’.
Custom callback.
MDFloatingActionButtonSpeedDial: callback: app.callback
def callback(self, instance): print(instance.icon)
callbackis aObjectPropertyand defaults to None.
Floating text color in
rgbaformat.label_text_coloris aListPropertyand defaults to [0, 0, 0, 1].
Must be a dictionary
{ 'name-icon': 'Text label', ..., ..., }
If True, the button will increase on the right side by 2.5 piesels if the
hint_animationparameter equal to True.False
True
right_padis aBooleanPropertyand defaults to False.
If
Truethen the root button will rotate 45 degrees when the stack is opened.rotation_root_buttonis aBooleanPropertyand defaults to False.
The name of the stack opening animation type.
opening_transitionis aStringPropertyand defaults to ‘out_cubic’.
The name of the stack closing animation type.
closing_transitionis aStringPropertyand defaults to ‘out_cubic’.
The name of the animation type to rotate the root button when opening the stack.
opening_transition_button_rotationis aStringPropertyand defaults to ‘out_cubic’.
The name of the animation type to rotate the root button when closing the stack.
closing_transition_button_rotationis aStringPropertyand defaults to ‘out_cubic’.
Time required for the stack to go to: attr:state ‘open’.
opening_timeis aNumericPropertyand defaults to 0.2.
Time required for the stack to go to: attr:state ‘close’.
closing_timeis aNumericPropertyand defaults to 0.2.
Time required to rotate the root button 45 degrees during the stack opening animation.
opening_time_button_rotationis aNumericPropertyand defaults to 0.2.
Time required to rotate the root button 0 degrees during the stack closing animation.
closing_time_button_rotationis aNumericPropertyand defaults to 0.2.
Indicates whether the stack is closed or open. Available options are: ‘close’, ‘open’.
stateis aOptionPropertyand defaults to ‘close’.
Root button color in
rgbaformat.bg_color_root_buttonis aListPropertyand defaults to [].
The color of the buttons in the stack
rgbaformat.bg_color_stack_buttonis aListPropertyand defaults to [].
The color icon of the buttons in the stack
rgbaformat.color_icon_stack_buttonis aListPropertyand defaults to [].
The color icon of the root button
rgbaformat.color_icon_root_buttonis aListPropertyand defaults to [].
Background color for the text of the buttons in the stack
rgbaformat.bg_hint_coloris aListPropertyand defaults to [].
Whether to use button extension animation to display text labels.
hint_animationis aBooleanPropertyand defaults to False.
Called when a stack is opened.
Called when a stack is closed.
Called when the mouse cursor goes outside the button of stack.
Called when the mouse cursor is over a button from the stack.
Creates a stack of buttons.
Sets the position of the floating labels.
Sets the position of the root button.
Sets the position of the bottom buttons in a stack.
Opens a button stack.
Closes the button stack.