【问题标题】:Move between screens (Python 3.6, Kivy 1.9.1)在屏幕之间移动(Python 3.6、Kivy 1.9.1)
【发布时间】:2017-03-01 10:27:53
【问题描述】:

我需要在屏幕之间移动方面的帮助。我需要 5 个屏幕(MainWindow、Button1、Button2、Button3、Button4)从 MainWindow 移动到 Button 屏幕,从按钮屏幕移回 MainWindow。当我按下 4 个按钮之一时出现问题,控制台返回错误。

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.core.window import Window

Window.size = (400,650)


class MainWindow(FloatLayout):
    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)

        # Main Label
        label_position = AnchorLayout(anchor_x='center',
                                      anchor_y='top')
        label_settings = Label(text='> > > MyOrganiser MainWindow < < <',
                               size=(200, 50),
                               size_hint=(None, None))
        label_position.add_widget(label_settings)
        self.add_widget(label_position)

        # Main Menu button 1
        button_position = AnchorLayout(anchor_x='left',
                                       anchor_y='center')
        button_settings = Button(text='button1',
                                 size=(200, 200),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=Button1)

        # Main Menu button 2
        button_position = AnchorLayout(anchor_x='right',
                                       anchor_y='center')
        button_settings = Button(text='button2',
                                 size=(200, 200),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=Button2)

        # Main Menu button 3
        button_position = AnchorLayout(anchor_x='left',
                                       anchor_y='bottom')
        button_settings = Button(text='button3',
                                 size=(200, 200),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=Button3)

        # Main Menu button 4
        button_position = AnchorLayout(anchor_x='right',
                                       anchor_y='bottom')
        button_settings = Button(text='button4',
                                 size=(200, 200),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=Button4)


class Button1(FloatLayout):
    def __init__(self, **kwargs):
        super(Button1, self).__init__(**kwargs)

        # Window Button1 Label
        label_position = AnchorLayout(anchor_x='center',
                                      anchor_y='top')
        label_settings = Label(text='> > > MyOrganiser Button1 < < <',
                               size=(200, 50),
                               size_hint=(None, None))
        label_position.add_widget(label_settings)
        self.add_widget(label_position)

        # back button for Button1 window
        button_position = AnchorLayout(anchor_x='left',
                                       anchor_y='bottom')
        button_settings = Button(text='back',
                                 size=(100, 50),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=MainWindow)


class Button2(FloatLayout):
    def __init__(self, **kwargs):
        super(Button2, self).__init__(**kwargs)

        # Window Button2 Label
        label_position = AnchorLayout(anchor_x='center',
                                      anchor_y='top')
        label_settings = Label(text='> > > MyOrganiser Button2 < < <',
                               size=(200, 50),
                               size_hint=(None, None))
        label_position.add_widget(label_settings)
        self.add_widget(label_position)

        # back button for Button2 window
        button_position = AnchorLayout(anchor_x='left',
                                       anchor_y='bottom')
        button_settings = Button(text='back',
                                 size=(100, 50),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=MainWindow)


class Button3(FloatLayout):
    def __init__(self, **kwargs):
        super(Button3, self).__init__(**kwargs)

        # Window Button3 Label
        label_position = AnchorLayout(anchor_x='center',
                                      anchor_y='top')
        label_settings = Label(text='> > > MyOrganiser Button3 < < <',
                               size=(200, 50),
                               size_hint=(None, None))
        label_position.add_widget(label_settings)
        self.add_widget(label_position)

        # back button for Button3 window
        button_position = AnchorLayout(anchor_x='left',
                                       anchor_y='bottom')
        button_settings = Button(text='back',
                                 size=(100, 50),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=MainWindow)


class Button4(FloatLayout):
    def __init__(self, **kwargs):
        super(Button4, self).__init__(**kwargs)

        # Window Button4 Label
        label_position = AnchorLayout(anchor_x='center',
                                      anchor_y='top')
        label_settings = Label(text='> > > MyOrganiser Button4 < < <',
                               size=(200, 50),
                               size_hint=(None, None))
        label_position.add_widget(label_settings)
        self.add_widget(label_position)

        # back button for Button4 window
        button_position = AnchorLayout(anchor_x='left',
                                       anchor_y='bottom')
        button_settings = Button(text='back',
                                 size=(100, 50),
                                 size_hint=(None, None))
        button_position.add_widget(button_settings)
        self.add_widget(button_position)

        button_settings.bind(on_press=MainWindow)


class MyOrganiser(App):
    def build(self):
        return MainWindow()

if __name__ == '__main__':
    MyOrganiser().run()

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    您可以为此使用 ScreenManager 和 Screen。
    一个例子:

    from kivy.app import App
    from kivy.uix.screenmanager import Screen
    from kivy.lang import Builder
    
    
    class MainWindow(Screen):
        pass
    
    class Button1(Screen):
        pass
    
    
    root = Builder.load_string('''
    
    <MainWindow>:
        name: "mainwindow"
        FloatLayout:
            Button:
                text: "Goto Button1"
                on_release: root.manager.current = "button1"
    <Button1>:
        name: "button1"
        FloatLayout:
            Button:
                text: "Goto MainWindow"
                on_release: root.manager.current = "mainwindow"
    
    ScreenManager:
        MainWindow:
        Button1:
    
    ''')
    
    
    
    class MyApp(App):
        def build(self):
            return root
    
    
    MyApp().run()
    

    根据 OP 的要求,在仅 Python 的版本中:

    from kivy.app import App
    from kivy.uix.screenmanager import Screen, ScreenManager
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.button import Button
    
    
    class MainWindow(Screen):
    
        def __init__(self,**kwargs):
            super(MainWindow,self).__init__(**kwargs)
    
            self.name = "mainwindow"
            self.fl = FloatLayout()
            self.button = Button(text="Goto Button1")
            self.button.bind(on_release=self.goto)
    
            self.fl.add_widget(self.button)
            self.add_widget(self.fl)
    
        def goto(self,*args):
            self.manager.current = "button1"
    
    
    
    class Button1(Screen):
    
        def __init__(self,**kwargs):
            super(Button1,self).__init__(**kwargs)
    
            self.name = "button1"
            self.fl = FloatLayout()
            self.button = Button(text="Goto MainWindow")
            self.button.bind(on_release=self.goto)
    
            self.fl.add_widget(self.button)
            self.add_widget(self.fl)
    
        def goto(self,*args):
            self.manager.current = "mainwindow"
    
    
    
    class MyApp(App):
        def build(self):
            sm = ScreenManager()
            sm.add_widget(MainWindow())
            sm.add_widget(Button1())
            return sm
    
    MyApp().run()
    

    【讨论】:

    • 也许你有没有 kivy 语言的解决方案?
    • @MarcinKoprek 是的,它几乎是一样的。但我会更新它,添加仅 python 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    相关资源
    最近更新 更多