简体   繁体   English

在单独的.kv(Kivy)文件中定义的屏幕之间切换

[英]Switching between screens defined in separate .kv (Kivy) files

I once managed to get a multi-screen program working by having everything (including screens) defined in a single .kv file. 我曾经设法通过在单个.kv文件中定义所有内容(包括屏幕)来使多屏幕程序正常工作。

By using root.current (in .kv file) or self.root.current (in Python file) I was able to switch between screens. 通过使用root.current (在.kv文件中)或self.root.current (在Python文件中),我可以在屏幕之间切换。 However, the .kv file grows very large and hard to maintain once there are several screens with many widgets. 但是,一旦有多个带有许多小部件的屏幕,.kv文件就会变得非常大且难以维护。

This time I tried to define Screens in separate .kv files, but I can't get switching between screens to work. 这次,我试图在单独的.kv文件中定义屏幕,但是我无法在屏幕之间切换来工作。 Every attempt so far resulted in an error (invalid syntax, screen name not defined...). 到目前为止,每次尝试都导致错误(语法无效,屏幕名称未定义...)。

Is there a way (or ways) to switch between screens, defined in separate .kv files? 有没有一种方法可以在不同的.kv文件中定义的屏幕之间进行切换? Here are the files I am using: 这是我正在使用的文件:

main.py main.py

from kivy.app import App


class MainApp(App):
    pass


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

main.kv: main.kv:

#:include screen_1.kv
#:include screen_2.kv

#:import NoTransition kivy.uix.screenmanager.NoTransition

ScreenManager:
    transition: NoTransition()


    Screen:
        name: "main_screen"

        BoxLayout:
            orientation: "vertical"

            Label:
                text: "main screen"
            Button:
                text: "to screen 1"
                on_press: app.root.current = "screen_1"
            Button:
                text: "to screen 2"
                on_press: app.root.current = "screen_2"

screen_1.kv: screen_1.kv:

Screen:
    name: 'screen_1'

    BoxLayout:
        orientation: "vertical"

        Label:
            text: "Screen 1"
        Button:
            text: "to main screen"
            on_press: app.root.current = "main_screen"
        Button:
            text: "to screen 2"
            on_press: app.root.current = "screen_2"

screen_2.kv: screen_2.kv:

Screen:
    name: 'screen_2'

    BoxLayout:
        orientation: "vertical"

        Label:
            text: "Screen 2"
        Button:
            text: "to main screen"
            on_press: app.root.current = "main_screen"
        Button:
            text: "to screen 1"
            on_press: app.root.current = "screen_1"

Solution

  1. Add dynamic class into screen_1.kv and screen_2.kv , eg <Screen1@Screen>: and <Screen2@Screen>: respectively. 动态类添加到screen_1.kvscreen_2.kv ,例如分别为<Screen1@Screen>:<Screen2@Screen>: screen_2.kv
  2. Instantiate screens, Screen1: and Screen2: in main.kv 实例化main.kv屏幕Screen1:Screen2:

Example

screen_1.kv screen_1.kv

<Screen1@Screen>:
    name: 'screen_1'

    BoxLayout:
        orientation: "vertical"

        Label:
            text: "Screen 1"
        Button:
            text: "to main screen"
            on_press: app.root.current = "main_screen"
        Button:
            text: "to screen 2"
            on_press: app.root.current = "screen_2"

screen_2.kv screen_2.kv

<Screen2@Screen>:
    name: 'screen_2'

    BoxLayout:
        orientation: "vertical"

        Label:
            text: "Screen 2"
        Button:
            text: "to main screen"
            on_press: app.root.current = "main_screen"
        Button:
            text: "to screen 1"
            on_press: app.root.current = "screen_1"

main.kv 主.kv

#:include screen_1.kv
#:include screen_2.kv

#:import NoTransition kivy.uix.screenmanager.NoTransition


ScreenManager:
    transition: NoTransition()


    Screen:
        name: "main_screen"

        BoxLayout:
            orientation: "vertical"

            Label:
                text: "main screen"
            Button:
                text: "to screen 1"
                on_press: app.root.current = "screen_1"
            Button:
                text: "to screen 2"
                on_press: app.root.current = "screen_2"

    Screen1:

    Screen2:

Output 输出量

Img01-主屏幕 图2-屏幕1 Img03-屏幕2

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

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