初始化Kivy屏幕的正确方法是什么? [英] What is the correct way to initialize a Kivy Screen?
本文介绍了初始化Kivy屏幕的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将Kivy与.kv文件一起使用.这就是我的Python代码:
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()
这是我的.kv文件:
<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'
这是用Kivy初始化屏幕的正确方法吗?这行得通,但是我不确定构造函数是否是正确的方法.
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.
推荐答案
name
是kivy属性,因此您可能希望在调用super之后而不是之前对其进行初始化.
name
is a kivy property, so you probably want to intialise it after calling super, not before.
您也可以在kv中进行设置,而不必为此专门定义__init__
:
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'
这篇关于初始化Kivy屏幕的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文