_tkinter.TclError:错误#args:怎么了?

发布于 2024-10-31 18:26:27 字数 2252 浏览 2 评论 0原文

还在写游戏。不过,这个错误有点不同。我得到这样的回溯...

Tkinter 回调中的异常 回溯(最近一次调用最后一次): 文件“/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py”, 第 1399 行,在 __call__ 中 返回 self.func(*args) 文件“/Users/bluedragon1223/Desktop/Djambi0-2.py”,第 68 行,位于 _newSpaceChosen pieceID = event.widget.find_withtag(currentCommand) 文件“/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinte/__init__.py”,
第 2199 行,在 find_withtag 中 返回 self.find('withtag', tagOrId) 文件“/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py”, 第 2173 行,在查找中 self.tk.call((self._w, 'find') + args)) 或 () _tkinter.TclError: 错误 # args: 应该是“.23215664 find withtag tagOrId”

我的意思是,我认为代码足够无害。

我有全局变量 currentCommand = None(CurX, CurY) = (0,0)(ToX, ToY) = (0,0) code> 首先,如果这与它有关,但主要问题是我的事件。

有两个:

def _newSpaceChosen(event):
print(event.widget.find_withtag('rN')) #Used to check if I had used find_withtag correctly
pieceID = event.widget.find_withtag(currentCommand) #This is the problem source
[CurX, CurY] = event.widget.coords(pieceID[1])
print(CurX, CurY)
[MetaToX, MetaToY] = _point2square(event.x, event.y)
print(event.x, event.y)
print(MetaToX, MetaToY)
[ToX, ToY] = _square2point(MetaToX, MetaToY)
print(ToX, ToY)
event.widget.move(pieceID, ToX - CurX, ToY - CurY)

def _onPieceClick(event):
stuffTags = event.widget.gettags(event.widget.find_closest(event.x, event.y))
try:
    event.widget.delete(event.widget.find_withtag('bbox'))
except:
    pass
bboxULX = (event.x // 90 + 1) * 90
bboxULY = (event.y // 90 + 1) * 90 
bboxLRX = (event.x // 90 + 1) * 90 - 90
bboxLRY = (event.y // 90 + 1) * 90 - 90
event.widget.create_rectangle(bboxULX,bboxULY,bboxLRX,bboxLRY, width=3,   
outline='yellow',tag='bbox')
currentCommand = stuffTags[0]
print(currentCommand)`

想法是将游戏棋子标签存储在 currentCommand 中,然后使用该值来控制特定棋子,直到棋子通过如下绑定移动:

canvas.bind('<1>', _newSpaceChosen)

在 def __init__ 中(self, mainWin): 类 Board(Canvas) 的 : 每个片段都有自己的 tag_bind(#piece-var, '<1>', _onPieceClick)

我的假设是 currentCommand 没有足够快地接收到值。 你们认为是什么原因造成了这个痕迹?

Still writing a game. This error's a little different, though. I get a trace back like this...

Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py",
line 1399, in __call__
return self.func(*args)
File "/Users/bluedragon1223/Desktop/Djambi0-2.py", line 68, in _newSpaceChosen
pieceID = event.widget.find_withtag(currentCommand)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinte/__init__.py",
line2199, in find_withtag
return self.find('withtag', tagOrId)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py",
line 2173, in find
self.tk.call((self._w, 'find') + args)) or ()
_tkinter.TclError: wrong # args: should be ".23215664 find withtag tagOrId"

I mean, I thought the code innocuous enough.

I have global variables currentCommand = None and (CurX, CurY) = (0,0) and (ToX, ToY) = (0,0) to start with, if that has something to do with it, but the main problem is my events.

There are two:

def _newSpaceChosen(event):
print(event.widget.find_withtag('rN')) #Used to check if I had used find_withtag correctly
pieceID = event.widget.find_withtag(currentCommand) #This is the problem source
[CurX, CurY] = event.widget.coords(pieceID[1])
print(CurX, CurY)
[MetaToX, MetaToY] = _point2square(event.x, event.y)
print(event.x, event.y)
print(MetaToX, MetaToY)
[ToX, ToY] = _square2point(MetaToX, MetaToY)
print(ToX, ToY)
event.widget.move(pieceID, ToX - CurX, ToY - CurY)

def _onPieceClick(event):
stuffTags = event.widget.gettags(event.widget.find_closest(event.x, event.y))
try:
    event.widget.delete(event.widget.find_withtag('bbox'))
except:
    pass
bboxULX = (event.x // 90 + 1) * 90
bboxULY = (event.y // 90 + 1) * 90 
bboxLRX = (event.x // 90 + 1) * 90 - 90
bboxLRY = (event.y // 90 + 1) * 90 - 90
event.widget.create_rectangle(bboxULX,bboxULY,bboxLRX,bboxLRY, width=3,   
outline='yellow',tag='bbox')
currentCommand = stuffTags[0]
print(currentCommand)`

The idea was to store the game piece tag in currentCommand, and then use that value to control that specific piece until the piece was moved with bindings like this:

canvas.bind('<1>', _newSpaceChosen)

in the def __init__(self, mainWin): of a class Board(Canvas):
Each piece has it's own tag_bind(#piece-var, '<1>', _onPieceClick)

My hypothesis is that currentCommand is not receiving a value soon enough.
What do you guys think causes this trace?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

花心好男孩 2024-11-07 18:26:27

你的假设几乎肯定是正确的。您可以通过在调用 find_withtag 之前打印出该值来轻松测试该假设。

Your hypothesis is almost certainly correct. You can easily test this hypothesis by printing out the value before calling find_withtag.

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