导致属性错误的原因:'classObject'对象没有属性“_w”?
我正在使用 Python 3.1,并且我想创建一个游戏。我做了一个类Board(Canvas):
为什么?因为我需要通过“标签”来跟踪这些片段。但是,当我尝试将标签绑定到一块时,我得到了一个回溯,内容类似于...
Traceback (most recent call last):
File "/Users/bluedragon1223/Desktop/Djambi0-2.py", line 282, in <module>
x = Board()
File "/Users/bluedragon1223/Desktop/Djambi0-2.py", line 94, in __init__
self.tag_bind(self.canM, '<1>', _onPieceClick)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py",
line 2103, in tag_bind
return self._bind((self._w, 'bind', tagOrId),
AttributeError: 'Board' object has no attribute '_w'
我可以跟踪代码一直到错误,而且我认为我不理解这里的“_w”是什么。
在 x = Board()
之后,def __init__(self, window=mainWin):
包含 self.M = PhotoImage(file=path+'M.gif')
。 self.M
然后进入:
def __draw(self):
canvas = Canvas(mainWin,width=810,height=810)
for i in range(9):
canvas.create_line(90*i,0,90*i,810)
for j in range(9):
canvas.create_line(0,90*j,810,90*j)
canvas.create_rectangle(3,810,810,3)
canvas.bind('<1>', _point2square)
canvas.pack()
self.canM = canvas.create_image(405,405,image=self.M,tag = 'M')
之后,它在 self.tag_bind(self.canM, '<1>', _onPieceClick)
中使用。这就是错误发生的地方。我的问题是为什么?我做了什么以及如何解决它?
我很感激任何形式的帮助!
I'm working with Python 3.1, and I want to create a game. I made a class Board(Canvas):
Why? because I need to keep track of the pieces via 'tag'. BUT, when I attempt to bind a tag to a piece, I get a traceback that read something like...
Traceback (most recent call last):
File "/Users/bluedragon1223/Desktop/Djambi0-2.py", line 282, in <module>
x = Board()
File "/Users/bluedragon1223/Desktop/Djambi0-2.py", line 94, in __init__
self.tag_bind(self.canM, '<1>', _onPieceClick)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py",
line 2103, in tag_bind
return self._bind((self._w, 'bind', tagOrId),
AttributeError: 'Board' object has no attribute '_w'
I can follow the code all the way to the error, and I think I lack the understanding of what '_w' is here.
After x = Board()
the def __init__(self, window=mainWin):
contains self.M = PhotoImage(file=path+'M.gif')
. self.M
then goes into:
def __draw(self):
canvas = Canvas(mainWin,width=810,height=810)
for i in range(9):
canvas.create_line(90*i,0,90*i,810)
for j in range(9):
canvas.create_line(0,90*j,810,90*j)
canvas.create_rectangle(3,810,810,3)
canvas.bind('<1>', _point2square)
canvas.pack()
self.canM = canvas.create_image(405,405,image=self.M,tag = 'M')
After that, it's used in self.tag_bind(self.canM, '<1>', _onPieceClick)
. And that's where the error occurs. My question is why? What did I do, and how do I fix it?
I appreciate any sort of help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
诸如画布之类的 tkinter 对象只是实际 tk 小部件的代理对象。属性
_w
包含真实 tk 小部件的内部名称。例如:如果您收到类似
object has no attribute '_w'
的错误,则意味着您以某种方式创建了一个认为它是 tk 小部件的对象实例(因此具有诸如tag_bind
),但实际上没有与其关联的 tk 小部件。发生这种情况的一种方法是,如果您对 tkinter 小部件进行子类化,但不调用父类的 __init__ 方法。例如,执行此操作时您会遇到类似的错误(请注意我如何不在
Canvas
类上调用__init__
):我的猜测是您正在执行类似的操作这。您将 Board 定义为 Canvas 的子类,但没有调用 Canvas.__init__ 方法(或者正在调用它并忽略它引发的任何错误)
A tkinter object such as a canvas is simply a proxy object for an actual tk widget. The attribute
_w
contains the internal name of the real tk widget. For example:If you get an error like
object has no attribute '_w'
it means that somehow you created an instance of an object that thinks it is a tk widget (and thus has methods liketag_bind
), but doesn't actually have a tk widget associated with it.One way this can happen is if you subclass a tkinter widget but don't call the
__init__
method of the parent class. For example, you get get a similar error doing this (notice how I don't call__init__
on theCanvas
class):My guess is that you're doing something like this. You're defining Board as a subclass of
Canvas
but you aren't calling theCanvas.__init__
method (or are calling it and ignoring any errors it throws)作为 TKinter 初学者,我不断遇到此错误,因此这里是我如何修复该错误的方法,而不是根据 OP 对其起源的解释。
使用 lambda 绑定,我传入了一个函数声明而不是调用该函数。
添加手动函数调用使其工作时出错。
删除 lambda 允许函数仅使用声明按预期触发,但随后出现不同的错误。
为了解决这个问题,我向
handleInput
添加了第二个参数。也许这确实添加了一个未使用的参数,但它消除了错误。I continually run into this error as a TKinter beginner so here is a how-I-fixed-the-error rather an explanation of its origins, as per OP.
Binding with a lambda, I passed in a function declaration rather than calling the function.
Adding a manual function call made it work w.o error.
Removing the lambda allowed the function to fire as expected using only the declaration, but then there was a different error.
To solve this is I added a second param to
handleInput
. This does add an unused param, maybe, but it gets rid of the error.