<small id='nylQu'></small><noframes id='nylQu'>

    • <bdo id='nylQu'></bdo><ul id='nylQu'></ul>
    <tfoot id='nylQu'></tfoot>

      1. <i id='nylQu'><tr id='nylQu'><dt id='nylQu'><q id='nylQu'><span id='nylQu'><b id='nylQu'><form id='nylQu'><ins id='nylQu'></ins><ul id='nylQu'></ul><sub id='nylQu'></sub></form><legend id='nylQu'></legend><bdo id='nylQu'><pre id='nylQu'><center id='nylQu'></center></pre></bdo></b><th id='nylQu'></th></span></q></dt></tr></i><div id='nylQu'><tfoot id='nylQu'></tfoot><dl id='nylQu'><fieldset id='nylQu'></fieldset></dl></div>

        <legend id='nylQu'><style id='nylQu'><dir id='nylQu'><q id='nylQu'></q></dir></style></legend>
      2. kivy 官方 pong 教程:'NoneType' 对象没有属性

        时间:2023-10-09
        <legend id='5xxVC'><style id='5xxVC'><dir id='5xxVC'><q id='5xxVC'></q></dir></style></legend>

            <small id='5xxVC'></small><noframes id='5xxVC'>

            <i id='5xxVC'><tr id='5xxVC'><dt id='5xxVC'><q id='5xxVC'><span id='5xxVC'><b id='5xxVC'><form id='5xxVC'><ins id='5xxVC'></ins><ul id='5xxVC'></ul><sub id='5xxVC'></sub></form><legend id='5xxVC'></legend><bdo id='5xxVC'><pre id='5xxVC'><center id='5xxVC'></center></pre></bdo></b><th id='5xxVC'></th></span></q></dt></tr></i><div id='5xxVC'><tfoot id='5xxVC'></tfoot><dl id='5xxVC'><fieldset id='5xxVC'></fieldset></dl></div>

              <bdo id='5xxVC'></bdo><ul id='5xxVC'></ul>
                  <tbody id='5xxVC'></tbody>
                  <tfoot id='5xxVC'></tfoot>
                  本文介绍了kivy 官方 pong 教程:'NoneType' 对象没有属性 'center'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试通过本教程来学习 Kivy.我已将这是此步骤的完整代码:"之后的代码复制粘贴到 main.py 和 main.kv,如所述.尝试运行时,我得到:

                  I'm trying to learn Kivy with this tutorial. I've copy pasted the code after "Here is the entire code for this step:" to main.py and main.kv as described. When trying to run, I'm getting:

                  Traceback (most recent call last):
                    File "main.py", line 47, in <module>
                     PongApp().run()
                    File "/home/kivy/code/kivy/kivy/app.py", line 527, in run
                     root = self.build()
                    File "main.py", line 41, in build
                     game.serve_ball()
                    File "main.py", line 23, in serve_ball
                     self.ball.center = self.center
                  AttributeError: 'NoneType' object has no attribute 'center'
                  

                  我做错了什么?

                  main.kv:

                  #:kivy 1.0.9
                  
                  <PongBall>:
                      size: 50, 50 
                      canvas:
                          Ellipse:
                              pos: self.pos
                              size: self.size          
                  
                  <PongGame>:
                      ball: pong_ball
                  
                      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"
                  
                      PongBall:
                          id: pong_ball
                          center: self.parent.center
                  

                  main.py:

                  from kivy.app import App
                  from kivy.uix.widget import Widget
                  from kivy.properties import NumericProperty, ReferenceListProperty,
                      ObjectProperty
                  from kivy.vector import Vector
                  from kivy.clock import Clock
                  from random import randint
                  
                  
                  class PongBall(Widget):
                      velocity_x = NumericProperty(0)
                      velocity_y = NumericProperty(0)
                      velocity = ReferenceListProperty(velocity_x, velocity_y)
                  
                      def move(self):
                          self.pos = Vector(*self.velocity) + self.pos
                  
                  
                  class PongGame(Widget):
                      ball = ObjectProperty(None)
                  
                      def serve_ball(self):
                          self.ball.center = self.center
                          self.ball.velocity = Vector(4, 0).rotate(randint(0, 360))
                  
                      def update(self, dt):
                          self.ball.move()
                  
                          #bounce off top and bottom
                          if (self.ball.y < 0) or (self.ball.top > self.height):
                              self.ball.velocity_y *= -1
                  
                          #bounce off left and right
                          if (self.ball.x < 0) or (self.ball.right > self.width):
                              self.ball.velocity_x *= -1
                  
                  
                  class PongApp(App):
                      def build(self):
                          game = PongGame()
                          game.serve_ball()
                          Clock.schedule_interval(game.update, 1.0 / 60.0)
                          return game
                  
                  
                  if __name__ == '__main__':
                      PongApp().run()
                  

                  推荐答案

                  您的 KV 文件名称错误.它应该是pong.kv".如果 KV 文件的名称与您的应用程序的名称匹配(减去 App),则会自动使用它.

                  You have the wrong name for your KV file. It should be "pong.kv". If the name of the KV file matches the name of your app (minus App), then it will be automagically used.

                  您将您的 KV 文件命名为main.kv",它与您的应用名称PongApp"不匹配,所以魔法没有发生.您可以使用 Builder.load_file() 手动加载 KV 文件.如果您回头看教程,您会发现它要求您将 KV 文件命名为pong.kv".

                  You named your KV file "main.kv" which did not match with your app name "PongApp", so the magic did not happen. You were able to manually load the KV file by using Builder.load_file(). If you look back at the tutorial, you can see it requires you to name the KV file as "pong.kv".

                  这篇关于kivy 官方 pong 教程:'NoneType' 对象没有属性 'center'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:通过 Kivy 按钮调用不同类中的函数 下一篇:格式化 Kivy 弹出窗口以消除弹出背景

                  相关文章

                  最新文章

                  1. <i id='YgYLd'><tr id='YgYLd'><dt id='YgYLd'><q id='YgYLd'><span id='YgYLd'><b id='YgYLd'><form id='YgYLd'><ins id='YgYLd'></ins><ul id='YgYLd'></ul><sub id='YgYLd'></sub></form><legend id='YgYLd'></legend><bdo id='YgYLd'><pre id='YgYLd'><center id='YgYLd'></center></pre></bdo></b><th id='YgYLd'></th></span></q></dt></tr></i><div id='YgYLd'><tfoot id='YgYLd'></tfoot><dl id='YgYLd'><fieldset id='YgYLd'></fieldset></dl></div>
                    • <bdo id='YgYLd'></bdo><ul id='YgYLd'></ul>
                    <tfoot id='YgYLd'></tfoot>

                    <small id='YgYLd'></small><noframes id='YgYLd'>

                    1. <legend id='YgYLd'><style id='YgYLd'><dir id='YgYLd'><q id='YgYLd'></q></dir></style></legend>