我曾经设法通过在单个.kv文件中定义所有内容(包括屏幕)来使多屏幕程序正常工作。
通过使用import
(在.kv文件中)或root.current
(在Python文件中),我可以在屏幕之间进行切换。但是,一旦有多个窗口和许多小部件,.kv文件就会变得非常大且难以维护。
这一次,我试图在单独的.kv文件中定义屏幕,但是我无法在屏幕之间切换来工作。到目前为止,每次尝试都导致错误(语法无效,屏幕名称未定义...)。
是否有一种或多种在不同的.kv文件中定义的屏幕之间切换的方法? 这是我正在使用的文件:
main.py
self.root.current
main.kv:
from kivy.app import App
class MainApp(App):
pass
if __name__ == '__main__':
MainApp().run()
screen_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_2.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"
答案 0 :(得分:0)
screen_1.kv
和screen_2.kv
中,例如分别为<Screen1@Screen>:
和<Screen2@Screen>:
。Screen1:
中的Screen2:
和main.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"
<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: