Python / Kivy - kv文件调用不起作用

时间:2017-06-17 17:18:08

标签: python kivy

我正在用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()

这是我的输出

enter image description here

你可以帮我理解我错过的东西吗? 如果我在kv文件中添加一个带有描述的简单按钮,代码就能完美运行。 谢谢

2 个答案:

答案 0 :(得分:1)

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

class Test(AnchorLayout):
    pass

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

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

从.kv中删除test和anchorlayout并替换为新类的名称&#34;测试&#34;,并取消.kv文件的其余部分,以便所有内容都对齐。

答案 1 :(得分:-1)

解决了@FJSevilla删除和重新缩进的问题。