导致属性错误的原因:'classObject'对象没有属性“_w”?

发布于 2024-10-31 09:43:29 字数 1370 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里的微风 2024-11-07 09:43:29

诸如画布之类的 tkinter 对象只是实际 tk 小部件的代理对象。属性 _w 包含真实 tk 小部件的内部名称。例如:

$ python2.5
Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter as tk
>>> root=tk.Tk()
>>> canvas = tk.Canvas(root)
>>>> print canvas._w
.8327736

如果您收到类似 object has no attribute '_w' 的错误,则意味着您以某种方式创建了一个认为它是 tk 小部件的对象实例(因此具有诸如 tag_bind),但实际上没有与其关联的 tk 小部件。

发生这种情况的一种方法是,如果您对 tkinter 小部件进行子类化,但不调用父类的 __init__ 方法。例如,执行此操作时您会遇到类似的错误(请注意我如何不在 Canvas 类上调用 __init__ ):

>>> class Board(tk.Canvas):
...     def __init__(self, *args):
...         pass         
...     
>>> board = Board()
>>> board.tag_bind("whatever","<1>", None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/Tkinter.py", line 2129, in tag_bind
return self._bind((self._w, 'bind', tagOrId),
AttributeError: Board instance has no attribute '_w'

我的猜测是您正在执行类似的操作这。您将 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:

$ python2.5
Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter as tk
>>> root=tk.Tk()
>>> canvas = tk.Canvas(root)
>>>> print canvas._w
.8327736

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 like tag_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 the Canvas class):

>>> class Board(tk.Canvas):
...     def __init__(self, *args):
...         pass         
...     
>>> board = Board()
>>> board.tag_bind("whatever","<1>", None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/Tkinter.py", line 2129, in tag_bind
return self._bind((self._w, 'bind', tagOrId),
AttributeError: Board instance has no attribute '_w'

My guess is that you're doing something like this. You're defining Board as a subclass of Canvas but you aren't calling the Canvas.__init__ method (or are calling it and ignoring any errors it throws)

童话 2024-11-07 09:43:29

作为 TKinter 初学者,我不断遇到此错误,因此这里是我如何修复该错误的方法,而不是根据 OP 对其起源的解释。

使用 lambda 绑定,我传入了一个函数声明而不是调用该函数。

layout.bind("<Return>",lambda event: self.handleInput)

AttributeError: 'layout' object has no attribute '_w'

添加手动函数调用使其工作时出错。

layout.bind("<Return>",lambda event: self.handleInput())

删除 lambda 允许函数仅使用声明按预期触发,但随后出现不同的错误。

layout.bind("<Return>",self.handleInput)

handleInput() takes 1 positional argument but 2 were given

为了解决这个问题,我向 handleInput 添加了第二个参数。也许这确实添加了一个未使用的参数,但它消除了错误。

def handleInput(self,event):
    print('works')

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.

layout.bind("<Return>",lambda event: self.handleInput)

AttributeError: 'layout' object has no attribute '_w'

Adding a manual function call made it work w.o error.

layout.bind("<Return>",lambda event: self.handleInput())

Removing the lambda allowed the function to fire as expected using only the declaration, but then there was a different error.

layout.bind("<Return>",self.handleInput)

handleInput() takes 1 positional argument but 2 were given

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.

def handleInput(self,event):
    print('works')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文