简体   繁体   English

如何使用纯Python编写的Kivy在2个屏幕之间切换?

[英]How do I switch between 2 screens with Kivy written in pure python?

What I'm trying to do is create two screens, each with two buttons. 我想做的是创建两个屏幕,每个屏幕都有两个按钮。 One button switches to the other screen, and the other button just executes some code (just a print statement right now). 一个按钮切换到另一个屏幕,另一个按钮仅执行一些代码(现在只是一条打印语句)。 I can't quite figure out how I'm supposed to be tying a name to a screen, because when I try and switch screens with my button I get the error 我不太清楚应该如何将名称与屏幕绑定,因为当我尝试使用按钮切换屏幕时,我得到了错误

kivy.uix.screenmanager.ScreenManagerException: No Screen with name "screen2" kivy.uix.screenmanager.ScreenManagerException:没有名称为“ screen2”的屏幕

I tried printing out the name of the screens I created, and I just get 我尝试打印出我创建的屏幕的名称,然后我得到了

['', ''] ['','']

So I assume that the two screens I made were created, but my attempt to name them failed. 因此,我假设创建了两个屏幕,但是命名它们的尝试失败。

Here's my full code: 这是我的完整代码:

#PYTHON 3.4.4
#KIVY 1.9.1


from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen


class ScreenOne(Screen):

    def __init__(self, **kwargs):
        super().__init__()

        btn1 = Button(
            text='change screen',
            size_hint=(.5, .25),
            pos_hint={'left':0, 'top':1}
            )
        btn1.bind(on_press=self.changer)
        self.add_widget(btn1)

        other_btn1 = Button(
            text='test button',
            size_hint=(.5, .25),
            pos_hint={'right':1, 'bottom':0}
            )
        other_btn1.bind(on_press=self.test)
        self.add_widget(other_btn1)


    def changer(self,*args):

        self.manager.current = 'screen2'

    def test(self,instance):
        print('This is a test')



class ScreenTwo(Screen):

    def __init__(self, **kwargs):
        super().__init__()

        btn2 = Button(
            text='change screen',
            size_hint=(.5, .25),
            pos_hint={'left':0, 'top':1}
            )
        btn2.bind(on_press=self.changer)
        self.add_widget(btn2)


        other_btn2 = Button(
            text='test button 2',
            size_hint=(.5, .25),
            pos_hint={'right':1, 'bottom':0}
            )
        other_btn2.bind(on_press=self.test)
        self.add_widget(other_btn2)


    def changer(self,*args):
        self.manager.current = 'screen1'

    def test(self,instance):
        print('This is another test')




class TestApp(App):

    def build(self):
        sm = ScreenManager()
        sc1 = ScreenOne(name='screen1')
        sc2 = ScreenTwo(name='screen2')
        sm.add_widget(sc1)
        sm.add_widget(sc2)
        print (sm.screen_names)
        return sm


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

If you want your "kid" to act as a "parent" give him a chance to listen to the same "kwargs". 如果您希望您的“孩子”扮演“父母”的角色,请给他一个机会听同样的“孩子”。

Feed super(). 馈送super()。 init () with **kwargs and it will work. 用** kwargs 初始化 ()即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM