Python 在单独的.kv(Kivy)文件中定义的屏幕之间切换
我曾经通过在一个.kv文件中定义所有内容(包括屏幕)来实现多屏幕程序 通过使用Python 在单独的.kv(Kivy)文件中定义的屏幕之间切换,python,kivy,screen,Python,Kivy,Screen,我曾经通过在一个.kv文件中定义所有内容(包括屏幕)来实现多屏幕程序 通过使用root.current(在.kv文件中)或self.root.current(在Python文件中),我能够在屏幕之间切换。但是,一旦有多个屏幕上有许多小部件,.kv文件就会变得非常大,很难维护 这次我试图在单独的.kv文件中定义屏幕,但我无法在屏幕之间切换。到目前为止,每次尝试都会导致错误(语法无效,未定义屏幕名称…) 是否有一种(或多种)方法可以在单独的.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:
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:
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.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:
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:
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"
屏蔽2.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:
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:
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.kv
和屏幕2.kv
,例如:
和:
main.kv
谢谢,此解决方案有效。为了将来的参考,你能告诉我资源/文档,其中解释了这项技术。因此,我将了解它是如何工作的,并能在下次自己找到类似问题的解决方案。谢谢,不客气。更新后的文章与链接。注意。我相信我已经这样做了,还是因为某种原因没有这样做?