What is the correct way to initialize a Kivy Screen?

瘦欲@ 提交于 2020-01-07 06:46:07

问题


I am using Kivy with a .kv file. This is what my Python code looks like:

class WelconeScreen(Screen):
    def __init__(self, **kwargs):
        self.name='home'
        super(Screen,self).__init__(**kwargs)

class QuestionScreen(Screen):
    def __init__(self, **kwargs):
        self.name='question'
        super(Screen,self).__init__(**kwargs)

class RootScreen(ScreenManager):
    pass

class TestApp(App):
   def build(self):
        return RootScreen()

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

And this is my .kv file:

<RootScreen>:
    WelcomeScreen:
    QuestionScreen:

<WelcomeScreen>:
    Button:
        text: 'Download DB'
<QuestionScreen>:
    BoxLayout:
        Button:
            text: 'My settings button'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'home'

Is this the correct way to initialize a Screen with Kivy? This works, but I am not sure the constructor is the right way to do it.


回答1:


name is a kivy property, so you probably want to intialise it after calling super, not before.

You can also set it in kv instead, then you don't have to define an __init__ just for this:

<WelcomeScreen>:
    name: 'home'
    Button:
        text: 'Download DB'



回答2:


You write : class WelconeScreen

But it's : class WelcomeScreen



来源:https://stackoverflow.com/questions/28527542/what-is-the-correct-way-to-initialize-a-kivy-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!