问题
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