kv文件:声明后无效的数据

时间:2018-07-05 10:37:26

标签: android python kivy

我在win8上使用python 3.6.5。我正在关注本(https://kivy.org/docs/tutorials/pong.html)教程。这种情况: 我有 main.py

from kivy.app import App
from kivy.uix.widget import Widget


class PongGame(Widget):
    pass


class PongApp(App):
    def build(self):
        return PongGame()

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

pong.kv

#:kivy 1.10.1

<PongGame>:
    canvas:
        Rectangle:
            pos: self.center_x - 5, 0
            size: 10, self.height

    Label:
        font_size: 70
        center_x: root.width / 4
        top: root.top - 50
        text: "0"

    Label:
        font_size: 70
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: "0"

打开main.py时,出现以下错误:

[INFO   ] [Logger      ] Record log in C:\Users\Lorenzo\.kivy\logs\kivy_18-07-05_3.txt
[INFO   ] [Kivy        ] v1.10.0
[INFO   ] [Python      ] v3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSCv.1900 64 bit (AMD64)]
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
Traceback (most recent call last):
File "main.py", line 15, in <module>
  PongApp().run()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\kivy\app.py", line 801, in run
  self.load_kv(filename=self.kv_file)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\kivy\app.py", line 598, in load_kv
  root = Builder.load_file(rfilename)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\kivy\lang\builder.py", line 301, in load_file
  return self.load_string(data, **kwargs)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\kivy\lang\builder.py", line 350, in load_string
  parser = Parser(content=string, filename=fn)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\kivy\lang\parser.py", line 392, in __init__
  self.parse(content)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\kivy\lang\parser.py", line 501, in parse
  objects, remaining_lines = self.parse_level(0, lines)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\kivy\lang\parser.py", line 575, in parse_level
 'Invalid data after declaration')
kivy.lang.parser.ParserException: Parser: File "C:\Users\Lorenzo\Documents\app-kivy\pong.kv", line 1:
 ...
       1:#:kivy 1.10.1
       2:
       3:PongGame:
 ...
 Invalid data after declaration

我还没有在堆栈上找到解决方案。

你能帮我吗?

编辑:我解决了。问题是utf8编码[您可以在这里看到:“(...)1:#:kivy 1.10.1(...)”]。所以我使用ANSI编码保存了我的kv文件,

2 个答案:

答案 0 :(得分:0)

我解决了。问题是utf8编码[您可以在这里看到:“(...)1:#:kivy 1.10.1(...)”]。所以我使用ANSI编码保存了我的kv文件,

答案 1 :(得分:0)

我也收到此错误。这不完全是您的情况,但可能对您或其他人有帮助。在我的情况下,由于数据声明是AFTER规则声明而发生的错误。

错误:

top_menu: top_menu
TopMenu:
    id: top_menu

screen_manager: screen_manager
ScreenManager:
    id: screen_manager
    transition:
        SlideTransition(direction='down')
    Screen:
        name: 'MainPageScreen'  
        MainPageScreen
    Screen:
        name: 'MenuScreen'  
        MenuScreen

右:

top_menu: top_menu
screen_manager: screen_manager

TopMenu:
    id: top_menu

ScreenManager:
    id: screen_manager
    transition:
        SlideTransition(direction='down')
    Screen:
        name: 'MainPageScreen'
        MainPageScreen
    Screen:
        name: 'MenuScreen'
        MenuScreen

由于“ x = content.split(':',1)”行中的冒号“:”,python似乎将“ screen_manager:screen_manager”行解析为规则声明的一部分。这是python \ Lib \ site-packages \ kivy \ lang \ parser.py(ln 563)中的代码段:

        # Level finished
        if count < indent:
            return objects, lines[i - 1:]

        # Current level, create an object
        elif count == indent:
            x = content.split(':', 1)
            if not len(x[0]):
                raise ParserException(self, ln, 'Identifier missing')
            if (len(x) == 2 and len(x[1]) and
                    not x[1].lstrip().startswith('#')):
                raise ParserException(self, ln,
                                      'Invalid data after declaration')