我正在尝试将图标与 menu_items 列表一起使用 我一事无成。我尝试过不同的
viewclass
es
(OneLineListItem
、OneLineAvatarIconListItem
和OneLineIconListItem
)
和各种键(“icon”,“leading_icon”,“trailing_icon”,“left_icon”)
但它们似乎都不起作用。不过有了OneLineIconListItem
,
文字稍微向右移动,就像它正在做的那样
图标有空间,但图标不显示。
这是我的代码:
kv = """
Screen:
MDIconButton:
id: button
icon: "dots-vertical"
pos_hint: {"x": .2, "y": .8}
_no_ripple_effect: True
on_release: app.dropdown.open()
"""
class DemoApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.item_height = 50
self.screen = Builder.load_string(kv)
menu_items = [
{
"viewclass": "OneLineIconListItem",
"icon": "language-python",
"text": "Set Off",
# one of the many attempts
# "on_release": lambda x = f"Set {'Off' if text.endswith('On') else 'On'}!": self.callback(x),
# another attempt at accessing "text"
# "on_release": lambda item, message="Game Saved!": self.callback(item["text"], message),
"height": dp(self.item_height),
"divider": None
}
]
self.dropdown = MDDropdownMenu(
items = menu_items,
width_mult = 4,
caller = self.screen.ids.button,
max_height = len(menu_items) * dp(self.item_height),
)
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return self.screen
def callback(self, *args):
print(args[0])
# for item in menu_items:
# if item['text'] == 'Set Off':
# print(args[0])
# item['text'] = 'Set On'
# elif item['text'] == 'Set On':
# print(args[0])
# item['text'] = 'Set Off'
# else:
# print(args[0])
DemoApp().run()
另外,请注意,我正在尝试访问和更新“文本”键 没有成功。如果我按下拉项,我想更改它 文本从“设置关闭”更改为“设置打开”,反之亦然。
任何帮助将不胜感激。谢谢。
您不能直接在
icon
中使用 OneLineIconListItem
属性。 文档中的示例显示:
OneLineIconListItem:
text: "Single-line item with avatar"
IconLeftWidget:
icon: "language-python"
因此
icon
必须用作下属 IconLeftWidget
的属性。解决方法是创建您自己的 OneLineIconListItem
的自定义版本,可能命名为 MyOneLineIconListItem
:
class MyOneLineIconListItem(OneLineIconListItem):
icon = StringProperty('') # property to handle the icon
然后在您的
kv
中,您可以包含新课程的规则:
<MyOneLineIconListItem>:
IconLeftWidget:
icon: root.icon
然后只需使用
MyOneLineIconListItem
作为菜单项中的 viewclass
:
"viewclass": "MyOneLineIconListItem",