猿问

MDDropDownMenu KivyMD 的绑定按钮

我在 KivyMD 的屏幕上有一个 MDTextField。所需的过程是在 TextField 的 on_focus 事件上打开 MDDropDownMenu。此外,一旦您选择了 DropDownMenu 的某个元素,此类菜单将被关闭,并且所选文本将被添加到 MDTextField 中。


我成功地编码了该过程的第一部分。尽管如此,我似乎无法正确地绑定按钮。我没有收到错误消息,但似乎从未调用绑定函数。


这是我的 Kivy 代码:


<TravelManagerWindow>:

    MDTextField:

        id: dest_dropdown_item

        pos_hint: {"x":0, 'center_y': 0.5}

        write_tab: False

        size_hint: 1, None

        icon_right: 'arrow-down-drop-circle-outline'

        text:'Seleccionar'

        on_focus: if self.focus: root.display_dropdown()

这是我的Python代码:


class TravelManagerWindow(Screen):

    def display_dropdown(self):

        # Create the drop down menu

        menu_items = [{"text": f"Item {i}"} for i in range(5)]

        self.menu = MDDropdownMenu(

            caller=self.ids.dest_dropdown_item,

            items=menu_items,

            width_mult=4,

        )

        self.menu.open()

        self.menu.bind(on_release=self.set_item)


    def set_item(self, instance_menu, instance_menu_item):

        def set_item(interval):

            self.ids.dest_dropdown_item.text = instance_menu_item.text

            instance_menu.dismiss()

        Clock.schedule_once(set_item, 0.5)

我相信 set_item 函数甚至没有被调用,但是,menu.open() 确实正确触发。


有什么建议么?


预先非常感谢。


慕少森
浏览 159回答 3
3回答

qq_遁去的一_1

同样的问题,我有 kivymd 0.104.1我通过安装 0.104.2dev 解决了pip 安装https://github.com/kivymd/KivyMD/archive/master.zip

呼唤远方

从 master 分支安装 kivymd。pip install&nbsp;https://github.com/kivymd/KivyMD/archive/master.zip

守着一只汪

当我写这篇文章时,从 k master 分支安装 kivy 不仅没有解决问题,反而让问题变得更糟。编辑:找到非主分支安装的修复self.menu_items=[{"text": f"{item}"} for item in self.itens]self.menu = MDDropdownMenu(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caller=self.nome,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items=self.menu_items,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position="bottom",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width_mult=4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Here you'll put that bind func&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback = self.function&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp;def function(self,instance):&nbsp; &nbsp; #Prints the text of the menu item instance&nbsp; &nbsp; print(instance.text)
随时随地看视频慕课网APP

相关分类

Python
我要回答