python - 向内容导航抽屉添加功能
问题描述
我正在尝试将功能添加到我的 kivymd 导航抽屉,但我找不到方法来做到这一点。我希望这些项目可以书写不同的页面。一个例子是设置项应该在点击时打开设置页面。我使用新更新的 kivymd 版本 0.103.0
这是一个示例代码
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class TestNavigationDrawer(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
icons_item = {
"folder": "My files",
"account-multiple": "Shared with me",
"star": "Starred",
"history": "Recent",
"checkbox-marked": "Shared with me",
"upload": "Upload",
}
for icon_name in icons_item.keys():
self.root.ids.content_drawer.ids.md_list.add_widget(
ItemDrawer(icon=icon_name, text=icons_item[icon_name])
)
解决方案
另一种解决方案
我是一个绝对的初学者,我的解决方案可能不是最好的,甚至不是正确的。但它对我有用。
我的 kivy、kivymd 和 python 版本:
名称:基维版本:2.0.0
名称:kivymd 版本:0.104.1
蟒蛇 3.8.5
主文件
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ListProperty
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem, MDList
class NavigationDrawer(BoxLayout):
pass
class ContentNavigationDrawer(BoxLayout):
pass
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
text_color = ListProperty((0, 0, 0, 1))
class DrawerList(ThemableBehavior, MDList):
def set_color_item(self, instance_item):
"""Called when tap on a menu item."""
# Set the color of the icon and text for the menu item.
for item in self.children:
if item.text_color == self.theme_cls.primary_color:
item.text_color = self.theme_cls.text_color
break
instance_item.text_color = self.theme_cls.primary_color
class TestNavigationDrawer(MDApp):
def build(self):
return Builder.load_file('main.kv')
def on_start(self):
icons_item = {
"home": "Home",
"folder": "My files",
"account-multiple": "Shared with me",
"star": "Starred",
"history": "Recent",
"checkbox-marked": "Shared with others",
"upload": "Upload",
}
for icon_name in icons_item.keys():
self.root.ids.content_drawer.ids.md_list.add_widget(
ItemDrawer(icon=icon_name, text=icons_item[icon_name])
)
TestNavigationDrawer().run()
主文件
# Menu item in the DrawerList list.
<ItemDrawer>:
theme_text_color: "Custom"
# on_press: self.parent.set_color_item(self)
on_press:
app.root.ids.nav_drawer.set_state("close")
app.root.ids.screen_manager.current = self.text.lower()
IconLeftWidget:
id: icon
icon: root.icon
theme_text_color: "Custom"
text_color: root.text_color
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
spacing: "8dp"
AnchorLayout:
anchor_x: "left"
size_hint_y: None
height: avatar.height
Image:
id: avatar
size_hint: None, None
size: "56dp", "56dp"
source: "data/logo/kivy-icon-256.png"
MDLabel:
text: "KivyMD library"
font_style: "Button"
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: "kivydevelopment@gmail.com"
font_style: "Caption"
size_hint_y: None
height: self.texture_size[1]
ScrollView:
DrawerList:
id: md_list
<NavigationDrawer>:
orientation: 'vertical'
MDToolbar:
title: "Navigation Drawer"
elevation: 10
left_action_items: [["menu", lambda x: app.root.ids.nav_drawer.set_state("open")]]
Widget:
Screen:
NavigationLayout:
ScreenManager:
id: screen_manager
Screen:
name: "home"
NavigationDrawer:
BoxLayout:
MDLabel:
text: "Home"
halign: "center"
Screen:
name: "my files"
NavigationDrawer:
BoxLayout:
MDLabel:
text: "My Files"
halign: "center"
Screen:
name: "shared with me"
NavigationDrawer:
BoxLayout:
MDLabel:
text: "Shared with me"
halign: "center"
Screen:
name: "starred"
NavigationDrawer:
BoxLayout:
MDLabel:
text: "Starred"
halign: "center"
Screen:
name: "recent"
NavigationDrawer:
BoxLayout:
MDLabel:
text: "Recent"
halign: "center"
Screen:
name: "shared with others"
NavigationDrawer:
BoxLayout:
MDLabel:
text: "Shared with others"
halign: "center"
Screen:
name: "upload"
NavigationDrawer:
BoxLayout:
MDLabel:
text: "Upload"
halign: "center"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
推荐阅读
- swift - Actor 'self' 只能从异步初始化器中传递 'inout'
- maven - 使用 maven 或 artifactory 查找依赖项的所有引用?
- git - 从历史记录中删除过去的 git 提交 - 非交互方式
- vba - 如何修复excel VBA中的“运行时错误3251”
- c - 信号量和共享内存已打开/O_EXCL 标志有问题
- apache-flink - Apache Flink 中带有 TumblingWindow 的水印
- laravel - 在 eloquent 查询中检索特定值
- javascript - 选择具有动态设置 data-x 属性的元素
- css - 代码沙箱和本地反应的行为不同
- react-native - 使用 React Native Webview 在 iOS 上的键盘重叠输入