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

172 lines
4.7 KiB
Python
Raw Normal View History

2024-09-15 12:12:16 +00:00
"""
Components/DropdownItem
=======================
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/dropdown-item.png
:align: center
Usage
-----
.. code-block:: python
from kivy.lang import Builder
2024-09-15 17:57:02 +00:00
from kivymd.uix.menu import MDDropdownMenu
2024-09-15 12:12:16 +00:00
from kivymd.app import MDApp
KV = '''
MDScreen
2024-09-15 17:57:02 +00:00
md_bg_color: self.theme_cls.backgroundColor
2024-09-15 12:12:16 +00:00
MDDropDownItem:
2024-09-15 17:57:02 +00:00
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.open_menu(self)
MDDropDownItemText:
id: drop_text
text: "Item"
2024-09-15 12:12:16 +00:00
'''
2024-09-15 17:57:02 +00:00
class Example(MDApp):
def open_menu(self, item):
menu_items = [
{
"text": f"{i}",
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
MDDropdownMenu(caller=item, items=menu_items).open()
def menu_callback(self, text_item):
self.root.ids.drop_text.text = text_item
2024-09-15 12:12:16 +00:00
def build(self):
2024-09-15 17:57:02 +00:00
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
.. seealso::
`Work with the class MDDropdownMenu see here <https://kivymd.readthedocs.io/en/latest/components/menu/index.html#center-position>`_
2024-09-15 17:57:02 +00:00
API break
=========
1.2.0 version
-------------
.. code-block:: kv
MDDropDownItem:
text: 'Item'
on_release: print(*args)
2.0.0 version
-------------
.. code-block:: kv
MDDropDownItem:
on_release: print(*args)
MDDropDownItemText:
text: "Item text"
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
__all__ = ("MDDropDownItem", "MDDropDownItemText")
2024-09-15 12:12:16 +00:00
import os
2024-09-15 17:57:02 +00:00
from kivy.clock import Clock
2024-09-15 12:12:16 +00:00
from kivy.lang import Builder
2024-09-15 17:57:02 +00:00
from kivy.metrics import dp
from kivy.properties import ObjectProperty, ListProperty
2024-09-15 12:12:16 +00:00
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
2024-09-15 17:57:02 +00:00
from kivymd.uix.label import MDLabel
2024-09-15 12:12:16 +00:00
from kivymd import uix_path
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import DeclarativeBehavior
with open(
os.path.join(uix_path, "dropdownitem", "dropdownitem.kv"), encoding="utf-8"
) as kv_file:
Builder.load_string(kv_file.read())
2024-09-15 17:57:02 +00:00
# FIXME: When resizing the texture of the `MDDropDownItemText` widget,
# the canvas instruction that implements the triangle looks terrible.
# You need to edit the Triangle instructions according to the size
# of the `MDDropDownItemText` texture.
class MDDropDownItemText(MDLabel):
"""
Base texture for :class:`~MDDropDownItem` class (item text).
For more information, see in the
:class:`~kivymd.uix.label.label.MDLabel` class documentation.
.. versionadded:: 2.0.0
"""
2024-09-15 12:12:16 +00:00
class MDDropDownItem(
DeclarativeBehavior, ThemableBehavior, ButtonBehavior, BoxLayout
):
"""
Dropdown item class.
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
:class:`~kivy.uix.behaviors.ButtonBehavior` and
:class:`~kivy.uix.boxlayout.BoxLayout`
classes documentation.
"""
2024-09-15 17:57:02 +00:00
_drop_down_text = ObjectProperty()
_size = ListProperty()
def add_widget(self, widget, *args, **kwargs):
if isinstance(widget, MDDropDownItemText):
self._drop_down_text = widget
widget.bind(text=self.update_text_item)
Clock.schedule_once(lambda x: self.on_disabled(self, self.disabled))
else:
return super().add_widget(widget)
def update_text_item(self, instance, value) -> None:
"""Updates the text of the item."""
self._drop_down_text.texture_update()
drop_down_item_text = self.canvas.get_group("drop-down-item-text")[0]
drop_down_item_text.texture = None
drop_down_item_text.texture = self._drop_down_text.texture
drop_down_item_text.size = self._drop_down_text.texture_size
drop_down_item_text.pos = (self.x, self.y + dp(8))
self._size = self._drop_down_text.texture_size
def on_disabled(self, instance, value) -> None:
"""Fired when the values of :attr:`disabled` change."""
self._drop_down_text.disabled = value
drop_down_item_triangle_color = self.canvas.get_group(
"drop-down-item-triangle-color"
)[0]
drop_down_item_triangle_color.rgba = (
self._drop_down_text.disabled_color
if value
else self._drop_down_text.color
)
def on__drop_down_text(self, instance, value) -> None:
"""Fired when the values of :attr:`_drop_down_text` change."""
def set_size(*args):
self._size = value.texture_size
Clock.schedule_once(set_size)