使用Kivy的设置作为屏幕

我正在尝试使用 Settings 屏幕创建一个应用程序(在 ScreenManager 中) . 如何将 Settings 放入 Screen 所以我可以将 Screen 放入 ScreenManager 所以当 Screen 被导航到 Settings 实例时会被渲染,然后被隐藏它被导航远离它?

回答(1)

2 years ago

这是一个如何使用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()