从main.kv文件KivyMD获取ID

时间:2020-07-19 11:51:25

标签: python object kivy kivy-language kivymd

这可能有一个简单的解决方案,但我看不出来。以下面的代码为参考

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


main.kv

<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的print语句返回一个空的dict

1 个答案:

答案 0 :(得分:1)

ids文件中的kv在创建它们的规则的根目录中,因此nav_drawer idNavigationLayout的实例中,而不是HomeScreen。另外,ids__init__()方法中不可用,因为尚未创建它们。您可以使用idsClock.schedule_once()方法来延迟对on_kv_post()的访问。

您可以使用类似的方式访问ids中的App

App.get_running_app().root.ids['nav_drawer']