在单独的 .kv (Kivy) 文件中定义的屏幕之间切换 [英] Switching between screens defined in separate .kv (Kivy) files
问题描述
我曾经通过在单个 .kv 文件中定义所有内容(包括屏幕),设法使多屏幕程序工作.
通过使用 root.current
(在 .kv 文件中)或 self.root.current
(在 Python 文件中),我能够在屏幕之间切换.但是,一旦有多个带有许多小部件的屏幕,.kv 文件就会变得非常大且难以维护.
这次我尝试在单独的 .kv 文件中定义屏幕,但我无法在屏幕之间切换工作.到目前为止,每次尝试都会导致错误(语法无效,未定义屏幕名称...).
是否有一种(或多种)方法可以在不同的 .kv 文件中定义的屏幕之间切换?这是我正在使用的文件:
main.py
from kivy.app import App类 MainApp(App):经过如果 __name__ == '__main__':MainApp().run()
main.kv:
#:include screen_1.kv#:include screen_2.kv#:import NoTransition kivy.uix.screenmanager.NoTransition屏幕管理器:过渡:NoTransition()屏幕:名称:主屏幕"框布局:方向:垂直"标签:文字:主屏幕"按钮:文本:到屏幕 1"on_press: app.root.current = "screen_1"按钮:文字:到屏幕 2"on_press: app.root.current = "screen_2"
screen_1.kv:
屏幕:名称:'screen_1'框布局:方向:垂直"标签:文本:屏幕 1"按钮:文字:到主屏幕"on_press: app.root.current = "main_screen"按钮:文字:到屏幕 2"on_press: app.root.current = "screen_2"
screen_2.kv:
屏幕:名称:'screen_2'框布局:方向:垂直"标签:文本:屏幕 2"按钮:文字:到主屏幕"on_press: app.root.current = "main_screen"按钮:文本:到屏幕 1"on_press: app.root.current = "screen_1"
解决方案
- 将
I once managed to get a multi-screen program working by having everything (including screens) defined in a single .kv file.
By using
root.current
(in .kv file) orself.root.current
(in Python file) I was able to switch between screens. However, the .kv file grows very large and hard to maintain once there are several screens with many widgets.This time I tried to define Screens in separate .kv files, but I can't get switching between screens to work. 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? Here are the files I am using:
main.py
from kivy.app import App class MainApp(App): pass if __name__ == '__main__': MainApp().run()
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: 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: 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
- Add dynamic class into
screen_1.kv
andscreen_2.kv
, e.g.<Screen1@Screen>:
and<Screen2@Screen>:
respectively. - Instantiate screens,
Screen1:
andScreen2:
inmain.kv
Example
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
<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
#: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
这篇关于在单独的 .kv (Kivy) 文件中定义的屏幕之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Add dynamic class into