【问题标题】:Trying to use a class but get a NoneType error with Kivy尝试使用一个类,但在 Kivy 中出现 NoneType 错误
【发布时间】:2019-06-13 19:39:29
【问题描述】:

我正在尝试使用 Python 和 Kivy 构建我的第一个游戏。我使用 Kivy:s Pong 游戏教程作为我的游戏 https://kivy.org/doc/stable/tutorials/pong.html 的基础,并且我确实让我的船(png 文件)在背景上移动。然后我按照按钮上的 Sentex 指南https://youtu.be/CYNWK2GpwgA 并尝试将我的游戏移入菜单系统。

现在我的游戏无法运行。我得到: AttributeError:当我尝试移动我的船时,'NoneType' 对象没有属性 'center_x'。

我试图追溯问题并了解发生了什么。起初我认为错误是因为船是一个 NoneType 对象,但是当我使用 .self 时。我使用类 SpaceGame_TheGame?当我在它自己的类中使用它时,它怎么可能是 NoneType?

(我是编程新手,也是论坛的新手,我正在尽我所能,但我猜代码和帖子对你来说都是垃圾......)

感谢您的帮助!



from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty #Want my ship to be an object (right?)
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition #For menu system
from kivy.lang import Builder



#The different screens
class MainScreen(Screen):
    pass
class SpaceGame(Screen):
    pass
class Installningar(Screen):
    pass
class ScreenManagement(ScreenManager):
    pass


# The game
class SpaceShip(Widget): #The ship is a class
    pass


class SpaceGame_TheGame(Widget):
    player = ObjectProperty(None) #I want the ship to be an object that can get hit and stuff

    def on_touch_move(self,touch): #For movement
       pass
       self.player.center_x = touch.x
       self.player.center_y = touch.y


#Start up stuff
presentation = Builder.load_file("space.kv") #Presentation är bara ett namn på detta som han valde
class MainApp(App):
    def build(self):
        return presentation

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


#: kivy 1.10.1
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import Window kivy.core.window


ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    SpaceGame:
    Installningar:

<Button>:
    font_size: 20
    color: 0,1,0,1
    size_hint: 0.2, 0.15

<MainScreen>:
    name: "main"
    Button:
        on_release: app.root.current = "spacegame"
        text: "Start the game!"
        font_size: 20
        pos_hint: {"center_x": 0.5, 'center_y':0.7}

    Button:
        on_release: app.root.current = "installningar"
        text: "Settings"
        font_size: 20
        pos_hint: {"center_x": 0.5, 'center_y':0.5}

    Button:
        on_release: quit()
        text: "Quit"
        font_size: 20
        pos_hint: {"center_x": 0.5, 'center_y':0.3}


<SpaceShip>:
    canvas:
        Rectangle:
            size: 45,45
            pos: self.pos
            source: 'rymdskepp.png'


<SpaceGame>:
    name: "spacegame"
    player: space_ship

    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'bakgrund.jpg'


    FloatLayout:
        SpaceShip:
            id: space_ship
            x: root.x
            center_y: root.center_y


        SpaceGame_TheGame
            id: spacegame_thegame # an id for referring to this widget

        Button:
            text: "Back"
            pos_hint: {"right": 1, 'top':1}
            on_release: app.root.current = "main"


<Installningar>:
    name: "installningar"


    FloatLayout:
        Button:
            text: "Back"
            pos_hint: {"right": 1, 'top':1}
            on_release: app.root.current = "main"


[INFO   ] [Logger      ] Record log in C:\Users\fredr\.kivy\logs\kivy_19-06-13_28.txt
[INFO   ] [Kivy        ] v1.10.1
[INFO   ] [Python      ] v3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 NVIDIA 388.13'>
[INFO   ] [GL          ] OpenGL vendor <b'NVIDIA Corporation'>
[INFO   ] [GL          ] OpenGL renderer <b'GeForce GTX 980 Ti/PCIe/SSE2'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 NVIDIA'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Start application main loop
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "C:/Users/fredr/OneDrive - Nyköpings Enskilda Gymnasium/Matematik/Programmering/Blandat/Kivy/Mitt spel 2/main.py", line 42, in <module>
     MainApp().run()
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line 826, in run
     runTouchApp()
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 727, in mainloop
     self._mainloop()
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 460, in _mainloop
     EventLoop.idle()
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 340, in idle
     self.dispatch_input()
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 231, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1362, in on_motion
     self.dispatch('on_touch_move', me)
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1388, in on_touch_move
     if w.dispatch('on_touch_move', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\screenmanager.py", line 1196, in on_touch_move
     return super(ScreenManager, self).on_touch_move(touch)
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 471, in on_touch_move
     if child.dispatch('on_touch_move', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\relativelayout.py", line 296, in on_touch_move
     ret = super(RelativeLayout, self).on_touch_move(touch)
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 471, in on_touch_move
     if child.dispatch('on_touch_move', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\fredr\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 471, in on_touch_move
     if child.dispatch('on_touch_move', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:/Users/fredr/OneDrive - Nyköpings Enskilda Gymnasium/Matematik/Programmering/Blandat/Kivy/Mitt spel 2/main.py", line 29, in on_touch_move
     self.player.center_x = touch.x
 AttributeError: 'NoneType' object has no attribute 'center_x'

Process finished with exit code 1

I want my ship to move and don't get the error message...

【问题讨论】:

  • 一目了然,您从未将 SpaceGame_TheGame 设置为 None 以外的其他值。您的 kv 中有 SpaceGame 类的` player: space_ship`,但这是一个不同的小部件。
  • 您在代码中字面意思是 self.player = ObjectProperty(None)
  • 我如何引用它以使其工作?我怎样才能将它从无更改为某事? :) self.player = ObjectProperty(None) 部分用于 kivy.org 指南上的乒乓球比赛。不知道为什么那个游戏有效...

标签: python python-3.x kivy kivy-language


【解决方案1】:

问题 - AttributeError

     self.player.center_x = touch.x
 AttributeError: 'NoneType' object has no attribute 'center_x'

根本原因

player 定义为ObjectProperty 并在SpaceGame_TheGame(Widget) 类中初始化为None。此属性更新为保存id: space_ship 引用的SpaceShip 实例。

因此,当您的应用尝试通过player 检索center_x 时,它会引发错误,即NoneType 对象。

注意

在您的 Kivy 应用程序中,有两个唯一的 ObjectProperty,名称为 player。一个在SpaceGame_TheGame(Widget) 类中声明。另一个由 Kivy 自动创建并添加到 Screen 小部件 SpaceGame,因为该小部件没有具有给定名称 player 的属性。

解决方案

kv 文件

  • player: space_ship 从类规则&lt;SpaceGame&gt;: 移动到实例化对象下SpaceGame_TheGame:

片段 - kv 文件

<SpaceGame>:
    name: "spacegame"

    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'bakgrund.jpg'
    ...

        SpaceGame_TheGame:
            id: spacegame_thegame # an id for referring to this widget
            player: space_ship

py 文件

  • on_touch_move() 方法中,将self.player.center_x = touch.xself.player.center_y = touch.y 替换为self.player.pos = touch.pos

片段 - py 文件

class SpaceGame_TheGame(Widget):
    player = ObjectProperty(None)

    def on_touch_move(self, touch):
        self.player.pos = touch.pos

输出

猜你喜欢
  • 2016-11-03
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
相关资源
最近更新 更多