【问题标题】:What is the correct way to initialize a Kivy Screen?初始化 Kivy 屏幕的正确方法是什么?
【发布时间】:2015-02-15 15:15:12
【问题描述】:

我正在使用带有 .kv 文件的 Kivy。这就是我的 Python 代码的样子:

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 初始化屏幕的正确方法吗?这可行,但我不确定构造函数是否正确。

【问题讨论】:

    标签: kivy


    【解决方案1】:

    name 是一个 kivy 属性,所以你可能想在调用 super 之后初始化它,而不是之前。

    您也可以将其设置为 kv,然后您不必为此定义 __init__

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

    【讨论】:

    • 这正是我想要的:在我的 .kv 文件中初始化屏幕。谢谢!
    【解决方案2】:

    你写: 类 WelconeScreen

    但它是: 类 WelcomeScreen

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多