【问题标题】:Syntax error in Kivy Pong tutorialKivy Pong 教程中的语法错误
【发布时间】:2017-09-08 05:43:16
【问题描述】:

我已经从他们的网站开始了 Kivy 教程。一直遵循起始代码,我遇到了语法错误。我已经搜索了很多答案,但没有一个不适用于我的情况。

python 文件名为“PongApp.py”:

# -*- coding: utf-8 -*-
"""
Created on Thu Sep  7 21:16:31 2017

@author: 917k
"""

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”的 .kv 文件,它与“PongApp.py”位于同一目录中。

Pong.kv:

# -*- coding: utf-8 -*-

#:kivy 1.10.0

<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"

错误信息:

pos: self.center_x = -5, 0
  ^
SyntaxError: invalid syntax

我已经阅读了 Kivy 教程网站上关于命名 .kv 文件的说明,我认为我理解正确。我怀疑某处可能存在缩进或命名错误,但我似乎找不到。

【问题讨论】:

    标签: python syntax-error kivy


    【解决方案1】:

    您的 kv 文件中存在拼写错误。替换

    pos: self.center_x = -5, 0
    

    pos: self.center_x - 5, 0
    

    示例

    pong.kv

    # -*- coding: utf-8 -*-
    
    #:kivy 1.10.0
    
    <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"
    

    PongApp.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()
    

    输出

    【讨论】:

      【解决方案2】:
      # -*- coding: utf-8 -*-
      
      #:kivy 1.10.0
      
      <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"
      

      我认为您需要在两者之间留一个空格 (pos: self.center_x = -"here"5, 0),并且我已经更新了上面的代码,所以请尝试再次运行它

      【讨论】:

      • 我会在当天晚些时候试一试,看看是否有效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      相关资源
      最近更新 更多