简体   繁体   English

Kivy - 如何在不同文件中分屏?

[英]Kivy - how split screens in different files?

i have problem with including kv file to another one.我在将 kv 文件包含到另一个文件时遇到问题。 I wanna have separate files for each screen and include them in 'main.kv', but i have a problem with accesign id/components from splitted file.我想为每个屏幕创建单独的文件并将它们包含在“main.kv”中,但我对拆分文件中的 accesign id/components 有疑问。

See error:查看错误:

File "screen_home.kv", line 9, in left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]] NameError: name 'nav_drawer' is not defined文件“screen_home.kv”,第 9 行,在 left_action_items 中:[['menu', lambda x: nav_drawer.set_state("open")]] NameError: name 'nav_drawer' is not defined

See included files:查看包含的文件:

main.py主程序

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout


class ContentNavigationDrawer(BoxLayout):
    pass


class mainApp(MDApp):
    title = "mainApp"
    app_name = title

    def build(self):
        return Builder.load_file("main.kv")


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

main.kv主.kv

#:include screen_home.kv

<ContentNavigationDrawer>:
    orientation: "vertical"
    padding: "8dp"
    spacing: "8dp"
    AnchorLayout:
        anchor_x: "center"
        size_hint_y: None
        height: app_logo.height
        Image:
            id: app_logo
            size_hint: None, None
            size: "80dp", "80dp"
            source: "./res/images/logo.png"
    MDLabel:
        text: app.app_name
        halign: "center"
        font_style: "Subtitle2"
        size_hint_y: None
        height: self.texture_size[1]
    ScrollView:
        MDList:
            OneLineAvatarIconListItem:
                text: "Home"
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "screen_home"
                IconLeftWidget:
                    icon: "home"
                    valign: 'center'

            OneLineAvatarIconListItem:
                text: "Screen 1"
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "screen_1"
                IconLeftWidget:
                    icon: "scatter-plot"

Screen:
    NavigationLayout:
        ScreenManager:
            id: screen_manager
            ScreenHome:
            Screen:
                name: "screen_1"
                BoxLayout:
                    orientation: 'vertical'

                    MDToolbar:
                        title: "First Screen"
                        elevation: 10
                        left_action_items: [['menu', lambda x : nav_drawer.set_state("open")]]

                    BoxLayout:
                        orientation: 'horizontal'
                        MDLabel:
                            halign: "center"
                            valign: "center"
                            text: "Label on screen_1"

        MDNavigationDrawer:
            id: nav_drawer

            ContentNavigationDrawer:
                screen_manager: screen_manager
                nav_drawer: nav_drawer

screen_home.kv screen_home.kv

<ScreenHome@Screen>:
    name: "screen_home"
    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            title: "Home"
            elevation: 10
            left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]
        Widget:
            canvas:
                Color:
                    rgb: 0, 1, 0
                Rectangle:
                    size: (800,200)
        Widget:
            MDLabel:
                text: "Home screeen"
                size: root.width, root.height
                halign: 'center'
                valign: 'center'

So I am not 100% sure as I am dealing with this same issue.所以我不是 100% 确定,因为我正在处理同样的问题。 I am able to build the app correctly using a single "main.py" file where the kivyMD language is built in the same file.我能够使用单个“main.py”文件正确构建应用程序,其中在同一文件中构建了 kivyMD 语言。 But when I am moving into a design.kv file, I am having difficulty building the widget tree correctly to access the more complex widgets.但是当我进入 design.kv 文件时,我很难正确构建小部件树以访问更复杂的小部件。

I believe you must create a widget, as in MDScreen, to be the appliations main widget.我相信您必须创建一个小部件,如在 MDScreen 中一样,作为应用程序的主要小部件。 Then, you must add the complex widgets which you are choosing to use, IE MDNavigationDrawer.然后,您必须添加您选择使用的复杂小部件,即 IE MDNavigationDrawer。 Then further adding to the widgets, you add the screenmanager and then the individual screens.然后进一步添加到小部件,添加屏幕管理器,然后添加各个屏幕。

I am still seeking advice and trying to get this to work but I hope that helps.我仍在寻求建议并试图让它发挥作用,但我希望这会有所帮助。 If you have moved forward with your project and this issue please feel free to post.如果您已经推进了您的项目并且出现了这个问题,请随时发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM