代码之家  ›  专栏  ›  技术社区  ›  James C

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

  •  0
  • James C  · 技术社区  · 6 年前

    我曾经设法让一个多屏幕程序在一个.kv文件中定义所有内容(包括屏幕)来工作。

    通过使用 root.current (在.kv文件中)或 self.root.current (在python文件中)我能够在屏幕之间切换。但是,一旦有多个带有许多小部件的屏幕,.kv文件就会变得非常大并且很难维护。

    这次我试图在单独的.kv文件中定义屏幕,但是我不能在屏幕之间切换来工作迄今为止的每次尝试都会导致错误(无效语法、未定义屏幕名称…)。

    是否有一种(或多种)在单独的.kv文件中定义的屏幕之间切换的方法? 以下是我正在使用的文件:

    主.py

    from kivy.app import App
    
    
    class MainApp(App):
        pass
    
    
    if __name__ == '__main__':
        MainApp().run()
    

    主电压:

    #: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"
    

    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"
    

    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"
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   ikolim    6 年前

    解决方案

    1. 添加 dynamic class 进入之内 screen_1.kv screen_2.kv ,例如。 <Screen1@Screen>: <Screen2@Screen>: 分别是。
    2. 实例化屏幕, Screen1: Screen2: 在里面 main.kv

    例子

    屏蔽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"
    

    屏蔽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"
    

    主电压

    #: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:
    

    输出

    Img01 - Main Screen Img02 - Screen1 Img03 - Screen2