[英]where does this object come from
我是一个相对较新的程序员,并且我正在开发游戏。 我正在使用我以前的项目中运行良好的一些代码。 但是现在,当我尝试调用某个我不认为需要任何参数的函数时,它将返回一些奇怪的错误。
我有从以前的项目中复制的此类:
import pyglet as p
class Button(object):
def __init__(self, image, x, y, text, on_clicked):
self._width = image.width
self._height = image.height
self._sprite = p.sprite.Sprite(image, x, y)
self._label = p.text.Label(text,
font_name='Times New Roman',
font_size=20,
x=x + 20, y=y + 15,
anchor_x='center',
anchor_y='center')
self._on_clicked = on_clicked # action executed when button is clicked
def contains(self, x, y):
return (x >= self._sprite.x - self._width // 2
and x < self._sprite.x + self._width // 2
and y >= self._sprite.y - self._height // 2
and y < self._sprite.y + self._height // 2)
def clicked(self, x, y):
if self.contains(x, y):
self._on_clicked(self)
def draw(self):
self._sprite.draw()
self._label.draw()
我有调用函数的窗口事件(w是窗口):
@w.event
def on_mouse_press(x, y, button, modifiers):
for button in tiles:
button.clicked(x, y)
以及它调用的函数的三个变体,每个变体都有不同的“错误”:
def phfunc(a):
print(a)
返回以下内容: <Button.Button object at 0x0707C350>
def phfunc(a):
print('a')
返回:实际应
def phfunc():
print('a')
返回一长串的回调,导致以下结果:
File "C:\Google Drive\game programmeren\main.py", line 15, in on_mouse_press
button.clicked(x, y)
File "C:\Google Drive\game programmeren\Button.py", line 25, in clicked
self._on_clicked(self)
TypeError: phfunc() takes no arguments (1 given)
我最好的猜测是它具有的参数是Button类的self。 这是正确的,我应该为此担心吗?
您以self
为参数调用self._on_clicked
存储的函数引用。 self
是Button
类的实例:
self._on_clicked(self)
自定义Button
类的默认表示是<Button.Button object at 0x0707C350>
。
既然您明确地这样做了,那就不用担心了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.