【发布时间】:2018-12-30 07:58:15
【问题描述】:
我曾经设法通过在单个 .kv 文件中定义所有内容(包括屏幕)来使多屏幕程序正常工作。
通过使用root.current(在.kv 文件中)或self.root.current(在Python 文件中),我能够在屏幕之间切换。但是,一旦有多个带有许多小部件的屏幕,.kv 文件就会变得非常大并且难以维护。
这一次我尝试在单独的 .kv 文件中定义屏幕,但我无法在屏幕之间切换工作。到目前为止,每次尝试都会导致错误(语法无效,未定义屏幕名称......)。
是否有一种方法(或多种方法)在不同的 .kv 文件中定义的屏幕之间切换? 以下是我正在使用的文件:
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"
【问题讨论】: