0

这可能有一个简单的解决方案,但我无法弄清楚。以以下代码为参考

from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivymd.theming import ThemeManager
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivymd.uix.navigationdrawer import NavigationLayout, MDNavigationDrawer
from kivymd.uix.list import OneLineListItem, OneLineIconListItem
from kivymd.uix.list import MDList
from kivymd.uix.toolbar import MDToolbar

class HomeScreen(Screen):
    pass

class ContentDrawer(BoxLayout): 
    screen_manager = ObjectProperty() 
    nav_drawer = ObjectProperty()

class MainApp(MDApp):
    def __init__(self):
        self.GUI = Builder.load_file("main.kv")
        self.theme_cls = ThemeManager()

    def build(self):
        return self.GUI

if __name__ == "__main__":
    MainApp().run()


主文件

<ContentDrawer>:
    ScrollView:
        MDList:
            OneLineIconListItem: 
                text: "Screen 1" 
                icon: 'basketball-hoop'
                on_press: 
                    root.nav_drawer.set_state("close") 
                    root.screen_manager.current = "add_buy_condition_screen"

            OneLineIconListItem: 
                text: "Screen 2" 
                icon: 'basketball'
                on_press: 
                    root.nav_drawer.set_state("close") 
                    root.screen_manager.current = "add_sell_condition_screen"

NavigationLayout: 
    MDNavigationDrawer: 
        id: nav_drawer
    
        ContentDrawer: 
            screen_manager: screen_manager 
            nav_drawer: nav_drawer

    ScreenManager:
        HomeScreen:
            name: "home_screen"
            id: home_screen
            BoxLayout:
                orientation:"vertical"
                MDToolbar:
                    pos_hint:{"top": 1}
                    elevation:10
                    title:"Lleno"
                    left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

所以我正在尝试创建一个工具栏,当我使用上面的代码创建它时,除了 MDNavigation 抽屉出现在工具栏下方之外,一切都很好。我想为 Homescreen 类中的工具栏创建框布局,如下所示。

class HomeScreen(Screen):
    
    def __init__(self, **kwargs):
        super(HomeScreen, self).__init__(**kwargs)
        print("My main app's ids: ", self.ids)
     
        self.nav_drawer = self.ids["nav_drawer"]

        self.header_box = BoxLayout(orientation="vertical")
        self.toolbar = MDToolbar(pos_hint={"top": 1}, elevation=10,
            title="Lleno", left_action_items= [["menu", lambda x: nav_drawer.set_state("open")]])

        self.header_box.add_widget(self.toolbar)   

        self.add_widget(self.header_box)

但是我似乎无法从我的 main.kv 文件中获取 self.nav_drawer id,我如何才能访问该 id 或者以这样的方式创建工具栏的另一种方法,以便当 MDNavigation 抽屉打开工具栏时在它下面。self.ids 的打印语句返回一个空字典 <class 'ObservableDict'>

4

1 回答 1

1

ids来自文件的kv位于创建它们的规则的根目录中,因此nav_drawer id位于 的实例中NavigationLayout,而不是HomeScreen. 此外,该方法ids中不可用__init__(),因为它们尚未创建。ids您可以通过使用Clock.schedule_once()on_kv_post()方法延迟对的访问。

您可以使用类似的东西访问ids您的App

App.get_running_app().root.ids['nav_drawer']
于 2020-07-19T13:24:20.080 回答