我尝试使用Settings
屏幕创建应用程序(在ScreenManager
中)。如何将Settings
放入Screen
,以便将Screen
放入ScreenManager
,以便Screen
导航到Settings
实例被渲染,然后被隐藏,导航离开它?
答案 0 :(得分:0)
以下是如何使用2个屏幕的screenmanager的基本示例。
from kivy.app import App
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class ScreenOne(Screen):
def __init__(self,**kwargs):
super(ScreenOne,self).__init__(**kwargs)
self.box = BoxLayout(orientation='vertical')
self.label1 = Label(text="One label on screen 1")
self.label2 = Label(text="Another label label")
self.button = Button(text="Go to screen 2")
self.button.bind(on_press=self.change_screen)
self.box.add_widget(self.label1)
self.box.add_widget(self.label2)
self.box.add_widget(self.button)
self.add_widget(self.box)
def change_screen(self,*args):
self.manager.current = 'screen2'
class ScreenTwo(Screen):
def __init__(self,**kwargs):
super (ScreenTwo,self).__init__(**kwargs)
self.box = BoxLayout(orientation='vertical')
self.label1 = Label(text="One label on screen 2")
self.label2 = Label(text="Another label label")
self.button = Button(text="Go to screen 1")
self.button.bind(on_press=self.change_screen)
self.box.add_widget(self.label1)
self.box.add_widget(self.label2)
self.box.add_widget(self.button)
self.add_widget(self.box)
def change_screen(self,*args):
self.manager.current = 'screen1'
class MyApp(App):
def __init__(self,**kwargs):
super(MyApp,self).__init__(**kwargs)
self.sm = ScreenManager()
self.sm.add_widget(ScreenOne(name = "screen1"))
self.sm.add_widget(ScreenTwo(name = "screen2"))
def build(self):
return self.sm
if __name__ == "__main__":
MyApp().run()