Button#
Buttons allow users to take actions, and make choices,
with a single tap. KivyMD provides the following button classes for use: The You can also use custom icons: By default, By default, the color of The above parameters for To change To change the text color of: class:~MDFlatButton use the Or use markup: To specify the font size and font name, use the parameters as in the usual
Kivy buttons: This button is similar to the Button parameters Button parameters Button parameters Button parameters Note Notice that the width of the Note See the full list of arguments in the class
Or without KV Language: You can use various types of animation of labels for buttons on the stack: You can set your color values for background, text of buttons etc:
MDIconButton#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDIconButton:
icon: "language-python"
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
Example().run()
from kivymd.app import MDApp
from kivymd.uix.button import MDIconButton
from kivymd.uix.screen import MDScreen
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return (
MDScreen(
MDIconButton(
icon="language-python",
pos_hint={"center_x": 0.5, "center_y": 0.5},
)
)
)
Example().run()
icon parameter must have the name of the icon
from kivymd/icon_definitions.py file.MDIconButton:
icon: "kivymd/images/logo/kivymd-icon-256.png"
MDIconButton button has a size (dp(48), dp (48)).
Use icon_size attribute to resize the button:MDIconButton:
icon: "android"
icon_size: "64sp"
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, substituting theme_icon_color for
theme_text_color and icon_color for text_color.MDIconButton:
icon: "android"
theme_icon_color: "Custom"
icon_color: app.theme_cls.primary_color
MDFloatingActionButton#
MDIconButton apply
to MDFloatingActionButton.MDFloatingActionButton background, use the
md_bg_color parameter:MDFloatingActionButton:
icon: "android"
md_bg_color: app.theme_cls.primary_color
Material design style 3#
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButton
KV = '''
MDScreen:
md_bg_color: "#f7f2fa"
MDBoxLayout:
id: box
spacing: "56dp"
adaptive_size: True
pos_hint: {"center_x": .5, "center_y": .5}
'''
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
self.theme_cls.material_style = "M3"
return Builder.load_string(KV)
def on_start(self):
data = {
"standard": {"md_bg_color": "#fefbff", "text_color": "#6851a5"},
"small": {"md_bg_color": "#e9dff7", "text_color": "#211c29"},
"large": {"md_bg_color": "#f8d7e3", "text_color": "#311021"},
}
for type_button in data.keys():
self.root.ids.box.add_widget(
MDFloatingActionButton(
icon="pencil",
type=type_button,
theme_icon_color="Custom",
md_bg_color=data[type_button]["md_bg_color"],
icon_color=data[type_button]["text_color"],
)
)
Example().run()
MDFlatButton#
text_color parameter:MDFlatButton:
text: "MDFlatButton"
theme_text_color: "Custom"
text_color: "orange"
MDFlatButton:
text: "[color=#00ffcc]MDFlatButton[/color]"
MDFlatButton:
text: "MDFlatButton"
font_size: "18sp"
font_name: "path/to/font"
MDRaisedButton#
MDFlatButton button except that you
can set the background color for MDRaisedButton:MDRaisedButton:
text: "MDRaisedButton"
md_bg_color: "red"
MDRectangleFlatButton#
MDRectangleFlatButton:
text: "MDRectangleFlatButton"
theme_text_color: "Custom"
text_color: "white"
line_color: "red"
MDRectangleFlatIconButton#
MDRectangleFlatIconButton are the same as
button MDRectangleFlatButton, with the addition of the
theme_icon_color and icon_color parameters as for MDIconButton.MDRectangleFlatIconButton:
icon: "android"
text: "MDRectangleFlatIconButton"
theme_text_color: "Custom"
text_color: "white"
line_color: "red"
theme_icon_color: "Custom"
icon_color: "orange"
Without border#
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.button import MDRectangleFlatIconButton
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return (
MDScreen(
MDRectangleFlatIconButton(
text="MDRectangleFlatIconButton",
icon="language-python",
line_color=(0, 0, 0, 0),
pos_hint={"center_x": .5, "center_y": .5},
)
)
)
Example().run()
MDRectangleFlatIconButton:
text: "MDRectangleFlatIconButton"
icon: "language-python"
line_color: 0, 0, 0, 0
pos_hint: {"center_x": .5, "center_y": .5}
MDRoundFlatButton#
MDRoundFlatButton:
text: "MDRoundFlatButton"
text_color: "white"
MDRoundFlatIconButton#
MDRoundFlatIconButton are the same as
button MDRoundFlatButton, with the addition of the
theme_icon_color and icon_color parameters as for MDIconButton:MDRoundFlatIconButton:
text: "MDRoundFlatIconButton"
icon: "android"
text_color: "white"
MDFillRoundFlatButton#
MDFillRoundFlatButton are the same as
button MDRaisedButton.MDFillRoundFlatIconButton#
MDFillRoundFlatIconButton are the same as
button MDRaisedButton, with the addition of the
theme_icon_color and icon_color parameters as for MDIconButton.MDFillRoundFlatIconButton
button matches the size of the button text.MDTextButton#
MDTextButton:
text: "MDTextButton"
custom_color: "white"
MDFloatingActionButtonSpeedDial#
MDFloatingActionButtonSpeedDial.from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
MDFloatingActionButtonSpeedDial:
data: app.data
root_button_anim: True
'''
class Example(MDApp):
data = {
'Python': 'language-python',
'PHP': 'language-php',
'C++': 'language-cpp',
}
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
Example().run()
from kivymd.uix.screen import MDScreen
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButtonSpeedDial
class Example(MDApp):
data = {
'Python': 'language-python',
'PHP': 'language-php',
'C++': 'language-cpp',
}
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
screen = MDScreen()
speed_dial = MDFloatingActionButtonSpeedDial()
speed_dial.data = self.data
speed_dial.root_button_anim = True
screen.add_widget(speed_dial)
return screen
Example().run()
from kivymd.uix.screen import MDScreen
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButtonSpeedDial
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return (
MDScreen(
MDFloatingActionButtonSpeedDial(
data={
'Python': 'language-python',
'PHP': 'language-php',
'C++': 'language-cpp',
},
root_button_anim=True,
)
)
)
Example().run()
MDFloatingActionButtonSpeedDial:
hint_animation: True
MDFloatingActionButtonSpeedDial:
hint_animation: True
bg_hint_color: app.theme_cls.primary_dark
Binds to individual buttons#
from kivy.lang import Builder
from kivy.properties import DictProperty
from kivymd.app import MDApp
KV = '''
MDScreen:
MDFloatingActionButtonSpeedDial:
id: speed_dial
data: app.data
root_button_anim: True
hint_animation: True
'''
class Example(MDApp):
data = DictProperty()
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
self.data = {
'Python': 'language-python',
'JS': [
'language-javascript',
"on_press", lambda x: print("pressed JS"),
"on_release", lambda x: print(
"stack_buttons",
self.root.ids.speed_dial.stack_buttons
)
],
'PHP': [
'language-php',
"on_press", lambda x: print("pressed PHP"),
"on_release", self.callback
],
'C++': [
'language-cpp',
"on_press", lambda x: print("pressed C++"),
"on_release", lambda x: self.callback()
],
}
return Builder.load_string(KV)
def callback(self, *args):
print(args)
Example().run()
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButtonSpeedDial
from kivymd.uix.screen import MDScreen
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return (
MDScreen(
MDFloatingActionButtonSpeedDial(
id="speed_dial",
hint_animation=True,
root_button_anim=True,
)
)
)
def on_start(self):
data = {
"Python": "language-python",
"JS": [
"language-javascript",
"on_press", lambda x: print("pressed JS"),
"on_release", lambda x: print(
"stack_buttons",
self.root.ids.speed_dial.stack_buttons
)
],
"PHP": [
"language-php",
"on_press", lambda x: print("pressed PHP"),
"on_release", self.callback
],
"C++": [
"language-cpp",
"on_press", lambda x: print("pressed C++"),
"on_release", lambda x: self.callback()
],
}
self.root.ids.speed_dial.data = data
def callback(self, *args):
print(args)
Example().run()
API - kivymd.uix.button.button#
- class kivymd.uix.button.button.BaseButton(*args, **kwargs)#
Base class for all buttons.
For more information, see in the
AnchorLayoutclass documentation.- padding#
Padding between the widget box and its children, in pixels: [padding_left, padding_top, padding_right, padding_bottom].
padding also accepts a two argument form [padding_horizontal, padding_vertical] and a one argument form [padding].
New in version 1.0.0.
paddingis aVariableListPropertyand defaults to [16dp, 8dp, 16dp, 8dp].
- halign#
Horizontal anchor.
New in version 1.0.0.
anchor_xis anOptionPropertyand defaults to ‘center’. It accepts values of ‘left’, ‘center’ or ‘right’.
- valign#
Vertical anchor.
New in version 1.0.0.
anchor_yis anOptionPropertyand defaults to ‘center’. It accepts values of ‘top’, ‘center’ or ‘bottom’.
- text#
Button text.
textis aStringPropertyand defaults to ‘’.
- icon#
Button icon.
iconis aStringPropertyand defaults to ‘’.
- font_style#
Button text font style.
Available vanilla font_style are: ‘H1’, ‘H2’, ‘H3’, ‘H4’, ‘H5’, ‘H6’, ‘Subtitle1’, ‘Subtitle2’, ‘Body1’, ‘Body2’, ‘Button’, ‘Caption’, ‘Overline’, ‘Icon’.
font_styleis aStringPropertyand defaults to ‘Body1’.
- theme_text_color#
Button text type. Available options are: (“Primary”, “Secondary”, “Hint”, “Error”, “Custom”, “ContrastParentBackground”).
theme_text_coloris anOptionPropertyand defaults to None (set by button class).
- theme_icon_color#
Button icon type. Available options are: (“Primary”, “Secondary”, “Hint”, “Error”, “Custom”, “ContrastParentBackground”).
New in version 1.0.0.
theme_icon_coloris anOptionPropertyand defaults to None (set by button subclass).
- text_color#
Button text color in (r, g, b, a) or string format.
text_coloris aColorPropertyand defaults to None.
- icon_color#
Button icon color in (r, g, b, a) or string format.
icon_coloris aColorPropertyand defaults to None.
- font_name#
Button text font name.
font_nameis aStringPropertyand defaults to ‘’.
- font_size#
Button text font size.
font_sizeis aNumericPropertyand defaults to 14sp.
- icon_size#
Icon font size. Use this parameter as the font size, that is, in sp units.
New in version 1.0.0.
icon_sizeis aNumericPropertyand defaults to None.
- line_width#
Line width for button border.
line_widthis aNumericPropertyand defaults to 1.
- line_color#
Line color in (r, g, b, a) or string format for button border.
line_coloris aColorPropertyand defaults to None.
- line_color_disabled#
Disabled line color in (r, g, b, a) or string format for button border.
New in version 1.0.0.
line_color_disabledis aColorPropertyand defaults to None.
- md_bg_color#
Button background color in (r, g, b, a) or string format.
md_bg_coloris aColorPropertyand defaults to None.
- md_bg_color_disabled#
The background color in (r, g, b, a) or string format of the button when the button is disabled.
md_bg_color_disabledis aColorPropertyand defaults to None.
- disabled_color#
The color of the text and icon when the button is disabled, in (r, g, b, a) or string format.
New in version 1.0.0.
disabled_coloris aColorPropertyand defaults to None.
- rounded_button#
Should the button have fully rounded corners (e.g. like M3 buttons)?
New in version 1.0.0.
rounded_buttonis aBooleanPropertyand defaults to False.
- set_disabled_color(self, *args)#
Sets the color for the icon, text and line of the button when button is disabled.
- set_all_colors(self, *args)#
Set all button colours.
- set_button_colors(self, *args)#
Set all button colours (except text/icons).
- set_text_color(self, *args)#
Set _theme_text_color and _text_color based on defaults and options.
- set_icon_color(self, *args)#
Set _theme_icon_color and _icon_color based on defaults and options.
- set_radius(self, *args)#
Set the radius, if we are a rounded button, based on the current height.
- on_touch_down(self, touch)#
Animates fade to background on press, for buttons with no background color.
- on_touch_up(self, touch)#
Animates return to original background on touch release.
- class kivymd.uix.button.button.MDFlatButton(*args, **kwargs)#
A flat rectangular button with (by default) no border or background. Text is the default text color.
- padding#
Padding between the widget box and its children, in pixels: [padding_left, padding_top, padding_right, padding_bottom].
padding also accepts a two argument form [padding_horizontal, padding_vertical] and a one argument form [padding].
New in version 1.0.0.
paddingis aVariableListPropertyand defaults to [8dp, 8dp, 8dp, 8dp].
- class kivymd.uix.button.button.MDRaisedButton(*args, **kwargs)#
A flat button with (by default) a primary color fill and matching color text.
- class kivymd.uix.button.button.MDRectangleFlatButton(*args, **kwargs)#
A flat button with (by default) a primary color border and primary color text.
- class kivymd.uix.button.button.MDRectangleFlatIconButton(*args, **kwargs)#
A flat button with (by default) a primary color border, primary color text and a primary color icon on the left.
- class kivymd.uix.button.button.MDRoundFlatButton(*args, **kwargs)#
A flat button with (by default) fully rounded corners, a primary color border and primary color text.
- class kivymd.uix.button.button.MDRoundFlatIconButton(*args, **kwargs)#
A flat button with (by default) rounded corners, a primary color border, primary color text and a primary color icon on the left.
- class kivymd.uix.button.button.MDFillRoundFlatButton(*args, **kwargs)#
A flat button with (by default) rounded corners, a primary color fill and primary color text.
- class kivymd.uix.button.button.MDFillRoundFlatIconButton(*args, **kwargs)#
A flat button with (by default) rounded corners, a primary color fill, primary color text and a primary color icon on the left.
- class kivymd.uix.button.button.MDIconButton(*args, **kwargs)#
A simple rounded icon button.
- icon#
Button icon.
iconis aStringPropertyand defaults to ‘checkbox-blank-circle’.
- class kivymd.uix.button.button.MDFloatingActionButton(*args, **kwargs)#
Implementation FAB button.
- type#
Type of M3 button.
New in version 1.0.0.
Available options are: ‘small’, ‘large’, ‘standard’.
typeis anOptionPropertyand defaults to ‘standard’.
- set_font_size(self, *args)#
- set__radius(self, *args)#
- set_size_and_radius(self, *args)#
- set_size(self, *args)#
- class kivymd.uix.button.button.MDTextButton(**kwargs)#
This mixin class provides
Buttonbehavior. Please see thebutton behaviors moduledocumentation for more information.- Events:
- on_press
Fired when the button is pressed.
- on_release
Fired when the button is released (i.e. the touch/click that pressed the button goes away).
- color#
Button color in (r, g, b, a) or string format.
coloris aColorPropertyand defaults to None.
- color_disabled#
Button color disabled in (r, g, b, a) or string format.
color_disabledis aColorPropertyand defaults to None.
- animation_label(self)#
- on_press(self, *args)#
- on_disabled(self, instance_button, disabled_value)#
- class kivymd.uix.button.button.MDFloatingActionButtonSpeedDial(**kwargs)#
For more information, see in the
FloatLayoutclass documentation.- Events:
on_openCalled when a stack is opened.
on_closeCalled when a stack is closed.
on_press_stack_buttonCalled at the on_press event for the stack button.
on_release_stack_buttonCalled at the on_press event for the stack button.
- icon#
Root button icon name.
MDFloatingActionButtonSpeedDial: icon: "pencil"
iconis aStringPropertyand defaults to ‘plus’.
- anchor#
Stack anchor. Available options are: ‘right’.
anchoris aOptionPropertyand defaults to ‘right’.
- label_text_color#
Color of floating text labels in (r, g, b, a) or string format.
MDFloatingActionButtonSpeedDial: label_text_color: "orange"
label_text_coloris aColorPropertyand defaults to None.
- label_bg_color#
Background color of floating text labels in (r, g, b, a) or string format.
MDFloatingActionButtonSpeedDial: label_text_color: "black" label_bg_color: "orange"
label_bg_coloris aColorPropertyand defaults to [0, 0, 0, 0].
- label_radius#
The radius of the background of floating text labels.
MDFloatingActionButtonSpeedDial: label_text_color: "black" label_bg_color: "orange"
label_radiusis aColorPropertyand defaults to [0, 0, 0, 0].
- data#
Must be a dictionary.
{ 'name-icon': 'Text label', ..., ..., }
- right_pad#
If True, the background for the floating text label will increase by the number of pixels specified in the
right_pad_valueparameter.Works only if the
hint_animationparameter is set to True.False
MDFloatingActionButtonSpeedDial: hint_animation: True right_pad: False
True
MDFloatingActionButtonSpeedDial: hint_animation: True right_pad: True right_pad_value: "10dp"
right_padis aBooleanPropertyand defaults to False.
- right_pad_value#
See
right_padparameter for more information.right_pad_valueis aNumericPropertyand defaults to 0.
- root_button_anim#
If
Truethen the root button will rotate 45 degrees when the stack is opened.root_button_animis aBooleanPropertyand defaults to False.
- opening_transition#
The name of the stack opening animation type.
opening_transitionis aStringPropertyand defaults to ‘out_cubic’.
- closing_transition#
The name of the stack closing animation type.
closing_transitionis aStringPropertyand defaults to ‘out_cubic’.
- opening_transition_button_rotation#
The name of the animation type to rotate the root button when opening the stack.
opening_transition_button_rotationis aStringPropertyand defaults to ‘out_cubic’.
- closing_transition_button_rotation#
The name of the animation type to rotate the root button when closing the stack.
closing_transition_button_rotationis aStringPropertyand defaults to ‘out_cubic’.
- opening_time#
Time required for the stack to go to: attr:state ‘open’.
opening_timeis aNumericPropertyand defaults to 0.2.
- closing_time#
Time required for the stack to go to: attr:state ‘close’.
closing_timeis aNumericPropertyand defaults to 0.2.
- opening_time_button_rotation#
Time required to rotate the root button 45 degrees during the stack opening animation.
opening_time_button_rotationis aNumericPropertyand defaults to 0.2.
- closing_time_button_rotation#
Time required to rotate the root button 0 degrees during the stack closing animation.
closing_time_button_rotationis aNumericPropertyand defaults to 0.2.
- state#
Indicates whether the stack is closed or open. Available options are: ‘close’, ‘open’.
stateis aOptionPropertyand defaults to ‘close’.
- bg_color_root_button#
Background color of root button in (r, g, b, a) or string format.
bg_color_root_buttonis aColorPropertyand defaults to None.
- bg_color_stack_button#
Background color of the stack buttons in (r, g, b, a) or string format.
bg_color_stack_buttonis aColorPropertyand defaults to None.
- color_icon_stack_button#
The color icon of the stack buttons in (r, g, b, a) or string format.
color_icon_stack_buttonis aColorPropertyand defaults to None.
- color_icon_root_button#
The color icon of the root button in (r, g, b, a) or string format.
color_icon_root_buttonis aColorPropertyand defaults to None.
- bg_hint_color#
Background color for the floating text of the buttons in (r, g, b, a) or string format.
bg_hint_coloris aColorPropertyand defaults to None.
- hint_animation#
Whether to use button extension animation to display floating text.
hint_animationis aBooleanPropertyand defaults to False.
- stack_buttons#
- on_open(self, *args)#
Called when a stack is opened.
- on_close(self, *args)#
Called when a stack is closed.
- on_leave(self, instance_button: MDFloatingBottomButton)#
Called when the mouse cursor goes outside the button of stack.
- on_enter(self, instance_button: MDFloatingBottomButton)#
Called when the mouse cursor is over a button from the stack.
- on_press_stack_button(self, *args)#
Called at the on_press event for the stack button.
MDFloatingActionButtonSpeedDial: on_press_stack_button: print(*args)
New in version 1.1.0.
- on_release_stack_button(self, *args)#
Called at the on_release event for the stack button.
MDFloatingActionButtonSpeedDial: on_release_stack_button: print(*args)
New in version 1.1.0.
- set_pos_labels(self, instance_floating_label: MDFloatingLabel)#
Sets the position of the floating labels. Called when the application’s root window is resized.
- set_pos_root_button(self, instance_floating_root_button: MDFloatingRootButton)#
Sets the position of the root button. Called when the application’s root window is resized.
- set_pos_bottom_buttons(self, instance_floating_bottom_button: MDFloatingBottomButton)#
Sets the position of the bottom buttons in a stack. Called when the application’s root window is resized.
- open_stack(self, instance_floating_root_button: MDFloatingRootButton)#
Opens a button stack.
- do_animation_open_stack(self, anim_data: dict)#
- Parameters:
anim_data –
- {
- <kivymd.uix.button.MDFloatingBottomButton object>:
<kivy.animation.Animation>,
- <kivymd.uix.button.MDFloatingBottomButton object>:
<kivy.animation.Animation object>,
…,
}
- close_stack(self)#
Closes the button stack.