Search code examples
pythonkivykivy-languagekivymd

KivyMD MDNavigationRail, press icons


I was trying to use a new feature in KivyMD, the MDNavigationRail and wanted to give the icons in it a function. The goal is that the user could change to the desired screen by pressing the icon that represents it. I gave the icon an on_press. But something goes wrong, I get an error; ValueError: MDNavigationRail.state is set to an invalid option 'down'. Must be one of: ['close', 'open']. The rail should be open or closed I guess, isn't it possible to give it a function? Furthermore, I would want to know if it is possible to not break the text. If anyone could help me out, it would be very nice!

My .py file

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()

My kv file

<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}

Solution

  • It was a bug. Already fixed - https://github.com/kivymd/KivyMD/commit/8a31b0f3ccad9c2d9ad35d80953f7396f2dc78f2