test-kivy-app/kivy_venv/lib/python3.11/site-packages/kivymd/uix/button/button.py

1290 lines
32 KiB
Python
Raw Normal View History

2024-09-15 12:12:16 +00:00
"""
Components/Button
=================
.. seealso::
2024-09-15 17:57:02 +00:00
`Material Design spec, Buttons <https://m3.material.io/components/all-buttons>`_
2024-09-15 12:12:16 +00:00
.. rubric:: Buttons allow users to take actions, and make choices,
2024-09-15 17:57:02 +00:00
with a single tap. When choosing the right button for an action, consider
the level of emphasis each button type provides.
KivyMD provides the following button classes for use:
2024-09-15 12:12:16 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/buttons.png
:align: center
2024-09-15 17:57:02 +00:00
1. Elevated button
2. Filled button
3. Filled tonal button
4. Outlined button
5. Text button
6. Icon button
7. Segmented button
8. Floating action button (FAB)
9. Extended FAB
Common buttons
==============
.. rubric:: Buttons help people take action, such as sending an email, sharing
a document, or liking a comment.
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/common-buttons.png
:align: center
1. Elevated button
2. Filled button
3. Filled tonal button
4. Outlined button
5. Text button
Elevated_
Filled_
Tonal_
Outlined_
Text_
.. Elevated:
Elevated
--------
2024-09-15 12:12:16 +00:00
.. tabs::
.. tab:: Declarative KV style
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
2024-09-15 17:57:02 +00:00
md_bg_color: app.theme_cls.surfaceColor
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButton:
style: "elevated"
2024-09-15 12:12:16 +00:00
pos_hint: {"center_x": .5, "center_y": .5}
2024-09-15 17:57:02 +00:00
MDButtonIcon:
icon: "plus"
MDButtonText:
text: "Elevated"
2024-09-15 12:12:16 +00:00
'''
class Example(MDApp):
def build(self):
2024-09-15 17:57:02 +00:00
self.theme_cls.primary_palette = "Green"
2024-09-15 12:12:16 +00:00
return Builder.load_string(KV)
Example().run()
.. tab:: Declarative python style
.. code-block:: python
from kivymd.app import MDApp
2024-09-15 17:57:02 +00:00
from kivymd.uix.button import MDButton, MDButtonIcon, MDButtonText
2024-09-15 12:12:16 +00:00
from kivymd.uix.screen import MDScreen
class Example(MDApp):
def build(self):
2024-09-15 17:57:02 +00:00
self.theme_cls.primary_palette = "Green"
2024-09-15 12:12:16 +00:00
return (
MDScreen(
2024-09-15 17:57:02 +00:00
MDButton(
MDButtonIcon(
icon="plus",
),
MDButtonText(
text="Elevated",
),
style="elevated",
2024-09-15 12:12:16 +00:00
pos_hint={"center_x": 0.5, "center_y": 0.5},
2024-09-15 17:57:02 +00:00
),
md_bg_color=self.theme_cls.surfaceColor,
2024-09-15 12:12:16 +00:00
)
)
Example().run()
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/elevated-button.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
Common buttons can contain an icon or be without an icon:
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDButton:
style: "elevated"
text: "Elevated"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/elevated-without-icon-button.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. Filled:
Filled
------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDButton:
style: "filled"
MDButtonText:
text: "Filled"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/filled-button.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. Tonal:
Tonal
-----
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDButton:
style: "tonal"
MDButtonText:
text: "Tonal"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/tonal-button.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. Outlined:
Outlined
--------
.. code-block:: kv
MDButton:
style: "outlined"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Outlined"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/outlined-button.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. Text:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Text
----
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDButton:
style: "text"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Text"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/text-button.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
Customization of buttons
========================
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Text positioning and button size
--------------------------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButton:
style: "tonal"
theme_width: "Custom"
height: "56dp"
size_hint_x: .5
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonIcon:
x: text.x - (self.width + dp(10))
icon: "plus"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
id: text
text: "Tonal"
2024-09-15 12:12:16 +00:00
pos_hint: {"center_x": .5, "center_y": .5}
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/positioning-size-button.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Font of the button text
-----------------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButton:
style: "filled"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonIcon:
icon: "plus"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Filled"
font_style: "Title"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/font-style-button-text.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. code-block:: kv
MDButton:
style: "elevated"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Elevated"
theme_font_name: "Custom"
font_name: "path/to/font.ttf"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/font-name-button-text.png
:align: center
Custom button color
-------------------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDButton:
style: "elevated"
theme_shadow_color: "Custom"
shadow_color: "red"
MDButtonIcon:
icon: "plus"
theme_icon_color: "Custom"
icon_color: "green"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Elevated"
theme_text_color: "Custom"
text_color: "red"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/custom-color-button.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
Icon buttons
============
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. rubric:: Use icon buttons when a compact button is required, such as in a
toolbar or image list. There are two types of icon buttons: standard and
contained.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. seealso::
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
`Material Design spec, Icon buttons <https://m3.material.io/components/icon-buttons/overview>`_
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/icon-buttons.png
:align: center
1. Standard icon button
2. Contained icon button (including filled, filled tonal, and outlined styles)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
StandardIcon_
FilledIcon_
TonalIcon_
OutlinedIcon_
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. StandardIcon:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
StandardIcon
------------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDIconButton:
icon: "heart-outline"
style: "standard"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/icon-button-standard.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. FilledIcon:
FilledIcon
----------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDIconButton:
icon: "heart-outline"
style: "filled"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/icon-button-filled.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. TonalIcon:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
TonalIcon
---------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDIconButton:
icon: "heart-outline"
style: "tonal"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/icon-button-tonal.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. OutlinedIcon:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
OutlinedIcon
------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDIconButton:
icon: "heart-outline"
style: "outlined"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/icon-button-outlined.gif
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Custom icon size
----------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
MDIconButton:
icon: "heart-outline"
style: "tonal"
theme_font_size: "Custom"
font_size: "48sp"
radius: [self.height / 2, ]
size_hint: None, None
size: "84dp", "84dp"
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/icon-button-size.png
:align: center
Custom button color
-------------------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDIconButton:
icon: "heart-outline"
style: "tonal"
theme_bg_color: "Custom"
md_bg_color: "brown"
theme_icon_color: "Custom"
icon_color: "white"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/icon-button-color.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
FAB buttons
===========
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. rubric:: The FAB represents the most important action on a screen.
It puts key actions within reach.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. seealso::
`Material Design spec, FAB buttons <https://m3.material.io/components/floating-action-button/overview>`_
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fab-buttons.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
1. Standard FAB
2. Small FAB
3. Large FAB
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
There are three sizes of floating action buttons: FAB, small FAB, and large FAB:
Standard_
Small_
Large_
.. Standard:
Standard
--------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDFabButton:
icon: "pencil-outline"
style: "standard"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fab-button-standard.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. Small:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Small
-----
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDFabButton:
icon: "pencil-outline"
style: "small"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fab-button-small.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
.. Large:
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Large
-----
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDFabButton:
icon: "pencil-outline"
style: "large"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fab-button-large.gif
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
Additional color mappings
-------------------------
FABs can use other combinations of container and icon colors. The color
mappings below provide the same legibility and functionality as the default,
so the color mapping you use depends on style alone.
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fab-color-mapping.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
1. Surface
2. Secondary
3. Tertiary
2024-09-15 12:12:16 +00:00
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
2024-09-15 17:57:02 +00:00
from kivymd.uix.button import MDFabButton
2024-09-15 12:12:16 +00:00
KV = '''
MDScreen:
2024-09-15 17:57:02 +00:00
md_bg_color: app.theme_cls.surfaceColor
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDBoxLayout:
id: box
adaptive_size: True
spacing: "32dp"
pos_hint: {"center_x": .5, "center_y": .5}
2024-09-15 12:12:16 +00:00
'''
class Example(MDApp):
def build(self):
2024-09-15 17:57:02 +00:00
self.theme_cls.primary_palette = "Green"
2024-09-15 12:12:16 +00:00
return Builder.load_string(KV)
2024-09-15 17:57:02 +00:00
def on_start(self):
styles = {
"standard": "surface",
"small": "secondary",
"large": "tertiary",
}
for style in styles.keys():
self.root.ids.box.add_widget(
MDFabButton(
style=style, icon="pencil-outline", color_map=styles[style]
)
)
2024-09-15 12:12:16 +00:00
Example().run()
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fab-button-color-mapping.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
Extended FAB
============
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. rubric:: Extended floating action buttons (extended FABs) help people take
primary actions. They're wider than FABs to accommodate a text label and
larger target area.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. seealso::
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
`Material Design spec, FAB extended buttons <https://m3.material.io/components/extended-fab/overview>`_
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/extended-fab-button.png
:align: center
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
1. Extended FAB with both icon and label text
2. Extended FAB without icon
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
With icon
---------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: python
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
from kivy.lang import Builder
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
from kivymd.app import MDApp
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
KV = '''
MDScreen:
md_bg_color: app.theme_cls.surfaceColor
on_touch_down:
if not btn.collide_point(*args[1].pos): \\
btn.fab_state = "expand" \\
if btn.fab_state == "collapse" else "collapse"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDExtendedFabButton:
id: btn
pos_hint: {"center_x": .5, "center_y": .5}
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDExtendedFabButtonIcon:
icon: "pencil-outline"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDExtendedFabButtonText:
text: "Compose"
'''
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class Example(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
return Builder.load_string(KV)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Example().run()
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/extended-fab-button-icon.gif
:align: center
Without icon
------------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDExtendedFabButton:
fab_state: "expand"
MDExtendedFabButtonText:
text: "Compose"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/extended-fab-button-without-icon.png
2024-09-15 12:12:16 +00:00
:align: center
2024-09-15 17:57:02 +00:00
API break
=========
1.2.0 version
-------------
2024-09-15 12:12:16 +00:00
.. code-block:: kv
2024-09-15 17:57:02 +00:00
MDFloatingActionButton:
icon: "plus"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDRoundFlatButton:
text: "Outlined"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDRoundFlatIconButton:
text: "Outlined with icon"
icon: "plus"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDFillRoundFlatButton
text: "Filled"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDFillRoundFlatIconButton
text: "Filled with icon"
icon: "plus"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
2.0.0 version
-------------
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. note:: `MDFloatingActionButtonSpeedDial` type buttons were removed
in version `2.0.0`.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDFabButton:
icon: "plus"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButton:
style: "outlined"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Outlined"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButton:
style: "outlined"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonIcon:
icon: "plus"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Outlined with icon"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButton:
style: "filled"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Filled"
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. code-block:: kv
MDButton:
style: "filled"
MDButtonIcon:
icon: "plus"
MDButtonText:
text: "Filled"
2024-09-15 12:12:16 +00:00
"""
from __future__ import annotations
__all__ = (
"MDIconButton",
2024-09-15 17:57:02 +00:00
"MDButtonText",
"MDButtonIcon",
"MDFabButton",
"MDExtendedFabButton",
"MDExtendedFabButtonIcon",
"MDExtendedFabButtonText",
"MDButton",
"BaseButton",
"BaseFabButton",
2024-09-15 12:12:16 +00:00
)
import os
from kivy.clock import Clock
from kivy.lang import Builder
2024-09-15 17:57:02 +00:00
from kivy.metrics import dp
2024-09-15 12:12:16 +00:00
from kivy.properties import (
ColorProperty,
NumericProperty,
OptionProperty,
VariableListProperty,
2024-09-15 17:57:02 +00:00
ObjectProperty, DictProperty,
2024-09-15 12:12:16 +00:00
)
from kivy.uix.behaviors import ButtonBehavior
2024-09-15 17:57:02 +00:00
from kivy.uix.relativelayout import RelativeLayout
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
from kivymd.uix.label import MDIcon, MDLabel
2024-09-15 12:12:16 +00:00
from kivymd import uix_path
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import (
CommonElevationBehavior,
DeclarativeBehavior,
RectangularRippleBehavior,
2024-09-15 17:57:02 +00:00
BackgroundColorBehavior,
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
from kivymd.uix.behaviors.motion_behavior import MotionExtendedFabButtonBehavior
from kivymd.uix.behaviors.state_layer_behavior import StateLayerBehavior
2024-09-15 12:12:16 +00:00
with open(
os.path.join(uix_path, "button", "button.kv"), encoding="utf-8"
) as kv_file:
Builder.load_string(kv_file.read())
2024-09-15 17:57:02 +00:00
class BaseFabButton:
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Implements the basic properties for the
:class:`~MDExtendedFabButton` and :class:`~MDFabButton` classes.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 2.0.0
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
elevation_levels = DictProperty(
{
0: 0,
1: dp(4),
2: dp(8),
3: dp(12),
4: dp(16),
5: dp(18),
}
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Elevation is measured as the distance between components along the z-axis
in density-independent pixels (dps).
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 1.2.0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`elevation_levels` is an :class:`~kivy.properties.DictProperty`
and defaults to `{0: dp(0), 1: dp(4), 2: dp(8), 3: dp(12), 4: dp(16), 5: dp(18)}`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
color_map = OptionProperty(
"surface", options=("surface", "secondary", "tertiary")
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Additional color mappings.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Available options are: 'surface', 'secondary', 'tertiary'.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`color_map` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'secondary'`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
icon_color_disabled = ColorProperty(None)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The icon color in (r, g, b, a) or string format of the list item when
the widget item is disabled.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`icon_color_disabled` is a :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
style = OptionProperty("standard", options=("standard", "small", "large"))
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Button type.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Available options are: 'standard', 'small', 'large'.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`style` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'standard'`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
fab_state = OptionProperty("collapse", options=("collapse", "expand"))
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The state of the button.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Available options are: 'collapse' or 'expand'.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`fab_state` is an :class:`~kivy.properties.OptionProperty`
and defaults to "collapse".
"""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
md_bg_color_disabled = ColorProperty(None)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The background color in (r, g, b, a) or string format of the list item when
the list button is disabled.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`md_bg_color_disabled` is a :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
radius = VariableListProperty(
[
dp(16),
],
length=4,
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Canvas radius.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`radius` is an :class:`~kivy.properties.VariableListProperty`
and defaults to `[dp(16), dp(16), dp(16), dp(16)]`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
class BaseButton(
DeclarativeBehavior,
BackgroundColorBehavior,
RectangularRippleBehavior,
ButtonBehavior,
ThemableBehavior,
StateLayerBehavior,
):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Base button class.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
For more information, see in the
:class:`~kivymd.uix.behaviors.declarative_behavior.DeclarativeBehavior` and
:class:`~kivymd.uix.behaviors.backgroundcolor_behavior.BackgroundColorBehavior` and
:class:`~kivymd.uix.behaviors.ripple_behavior.RectangularRippleBehavior` and
:class:`~kivy.uix.behaviors.ButtonBehavior` and
:class:`~kivymd.theming.ThemableBehavior` and
:class:`~kivymd.uix.behaviors.state_layer_behavior.StateLayerBehavior`
classes documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
elevation_levels = DictProperty(
{
0: 0,
1: dp(4),
2: dp(8),
3: dp(12),
4: dp(16),
5: dp(18),
}
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Elevation is measured as the distance between components along the z-axis
in density-independent pixels (dps).
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 1.2.0
:attr:`elevation_levels` is an :class:`~kivy.properties.DictProperty`
and defaults to `{0: dp(0), 1: dp(4), 2: dp(8), 3: dp(12), 4: dp(16), 5: dp(18)}`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
md_bg_color_disabled = ColorProperty(None)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The background color in (r, g, b, a) or string format of the button when
the button is disabled.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`md_bg_color_disabled` is a :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
shadow_radius = VariableListProperty([0, 0, 0, 0])
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Button shadow radius.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`shadow_radius` is an :class:`~kivy.properties.VariableListProperty`
and defaults to `[0, 0, 0, 0]`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
md_bg_color = ColorProperty(None)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Button background color in (r, g, b, a) or string format.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`md_bg_color` is a :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
line_color = ColorProperty(None)
"""
Outlined color.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`line_color` is a :class:`~kivy.properties.ColorProperty`
2024-09-15 12:12:16 +00:00
and defaults to `None`.
"""
line_width = NumericProperty(1)
"""
Line width for button border.
:attr:`line_width` is a :class:`~kivy.properties.NumericProperty`
and defaults to `1`.
"""
2024-09-15 17:57:02 +00:00
def on_press(self, *args) -> None:
"""Fired when the button is pressed."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
self._on_press(args)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def on_release(self, *args) -> None:
"""
Fired when the button is released
(i.e. the touch/click that pressed the button goes away).
"""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
self._on_release(args)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDButton(BaseButton, CommonElevationBehavior, RelativeLayout):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Base class for all buttons.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 2.2.0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
For more information, see in the
:class:`~kivymd.uix.behaviors.elevation.CommonElevationBehavior` and
:class:`~BaseButton` and
:class:`~kivy.uix.relativelayout.RelativeLayout`
classes documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
style = OptionProperty(
"elevated", options=("elevated", "filled", "tonal", "outlined", "text")
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Button type.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Available options are: 'filled', 'elevated', 'outlined', 'tonal', 'text'.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`style` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'elevated'`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
radius = VariableListProperty(
[
dp(20),
]
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Button radius.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`radius` is an :class:`~kivy.properties.VariableListProperty`
and defaults to `[dp(20), dp(20), dp(20), dp(20)]`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
# kivymd.uix.button.button.MDButtonIcon object.
_button_icon = ObjectProperty()
# kivymd.uix.button.button.MDButtonText object.
_button_text = ObjectProperty()
_icon_left_pad = dp(16)
_spacing_between_icon_text = dp(10)
_text_right_pad = dp(24)
_text_left_pad = dp(24)
2024-09-15 12:12:16 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2024-09-15 17:57:02 +00:00
Clock.schedule_once(self.adjust_width, 0.2)
Clock.schedule_once(self.adjust_pos, 0.2)
def adjust_pos(self, *args) -> None:
"""Adjusts the pos of the button according to the content."""
if self._button_icon and self._button_text:
self._button_text.x = (
self._button_icon.x
+ self._spacing_between_icon_text
+ self._icon_left_pad
+ dp(2)
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
elif not self._button_icon and self._button_text:
self._button_text.x = self._text_left_pad
def adjust_width(self, *args) -> None:
"""Adjusts the width of the button according to the content."""
if self._button_icon and self._button_text:
if self.theme_width == "Primary":
self.width = (
self._button_icon.texture_size[0]
+ self._button_text.texture_size[0]
+ self._icon_left_pad
+ self._spacing_between_icon_text
+ self._text_right_pad
)
elif not self._button_icon and self._button_text:
if self.theme_width == "Primary":
self.width = (
self._button_text.texture_size[0]
+ self._text_left_pad
+ self._text_right_pad
)
elif self._button_icon and not self._button_text:
if self.theme_width == "Primary":
self.width = (
dp(48)
+ self._button_icon.texture_size[0]
- self._spacing_between_icon_text
)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def add_widget(self, widget, *args, **kwargs):
if isinstance(widget, MDButtonText):
self._button_text = widget
widget.bind(
text=lambda x, y: Clock.schedule_once(self.adjust_width, 0.2)
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
widget._button = self
elif isinstance(widget, MDButtonIcon):
self._button_icon = widget
widget._button = self
if isinstance(widget, (MDButtonIcon, MDButtonText)):
return super().add_widget(widget)
def set_properties_widget(self) -> None:
"""Fired `on_release/on_press/on_enter/on_leave` events."""
super().set_properties_widget()
if (
self._state == self.state_hover
and self.focus_behavior
or self._state == self.state_press
):
self._elevation_level = (
1
if self.theme_elevation_level == "Primary"
else self.elevation_level
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
self._shadow_softness = (
0
if self.theme_shadow_softness == "Primary"
else self.shadow_softness
2024-09-15 12:12:16 +00:00
)
2024-09-15 17:57:02 +00:00
if not self.disabled:
if self._state == self.state_hover and self.focus_behavior:
if self.style == "elevated":
self.elevation_level = 2
self.shadow_softness = 2
elif self._state == self.state_press:
if self.style == "elevated":
self.elevation_level = 2
self.shadow_softness = 2
elif not self._state:
if self.style == "elevated":
self.elevation_level = 1
self.shadow_softness = 0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDButtonText(MDLabel):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The class implements the text for the :class:`~MDButton` class.
2024-09-15 12:12:16 +00:00
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.label.label.MDLabel` class documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
# kivymd.uix.button.button.MDButton object.
_button = ObjectProperty()
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDButtonIcon(MDIcon):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The class implements an icon for the :class:`~MDButton` class.
2024-09-15 12:12:16 +00:00
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.label.label.MDIcon` class documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
# kivymd.uix.button.button.MDButton object.
_button = ObjectProperty()
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDIconButton(RectangularRippleBehavior, ButtonBehavior, MDIcon):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Base class for icon buttons.
2024-09-15 12:12:16 +00:00
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.behaviors.ripple_behavior.RectangularRippleBehavior` and
:class:`~kivy.uix.behaviors.ButtonBehavior` and
:class:`~kivy.uix.label.label.MDIcon`
2024-09-15 12:12:16 +00:00
classes documentation.
"""
2024-09-15 17:57:02 +00:00
style = OptionProperty(
"standard", options=("standard", "filled", "tonal", "outlined")
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Button type.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 2.0.0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
Available options are: 'standard', 'filled', 'tonal', 'outlined'.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`style` is an :class:`~kivy.properties.OptionProperty`
and defaults to `'standard'`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
md_bg_color_disabled = ColorProperty(None)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
The background color in (r, g, b, a) or string format of the list item when
the list button is disabled.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`md_bg_color_disabled` is a :class:`~kivy.properties.ColorProperty`
and defaults to `None`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
_line_color = ColorProperty(None)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def on_line_color(self, instance, value) -> None:
"""Fired when the values of :attr:`line_color` change."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
if not self.disabled and self.theme_line_color == "Custom":
self._line_color = value
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDFabButton(
BaseFabButton,
CommonElevationBehavior,
RectangularRippleBehavior,
ButtonBehavior,
MDIcon,
2024-09-15 12:12:16 +00:00
):
"""
2024-09-15 17:57:02 +00:00
Base class for FAB buttons.
2024-09-15 12:12:16 +00:00
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~BaseFabButton` and
:class:`~kivymd.uix.behaviors.elevation.CommonElevationBehavior` and
:class:`~kivymd.uix.behaviors.ripple_behavior.RectangularRippleBehavior` and
:class:`~kivy.uix.behaviors.ButtonBehavior` and
:class:`~kivymd.uix.label.label.MDIcon`
2024-09-15 12:12:16 +00:00
classes documentation.
"""
2024-09-15 17:57:02 +00:00
def on_press(self, *args) -> None:
"""Fired when the button is pressed."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
self._on_press(args)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def on_release(self, *args) -> None:
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Fired when the button is released
(i.e. the touch/click that pressed the button goes away).
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
self._on_release(args)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def set_properties_widget(self) -> None:
"""Fired `on_release/on_press/on_enter/on_leave` events."""
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
super().set_properties_widget()
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
if (
self._state == self.state_hover
and self.focus_behavior
or self._state == self.state_press
):
self._elevation_level = (
1
if self.theme_elevation_level == "Primary"
else self.elevation_level
)
self._shadow_softness = (
0
if self.theme_shadow_softness == "Primary"
else self.shadow_softness
)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
if not self.disabled:
if (
self._state == self.state_hover and self.focus_behavior
):
self.elevation_level = 1
self.shadow_softness = 0
elif self._state == self.state_press:
self.elevation_level = 2
self.shadow_softness = 2
elif not self._state:
self.elevation_level = 1
self.shadow_softness = 0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class MDExtendedFabButtonIcon(MDIcon):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Implements an icon for the :class:`~MDExtendedFabButton` class.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 2.0.0
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
class MDExtendedFabButtonText(MDLabel):
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Implements the text for the class :class:`~MDExtendedFabButton` class.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 2.0.0
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
class MDExtendedFabButton(
DeclarativeBehavior,
ThemableBehavior,
MotionExtendedFabButtonBehavior,
CommonElevationBehavior,
StateLayerBehavior,
BaseFabButton,
ButtonBehavior,
RelativeLayout,
2024-09-15 12:12:16 +00:00
):
"""
2024-09-15 17:57:02 +00:00
Base class for Extended FAB buttons.
.. versionadded:: 2.0.0
2024-09-15 12:12:16 +00:00
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.behaviors.declarative_behavior.DeclarativeBehavior` and
2024-09-15 12:12:16 +00:00
:class:`~kivymd.theming.ThemableBehavior` and
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.behaviors.motion_behavior.MotionExtendedFabButtonBehavior` and
:class:`~kivymd.uix.behaviors.elevation.CommonElevationBehavior` and
:class:`~kivymd.uix.behaviors.state_layer_behavior.StateLayerBehavior` and
:class:`~BaseFabButton` and
:class:`~kivy.uix.behaviors.ButtonBehavior` and
:class:`~kivy.uix.relativelayout.RelativeLayout`
classes documentation.
2024-09-15 12:12:16 +00:00
:Events:
2024-09-15 17:57:02 +00:00
`on_collapse`
Fired when the button is collapsed.
`on_expand`
Fired when the button is expanded.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
elevation_levels = DictProperty(
2024-09-15 12:12:16 +00:00
{
2024-09-15 17:57:02 +00:00
0: 0,
1: dp(4),
2: dp(8),
3: dp(12),
4: dp(16),
5: dp(18),
2024-09-15 12:12:16 +00:00
}
2024-09-15 17:57:02 +00:00
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Elevation is measured as the distance between components along the z-axis
in density-independent pixels (dps).
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
.. versionadded:: 1.2.0
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`elevation_levels` is an :class:`~kivy.properties.DictProperty`
and defaults to `{0: dp(0), 1: dp(4), 2: dp(8), 3: dp(12), 4: dp(16), 5: dp(18)}`.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
_icon = ObjectProperty()
_label = ObjectProperty()
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.register_event_type("on_collapse")
self.register_event_type("on_expand")
Clock.schedule_once(self._set_text_pos, 0.5)
def add_widget(self, widget, *args, **kwargs):
if isinstance(widget, MDExtendedFabButtonIcon):
self._icon = widget
elif isinstance(widget, MDExtendedFabButtonText):
self._label = widget
widget.opacity = 0
return super().add_widget(widget)
def on_collapse(self, *args):
"""Fired when the button is collapsed."""
def on_expand(self, *args):
"""Fired when the button is expanded."""
def on_fab_state(self, instance, state: str) -> None:
"""Fired when the :attr:`fab_state` value changes."""
if state == "expand":
Clock.schedule_once(self.expand)
Clock.schedule_once(lambda x: self.dispatch("on_expand"))
elif state == "collapse":
Clock.schedule_once(self.collapse)
Clock.schedule_once(lambda x: self.dispatch("on_collapse"))
def on__x(self, instance, value) -> None:
self._label.x = (
self._icon.x + self._icon.texture_size[0] + dp(24) - value
)
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
def _set_text_pos(self, *args):
if self._icon and self._label:
self._label.x = self._icon.x + self._icon.texture_size[0] + dp(24)
elif not self._icon and self._label:
self._label.opacity = 1
self.width = self._label.texture_size[0] + dp(32)
self._label.pos_hint = {"center_x": 0.5}