Menu#
See also Menus display a list of choices on temporary surfaces. Warning Do not create the Menu items are created in the same way as items for the
The Note This example uses drop down menus for both the righthand and
lefthand menus (i.e both the ‘triple bar’ and ‘triple dot’ menus) to
illustrate that it is possible. A better solution for the ‘triple bar’ menu
would probably have been See also
Usage#
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
KV = '''
MDScreen:
MDRaisedButton:
id: button
text: "PRESS ME"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.menu.open()
'''
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
menu_items = [
{
"text": f"Item {i}",
"viewclass": "OneLineListItem",
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
caller=self.screen.ids.button,
items=menu_items,
width_mult=4,
)
def menu_callback(self, text_item):
print(text_item)
def build(self):
return self.screen
Test().run()
MDDropdownMenu
object when you open
the menu window. Because on a mobile device this one will be very slow!Wrong#
menu = MDDropdownMenu(caller=self.screen.ids.button, items=menu_items)
menu.open()
Customization of menu item#
RecycleView
class.from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.list import IRightBodyTouch, OneLineAvatarIconListItem
from kivymd.uix.menu import MDDropdownMenu
KV = '''
<RightContentCls>
disabled: True
adaptive_size: True
pos_hint: {"center_y": .5}
MDIconButton:
icon: root.icon
user_font_size: "16sp"
md_bg_color_disabled: 0, 0, 0, 0
MDLabel:
text: root.text
font_style: "Caption"
adaptive_size: True
pos_hint: {"center_y": .5}
<Item>
IconLeftWidget:
icon: root.left_icon
RightContentCls:
id: container
icon: root.right_icon
text: root.right_text
MDScreen:
MDRaisedButton:
id: button
text: "PRESS ME"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.menu.open()
'''
class RightContentCls(IRightBodyTouch, MDBoxLayout):
icon = StringProperty()
text = StringProperty()
class Item(OneLineAvatarIconListItem):
left_icon = StringProperty()
right_icon = StringProperty()
right_text = StringProperty()
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
menu_items = [
{
"text": f"Item {i}",
"right_text": f"R+{i}",
"right_icon": "apple-keyboard-command",
"left_icon": "git",
"viewclass": "Item",
"height": dp(54),
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
caller=self.screen.ids.button,
items=menu_items,
width_mult=4,
)
def menu_callback(self, text_item):
print(text_item)
def build(self):
return self.screen
Test().run()
Header#
from kivy.lang import Builder
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.boxlayout import MDBoxLayout
KV = '''
<MenuHeader>
orientation: "vertical"
adaptive_size: True
padding: "4dp"
MDBoxLayout:
spacing: "12dp"
adaptive_size: True
MDIconButton:
icon: "gesture-tap-button"
pos_hint: {"center_y": .5}
MDLabel:
text: "Actions"
adaptive_size: True
pos_hint: {"center_y": .5}
MDScreen:
MDRaisedButton:
id: button
text: "PRESS ME"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.menu.open()
'''
class MenuHeader(MDBoxLayout):
'''An instance of the class that will be added to the menu header.'''
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
menu_items = [
{
"text": f"Item {i}",
"viewclass": "OneLineListItem",
"height": dp(56),
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
header_cls=MenuHeader(),
caller=self.screen.ids.button,
items=menu_items,
width_mult=4,
)
def menu_callback(self, text_item):
print(text_item)
def build(self):
return self.screen
Test().run()
Menu with MDTopAppBar#
MDDropdownMenu
works well with the standard
MDTopAppBar
. Since the buttons on the Toolbar are created
by the MDTopAppBar component, it is necessary to pass the button as an argument to
the callback using lambda x: app.callback(x).MDNavigationDrawer
.from kivy.lang import Builder
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.snackbar import Snackbar
KV = '''
MDBoxLayout:
orientation: "vertical"
MDTopAppBar:
title: "MDTopAppBar"
left_action_items: [["menu", lambda x: app.callback(x)]]
right_action_items: [["dots-vertical", lambda x: app.callback(x)]]
MDLabel:
text: "Content"
halign: "center"
'''
class Test(MDApp):
def build(self):
menu_items = [
{
"viewclass": "OneLineListItem",
"text": f"Item {i}",
"height": dp(56),
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
items=menu_items,
width_mult=4,
)
return Builder.load_string(KV)
def callback(self, button):
self.menu.caller = button
self.menu.open()
def menu_callback(self, text_item):
self.menu.dismiss()
Snackbar(text=text_item).open()
Test().run()
Position#
Bottom position#
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.uix.list import OneLineIconListItem
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
KV = '''
<IconListItem>
IconLeftWidget:
icon: root.icon
MDScreen
MDTextField:
id: field
pos_hint: {'center_x': .5, 'center_y': .6}
size_hint_x: None
width: "200dp"
hint_text: "Password"
on_focus: if self.focus: app.menu.open()
'''
class IconListItem(OneLineIconListItem):
icon = StringProperty()
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
menu_items = [
{
"viewclass": "IconListItem",
"icon": "git",
"height": dp(56),
"text": f"Item {i}",
"on_release": lambda x=f"Item {i}": self.set_item(x),
} for i in range(5)]
self.menu = MDDropdownMenu(
caller=self.screen.ids.field,
items=menu_items,
position="bottom",
width_mult=4,
)
def set_item(self, text__item):
self.screen.ids.field.text = text__item
self.menu.dismiss()
def build(self):
return self.screen
Test().run()
Center position#
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.uix.list import OneLineIconListItem
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
KV = '''
<IconListItem>
IconLeftWidget:
icon: root.icon
MDScreen
MDDropDownItem:
id: drop_item
pos_hint: {'center_x': .5, 'center_y': .5}
text: 'Item 0'
on_release: app.menu.open()
'''
class IconListItem(OneLineIconListItem):
icon = StringProperty()
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
menu_items = [
{
"viewclass": "IconListItem",
"icon": "git",
"text": f"Item {i}",
"height": dp(56),
"on_release": lambda x=f"Item {i}": self.set_item(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
caller=self.screen.ids.drop_item,
items=menu_items,
position="center",
width_mult=4,
)
self.menu.bind()
def set_item(self, text_item):
self.screen.ids.drop_item.set_item(text_item)
self.menu.dismiss()
def build(self):
return self.screen
Test().run()
API - kivymd.uix.menu.menu
#
- class kivymd.uix.menu.menu.MDDropdownMenu(**kwargs)#
- Events:
- on_release
The method that will be called when you click menu items.
- header_cls#
An instance of the class (Kivy or KivyMD widget) that will be added to the menu header.
New in version 0.104.2.
See Header for more information.
header_cls
is aObjectProperty
and defaults to None.
- items#
See
data
.items = [ { "viewclass": "OneLineListItem", "height": dp(56), "text": f"Item {i}", } for i in range(5) ] self.menu = MDDropdownMenu( items=items, ..., )
items
is aListProperty
and defaults to [].
- width_mult#
This number multiplied by the standard increment (‘56dp’ on mobile, ‘64dp’ on desktop), determines the width of the menu items.
If the resulting number were to be too big for the application Window, the multiplier will be adjusted for the biggest possible one.
self.menu = MDDropdownMenu( width_mult=4, ..., )
self.menu = MDDropdownMenu( width_mult=8, ..., )
width_mult
is aNumericProperty
and defaults to 1.
- max_height#
The menu will grow no bigger than this number. Set to 0 for no limit.
self.menu = MDDropdownMenu( max_height=dp(112), ..., )
self.menu = MDDropdownMenu( max_height=dp(224), ..., )
max_height
is aNumericProperty
and defaults to 0.
- border_margin#
Margin between Window border and menu.
self.menu = MDDropdownMenu( border_margin=dp(4), ..., )
self.menu = MDDropdownMenu( border_margin=dp(24), ..., )
border_margin
is aNumericProperty
and defaults to 4dp.
- ver_growth#
Where the menu will grow vertically to when opening. Set to None to let the widget pick for you. Available options are: ‘up’, ‘down’.
self.menu = MDDropdownMenu( ver_growth="up", ..., )
self.menu = MDDropdownMenu( ver_growth="down", ..., )
ver_growth
is aOptionProperty
and defaults to None.
- hor_growth#
Where the menu will grow horizontally to when opening. Set to None to let the widget pick for you. Available options are: ‘left’, ‘right’.
self.menu = MDDropdownMenu( hor_growth="left", ..., )
self.menu = MDDropdownMenu( hor_growth="right", ..., )
hor_growth
is aOptionProperty
and defaults to None.
- background_color#
Color in (r, g, b, a) or string format of the background of the menu.
self.menu = MDDropdownMenu( background_color=self.theme_cls.primary_light, ..., )
background_color
is aColorProperty
and defaults to None.
- opening_transition#
Type of animation for opening a menu window.
opening_transition
is aStringProperty
and defaults to ‘out_cubic’.
- opening_time#
Menu window opening animation time and you can set it to 0 if you don’t want animation of menu opening.
opening_time
is aNumericProperty
and defaults to 0.2.
- caller#
The widget object that calls the menu window.
caller
is aObjectProperty
and defaults to None.
- position#
Menu window position relative to parent element. Available options are: ‘auto’, ‘center’, ‘bottom’.
See Position for more information.
position
is aOptionProperty
and defaults to ‘auto’.
- radius#
Menu radius.
self.menu = MDDropdownMenu( radius=[24, 0, 24, 0], ..., )
radius
is aVariableListProperty
and defaults to ‘[dp(7)]’.
- elevation#
Elevation value of menu dialog.
New in version 1.0.0.
self.menu = MDDropdownMenu( elevation=4, ..., )
elevation
is anNumericProperty
and defaults to 4.
- check_position_caller(self, instance_window: WindowSDL, width: int, height: int)#
Called when the application root window is resized.
- set_menu_properties(self, interval: Union[int, float] = 0)#
Sets the size and position for the menu window.
- ajust_radius(self, interval: Union[int, float])#
Adjusts the radius of the first and last items in the menu list according to the radius that is set for the menu.
- adjust_position(self)#
Returns value ‘auto’ for the menu position if the menu position is out of screen.
- open(self)#
Animate the opening of a menu window.
- on_header_cls(self, instance_dropdown_menu, instance_user_menu_header)#
Called when a value is set to the
header_cls
parameter.
- on_touch_down(self, touch)#
Receive a touch down event.
- Parameters:
- touch:
MotionEvent
class Touch received. The touch is in parent coordinates. See
relativelayout
for a discussion on coordinate systems.
- touch:
- Returns:
bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.
- on_touch_move(self, touch)#
Receive a touch move event. The touch is in parent coordinates.
See
on_touch_down()
for more information.
- on_touch_up(self, touch)#
Receive a touch up event. The touch is in parent coordinates.
See
on_touch_down()
for more information.
- on_dismiss(self)#
Called when the menu is closed.
- dismiss(self, *args)#
Closes the menu.