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: See also
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):
return Builder.load_string(KV)
Example().run()
icon
parameter must have the name of the icon
from kivymd/icon_definitions.py
file.MDIconButton:
icon: "data/logo/kivy-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 TestNavigationDrawer(MDApp):
def build(self):
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"],
)
)
TestNavigationDrawer().run()
MDFlatButton#
text_color
parameter:MDFlatButton:
text: "MDFLATBUTTON"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
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: 1, 0, 1, 1
MDRectangleFlatButton#
MDRectangleFlatButton:
text: "MDRECTANGLEFLATBUTTON"
theme_text_color: "Custom"
text_color: 1, 0, 0, 1
line_color: 0, 0, 1, 1
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: 0, 0, 1, 1
line_color: 1, 0, 1, 1
theme_icon_color: "Custom"
icon_color: 1, 0, 0, 1
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):
screen = MDScreen()
screen.add_widget(
MDRectangleFlatIconButton(
text="MDRectangleFlatIconButton",
icon="language-python",
line_color=(0, 0, 0, 0),
pos_hint={"center_x": .5, "center_y": .5},
)
)
return screen
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: 0, 1, 0, 1
MDRoundFlatIconButton#
MDRoundFlatIconButton
are the same as
button MDRoundFlatButton
, with the addition of the
theme_icon_color
and icon_color
parameters as for MDIconButton
:MDRoundFlatIconButton:
icon: "android"
text: "MDROUNDFLATICONBUTTON"
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: 0, 1, 0, 1
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):
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):
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()
MDFloatingActionButtonSpeedDial:
hint_animation: True
MDFloatingActionButtonSpeedDial:
bg_hint_color: app.theme_cls.primary_light
API - kivymd.uix.button.button
#
- class kivymd.uix.button.button.BaseButton(**kwargs)#
Base class for all buttons.
- 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.
padding
is aVariableListProperty
and defaults to [16dp, 8dp, 16dp, 8dp].
- halign#
Horizontal anchor.
New in version 1.0.0.
anchor_x
is anOptionProperty
and defaults to ‘center’. It accepts values of ‘left’, ‘center’ or ‘right’.
- valign#
Vertical anchor.
New in version 1.0.0.
anchor_y
is anOptionProperty
and defaults to ‘center’. It accepts values of ‘top’, ‘center’ or ‘bottom’.
- text#
Button text.
text
is aStringProperty
and defaults to ‘’.
- icon#
Button icon.
icon
is aStringProperty
and 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_style
is aStringProperty
and defaults to ‘Body1’.
- theme_text_color#
Button text type. Available options are: (“Primary”, “Secondary”, “Hint”, “Error”, “Custom”, “ContrastParentBackground”).
theme_text_color
is anOptionProperty
and 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_color
is anOptionProperty
and defaults to None (set by button subclass).
- text_color#
Button text color in (r, g, b, a) format.
text_color
is aColorProperty
and defaults to None.
- icon_color#
Button icon color in (r, g, b, a) format.
icon_color
is aColorProperty
and defaults to None.
- font_name#
Button text font name.
font_name
is aStringProperty
and defaults to ‘’.
- font_size#
Button text font size.
font_size
is aNumericProperty
and 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_size
is aNumericProperty
and defaults to None.
- line_width#
Line width for button border.
line_width
is aNumericProperty
and defaults to 1.
- line_color#
Line color for button border.
line_color
is aColorProperty
and defaults to None.
- line_color_disabled#
Disabled line color for button border.
New in version 1.0.0.
line_color_disabled
is aColorProperty
and defaults to None.
- md_bg_color#
Button background color.
md_bg_color
is aColorProperty
and defaults to None.
- md_bg_color_disabled#
The background color of the button when the button is disabled.
md_bg_color_disabled
is aColorProperty
and defaults to None.
- disabled_color#
- The color of the text and icon when the button is disabled, in the
(r, g, b, a) format.
New in version 1.0.0.
disabled_color
is aColorProperty
and defaults to None.
- rounded_button#
Should the button have fully rounded corners (e.g. like M3 buttons)?
New in version 1.0.0.
rounded_button
is aBooleanProperty
and 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(**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.
padding
is aVariableListProperty
and defaults to [8dp, 8dp, 8dp, 8dp].
- class kivymd.uix.button.button.MDRaisedButton(**kwargs)#
A flat button with (by default) a primary color fill and matching color text.
- class kivymd.uix.button.button.MDRectangleFlatButton(**kwargs)#
A flat button with (by default) a primary color border and primary color text.
- class kivymd.uix.button.button.MDRectangleFlatIconButton(**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(**kwargs)#
A flat button with (by default) fully rounded corners, a primary color border and primary color text.
- class kivymd.uix.button.button.MDRoundFlatIconButton(**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(**kwargs)#
A flat button with (by default) rounded corners, a primary color fill and primary color text.
- class kivymd.uix.button.button.MDFillRoundFlatIconButton(**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(**kwargs)#
A simple rounded icon button.
- icon#
Button icon.
icon
is aStringProperty
and defaults to ‘checkbox-blank-circle’.
- class kivymd.uix.button.button.MDFloatingActionButton(**kwargs)#
Implementation FAB button.
- type#
Type of M3 button.
New in version 1.0.0.
Available options are: ‘small’, ‘large’, ‘standard’.
type
is anOptionProperty
and defaults to ‘standard’.
- set_font_size(self, *args)#
- set__radius(self, *args)#
- set_size(self, *args)#
- class kivymd.uix.button.button.MDTextButton(**kwargs)#
This mixin class provides
Button
behavior. Please see thebutton behaviors module
documentation 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) format.
color
is aColorProperty
and defaults to None.
- color_disabled#
Button color disabled in (r, g, b, a) format.
color_disabled
is aColorProperty
and defaults to None.
- animation_label(self)#
- on_press(self, *args)#
- on_disabled(self, instance_button, disabled_value)#
This function hides the shadow when the widget is disabled. It sets the shadow to 0.
- class kivymd.uix.button.button.MDFloatingActionButtonSpeedDial(**kwargs)#
- Events:
- icon#
Root button icon name.
icon
is aStringProperty
and defaults to ‘plus’.
- anchor#
Stack anchor. Available options are: ‘right’.
anchor
is aOptionProperty
and defaults to ‘right’.
- callback#
Custom callback.
MDFloatingActionButtonSpeedDial: callback: app.callback
def callback(self, instance): print(instance.icon)
callback
is aObjectProperty
and defaults to None.
- label_text_color#
Floating text color in (r, g, b, a) format.
label_text_color
is aColorProperty
and defaults to [0, 0, 0, 1].
- data#
Must be a dictionary
{ 'name-icon': 'Text label', ..., ..., }
- right_pad#
If True, the button will increase on the right side by 2.5 pixels if the
hint_animation
parameter equal to True.False
True
right_pad
is aBooleanProperty
and defaults to False.
- root_button_anim#
If
True
then the root button will rotate 45 degrees when the stack is opened.root_button_anim
is aBooleanProperty
and defaults to False.
- opening_transition#
The name of the stack opening animation type.
opening_transition
is aStringProperty
and defaults to ‘out_cubic’.
- closing_transition#
The name of the stack closing animation type.
closing_transition
is aStringProperty
and 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_rotation
is aStringProperty
and 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_rotation
is aStringProperty
and defaults to ‘out_cubic’.
- opening_time#
Time required for the stack to go to: attr:state ‘open’.
opening_time
is aNumericProperty
and defaults to 0.2.
- closing_time#
Time required for the stack to go to: attr:state ‘close’.
closing_time
is aNumericProperty
and 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_rotation
is aNumericProperty
and 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_rotation
is aNumericProperty
and defaults to 0.2.
- state#
Indicates whether the stack is closed or open. Available options are: ‘close’, ‘open’.
state
is aOptionProperty
and defaults to ‘close’.
- bg_color_root_button#
Root button color in (r, g, b, a) format.
bg_color_root_button
is aColorProperty
and defaults to [].
- bg_color_stack_button#
The color of the buttons in the stack (r, g, b, a) format.
bg_color_stack_button
is aColorProperty
and defaults to [].
- color_icon_stack_button#
The color icon of the buttons in the stack (r, g, b, a) format.
color_icon_stack_button
is aColorProperty
and defaults to [].
- color_icon_root_button#
The color icon of the root button (r, g, b, a) format.
color_icon_root_button
is aColorProperty
and defaults to [].
- bg_hint_color#
Background color for the text of the buttons in the stack (r, g, b, a) format.
bg_hint_color
is aColorProperty
and defaults to None.
- hint_animation#
Whether to use button extension animation to display text labels.
hint_animation
is aBooleanProperty
and defaults to False.
- 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.
- 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.