kivymd:如何在下拉菜单中添加图标?

Posted

技术标签:

【中文标题】kivymd:如何在下拉菜单中添加图标?【英文标题】:kivymd: How to add icon in Drop down menu? 【发布时间】:2021-12-01 14:30:56 【问题描述】:

我想在我的下拉菜单中添加一个左图标。它可以通过多种方式实现。但是我需要在下面的脚本中实现它,这是不正确的。

这是我的脚本:

from kivy.lang import Builder
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

KV = '''

MDScreen:
    MDDropDownItem:
        id: drop_item
        pos_hint: 'center_x': .5, 'center_y': .5
        text: 'Select'
        on_release: app.menu.open()
'''


class Test(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)
        d_items = ['item-1',"item-2","item-3"]
        menu_items = [
            
                "viewclass": "OneLineIconListItem",
                "text": i,
                "height": dp(40),
                "on_release": lambda x=i: self.set_item(x),
                "IconleftWidget": "icon",
                "icon":'account'
             for i in d_items
        ]
        self.menu = MDDropdownMenu(
            caller=self.screen.ids.drop_item,
            items=menu_items,
            position="center",
            width_mult=2.3,
        )
        self.menu.bind()

    def set_item(self, text_item):
        self.screen.ids.drop_item.text=text_item
        self.menu.dismiss()

    def build(self):
        return self.screen


Test().run()

图片:

您可以看到图标未显示。 如何在此下拉菜单中添加左图标?

提前致谢。

【问题讨论】:

【参考方案1】:

我想这就是你要找的。如果有效,请标记为已回答;-)

from kivy.lang import Builder
from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import StringProperty

from kivymd.uix.list import OneLineIconListItem


KV = '''

<IconListItem>

    IconLeftWidget:
        icon: root.icon

MDScreen:
    MDDropDownItem:
        id: drop_item
        pos_hint: 'center_x': .5, 'center_y': .5
        text: 'Select'
        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)
        d_items = ['item-1',"item-2","item-3"]
        menu_items = [
            
                "viewclass": "IconListItem",
                "icon":'account',
                # "height": dp(40),
                "height": dp(56), # it looks better with 56
                "text": i,
                "on_release": lambda x=i: self.set_item(x),
                # "IconleftWidget": "icon",
             for i in d_items
        ]
        self.menu = MDDropdownMenu(
            caller=self.screen.ids.drop_item,
            items=menu_items,
            position="center",
            width_mult=2.3,
        )
        self.menu.bind()

    def set_item(self, text_item):
        self.screen.ids.drop_item.text=text_item
        self.menu.dismiss()

    def build(self):
        return self.screen


Test().run()

如果有效,请标记为已回答;-)

【讨论】:

以上是关于kivymd:如何在下拉菜单中添加图标?的主要内容,如果未能解决你的问题,请参考以下文章

kivyMD:如何使用工具栏操作项创建下拉菜单?

下拉菜单 KivyMD 剂量显示

下拉菜单 KivyMD 未打开

KivyMD 带有下拉菜单的文本字段

将图标添加到引导下拉菜单项

将图标添加到 Bootstrap 下拉菜单