I am trying to create a scrollable list of dynamically generated (there is no known number before starting the app) MDDropdownMenus.
I have noticed that once the number of dropdown menus exceeds the space available in the window and the scrollview is necessary to reach some of the dynamically created widgets, the starting location of the opening animation slips away from the caller button’s center
. The further “up” the widget is from the bottom of the list, the further “up” the starting position of the dropdown animation begins.
I have traced the problem towards the caller’s center
property, which is calculated from the bottom of the list and gets used as the MDDropdownMenu’s animation origin calculation. Unfortunately, since the widgets reside inside of a scrollview, the origin in this case cannot be the caller’s center
property, but should rather be the processed via the to_window()
method first (as the dropdown’s origin should be within the currently visible window area, specifically at the position where the caller is right now in relation to the window).
I have tried to cut down my app to a minimum viable example here:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivymd.uix.button import MDRectangleFlatButton
from kivy.clock import Clock
from kivymd.uix.label import MDLabel
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.gridlayout import MDGridLayout
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import StringProperty # noqa: F403, F401
KV = '''
ScreenManager:
StatementsScreen:
<StatementItem>:
cols: 3
rows: 1
spacing: dp(20)
padding: dp(20)
size_hint_y: None
<StatementTextLabel>:
halign: 'left'
text_size: self.width, None
height: self.texture_size[1]
padding: dp(0), dp(0), dp(0), dp(6)
<StatementDropDown>:
on_release: root.open_menu()
<TopBar>:
orientation: "vertical"
MDTopAppBar:
title: root.text
<StatementsScreen>:
TopBar:
text: "Statements:"
MDGridLayout:
cols: 1
rows: 3
pos_hint: {'center_x':0.5, 'center_y':0.5}
size_hint_x: 0.95
size_hint_y: 1
ScrollView:
id: statements_scrollview
size_hint_x: 1
size_hint_y: 0.8
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
do_scroll_x: False
MDGridLayout:
cols: 1
size_hint_y: None
height: self.minimum_height
id: test_statements
'''
class TopBar(MDBoxLayout):
text = StringProperty()
class StatementTextLabel(MDLabel):
pass
class StatementDropDown(MDRectangleFlatButton):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self._do_setup)
def _do_setup(self, _):
menu_items = [
{
"text": f"choice {i}",
"viewclass": "OneLineListItem",
"on_release": lambda x=i: self.select_item_from_dropdown(x),
} for i in range(3)
]
self.dropdown = MDDropdownMenu(
caller=self,
items=menu_items,
)
self.bind(on_select=lambda instance, x: setattr(self, 'text', x))
def open_menu(self):
self.dropdown.open()
def select_item_from_dropdown(self, item_number):
print(item_number)
setattr(self, 'text', f"choice {item_number}")
self.dropdown.dismiss()
class StatementItem(MDGridLayout):
text = StringProperty()
def __init__(self, *args, text: str = None, **kwargs):
super().__init__(*args, **kwargs)
self.text = text
Clock.schedule_once(self._do_setup)
def _do_setup(self, _):
text_label = StatementTextLabel(
text=f'{self.text}',
size_hint_y=None,
)
text_label.bind(texture_size=text_label.setter('size'))
self.add_widget(text_label)
self.dropdown_button = StatementDropDown(text='select')
self.add_widget(self.dropdown_button)
class StatementsScreen(Screen):
def __init__(self, **kw):
super().__init__(**kw)
Clock.schedule_once(self._do_setup)
def _do_setup(self, _):
for number in range(1, 20):
statement = StatementItem(
text=f"statement {number}",
size_hint_y=None,
)
statement.bind(minimum_height=statement.setter('height'))
self.ids.test_statements.add_widget(statement)
class TestApp(MDApp):
def build(self):
app = Builder.load_string(KV)
return app
TestApp().run()
I am using kivy version 2.3.0 and kivymd version 1.2.0 with python 3.12.3.
When I click on the upper buttons (statement 1
to statement 12
), the corresponding dropdown menu seems to “fly” in from ever further above. When I click on any of the lower buttons (from statement 13
to the end of the list) the dropdown opens as expected.
I have tried to bind the MDDropdownMenu to the caller’s center
property but that results in the caller (the button) to be pushed way down (because it is inside the context of the scrollview’s coordinate system and not in the window’s coordinate system).
I have tried to bind the MDDropdownMenu to a special object that in turn is itself bound to the caller’s center
property, but to do that I would need to set the bind target to something like self.to_window(*self.pos)
and I have no idea how I would have to do that…
How do I get the MDDropdownMenu’s animation origin to be bound to the window’s coordinate system and not to the scrollview’s coordinates?
Best regards
frizzy jackalope is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.