KivyMD MDNavigationRail,按图标

时间:2020-11-09 17:35:20

标签: python kivy kivy-language kivymd

我试图在KivyMD中使用MDNavigationRail的新功能,并希望为其提供图标功能。目的是用户可以通过按代表所需屏幕的图标来切换到所需屏幕。我给了图标on_press。但是出了点问题,我得到了一个错误; ValueError: MDNavigationRail.state is set to an invalid option 'down'. Must be one of: ['close', 'open']。我猜应该将滑轨打开或关闭,难道不能给它一个功能吗?此外,我想知道是否有可能不破坏文本。如果有人可以帮助我,那就太好了!

我的.py文件

from kivy.uix.screenmanager import ScreenManager

from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen


class Screen1(MDScreen):
    def screen2(self):
        self.manager.current = 'screen2'


class Screen2(MDScreen):
    def screen1(self):
        self.manager.current = 'screen1'

    def rail_open(self):
        if self.ids.rail.state == "open":
            self.ids.rail.state = "close"
        else:
            self.ids.rail.state = "open"


class MyScreenManager(ScreenManager):
    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)


class Test(MDApp):
    def build(self):
        return MyScreenManager()

Test().run()

我的kv文件

<MyScreenManager>:
    Screen1:
        id: screen1
        name: 'screen1'
    Screen2:
        id: screen2
        name: 'screen2'

<Screen1>:
    id: screen1
    MDFloatLayout:
        MDRectangleFlatButton:
            text: "Change to screen 2"
            on_press: root.screen2()
            pos_hint: {'center_x':0.5, 'center_y':0.5}

<Screen2>:
    id: screen2
    MDBoxLayout:
        orientation: "vertical"
        MDToolbar:
            left_action_items: [["menu", lambda x: root.rail_open()]]
        MDBoxLayout:
            MDNavigationRail:
                id: rail
                elevation: 1
                use_resizeable: True
                MDNavigationRailItem:
                    icon: "home"
                    text: "homepage"
                    on_press: root.screen1()
                MDNavigationRailItem:
                    icon: ""
                    text: ""
            MDFloatLayout:
                MDTextField:
                    id: field1
                    hint_text: "Enter something:"
                    size_hint_x: 0.4
                    pos_hint: {'center_x':0.25,'top':0.8}

1 个答案:

答案 0 :(得分:1)