【问题标题】:Python / Kivy - kv file calling does not workPython / Kivy - kv 文件调用不起作用
【发布时间】:2017-06-17 17:18:08
【问题描述】:

我正在用 Python 和 Kivy 构建一个生命计数器。

下面是我的kv代码

#:kivy 1.0

<Test>:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        ScreenManager:
            size_hint: 1, .9
            id: _screen_manager
            Screen:
                name: 'screen1'
                BoxLayout: 
                    orientation: 'vertical'
                    padding: 50
                    BoxLayout:
                        orientation: 'horizontal'
                        Button:
                            text: "Life Counter"
                    BoxLayout:
                        orientation: 'horizontal'
                        Button:
                            text: "Player 1"
                        Button:
                            text: "Player 2"
                    BoxLayout:
                        orientation: 'horizontal'
                        Button:
                            text: "-"
                        Button:
                            text: "0"
                        Button:
                            text: "+"       
                        Button:
                            text: "-"
                        Button:
                            text: "0"
                        Button:
                            text: "+"

这是我的应用程序

import kivy
kivy.require('1.0.7')

from kivy.app import App

class TestApp(App):
    pass

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

这是我的输出

你能帮我理解我缺少什么吗? 如果我在 kv 文件中添加一个带有描述的简单按钮,则代码可以完美运行。 谢谢

【问题讨论】:

  • 删除 .kv 中的 &lt;Test&gt;: 行并重新缩进。
  • 它有效.. 谢谢!

标签: python kivy


【解决方案1】:

您的原始代码没有显示任何内容的原因是因为您在 main.py 中创建了类 TestApp() 然后调用它来运行,但是在您的 kv 文件中您有 &lt;Test&gt;: 并且所有内容都定义在那。基本上你所要做的就是创建一个如下所示的新类:

class Test(AnchorLayout):
    pass

然后在您的 TestApp 类中更改为以下内容:

class TestApp(App):
    define build(self):
    return Test()

从 .kv 中删除 test 和 anchorlayout 并替换为新类“Test”的名称,并取消缩进 .kv 文件的其余部分,以便所有内容对齐。

【讨论】:

    【解决方案2】:

    感谢@FJSevilla 删除&lt;test&gt; 并重新缩进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2017-05-03
      • 2015-08-16
      • 1970-01-01
      相关资源
      最近更新 更多