KivyMD: Fix opening position of multiple dynamically generated MDDropdownMenus inside of a Scrollview

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

New contributor

frizzy jackalope is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật