当我尝试绑定(bind)方法时,出现错误:
Traceback (most recent call last):
File "pygametest3.py", line 12, in <module>
render = winback.rend()
TypeError: unbound method rend() must be called with winback instance as first argument (got nothing instead)
这是代码,直到违规点:
import sys, pygame
pygame.init()
class winback:
"""Render the window"""
def rend(self):
rendsurf.fill(black)
rendsurf.blit (landsurf, (landx,landy,640,480))
screen.blit (rendsurf, (0,0,640,480))
pygame.display.flip()
render = winback.rend()
此外,如果这是显而易见的并且不值得发布/重新发布,我很抱歉。这是我第一次真正投入到 python 中,我为此工作了一整天,感觉有点傻,现在是凌晨 12 点 30 分。
最佳答案
类必须先实例化,然后才能调用它们的普通方法。
class Winback(object):
def rend(self):
...
winback = Winback()
render = winback.rend()
或者你可以使用@staticmethod
。 But make it a module-level function instead .
关于Python:未绑定(bind)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6591610/