Kivy - 尝试使用.kv文件时出错

时间:2017-01-26 08:33:40

标签: python kivy

我刚刚开始学习kivy,当我运行此代码时,我无法获得黑色窗口。

这是我的代码段:

main.py

from kivy.app import App
from kivy.uix.label import Label
from kivy.lang import Builder

Builder.load_file("awesome.kv")

class MyLabel(Label): pass

class AwesomeApp(App):
    def build(self):
        return MyLabel()

if __name__ == "__main__":
    AwesomeApp().run()

我的 awesome.kv 文件是:

<MyLabel>:
    text : "Hello!!"

在控制台上收到此消息:

[INFO              ] [Logger      ] Record log in /home/cyka/.kivy/logs/kivy_17-01-26_41.txt
[INFO              ] [Kivy        ] v1.9.1
[INFO              ] [Python      ] v3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609]
[INFO              ] [Factory     ] 179 symbols loaded
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_gif, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO              ] [Text        ] Provider: sdl2
 Traceback (most recent call last):
   File "/home/cyka/Documents/main.py", line 5, in <module>
     Builder.load_file("awesome.kv")
   File "/usr/lib/python3/dist-packages/kivy/lang.py", line 1842, in load_file
     return self.load_string(data, **kwargs)
   File "/usr/lib/python3/dist-packages/kivy/lang.py", line 1889, in load_string
     parser = Parser(content=string, filename=fn)
   File "/usr/lib/python3/dist-packages/kivy/lang.py", line 1264, in __init__
     self.parse(content)
   File "/usr/lib/python3/dist-packages/kivy/lang.py", line 1366, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "/usr/lib/python3/dist-packages/kivy/lang.py", line 1480, in parse_level
     'Invalid property name')
 kivy.lang.ParserException: Parser: File "awesome.kv", line 2:
 ...
       1:<MyLabel>:
 >>    2:    text : "Hello!!"
 ...
 Invalid property name
>>> 

此外,当我使用字符串构建时,它完全正常。

from kivy.app import App
from kivy.uix.label import Label
from kivy.lang import Builder

Builder.load_string('''
<MyLabel>:
    text: 'Hello World'
''')

class MyLabel(Label): pass    
class AwesomeApp(App):
    def build(self):
        return MyLabel()

if __name__ == "__main__":
    AwesomeApp().run()

任何修复或帮助,以便为什么会发生这种情况将非常感激。 谢谢。

0 个答案:

没有答案