0

我正在与graphics.py. 它是 Tkinter 的基于基本功能的“简单”衍生物

我正在开发一款游戏,如果我尝试 undraw() 方法,我会收到此错误

由于隐私问题,代码已被删除

完整追溯:

Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
    return self.func(*args)   File "C:\Users\Benny\Google Drive\PSAP\Assignments\Assignment_2_PSP\Assignment_2_Part2\Assign2_Part2.py",
line 38, in handleKeys
    play_game()   File "C:\Users\Benny\Google Drive\PSAP\Assignments\Assignment_2_PSP\Assignment_2_Part2\Assign2_Part2.py",
line 100, in play_game
    i.undraw()   File "C:\Users\Benny\Google Drive\PSAP\Assignments\Assignment_2_PSP\Assignment_2_Part2\game.py",
line 115, in undraw
    self.image.undraw()   File "C:\Users\Benny\Google Drive\PSAP\Assignments\Assignment_2_PSP\Assignment_2_Part2\graphics.py",
line 783, in undraw
    del self.imageCache[self.imageId]  # allow gc of tk photoimage KeyError: 6
4

1 回答 1

4

这意味着您的字典没有键18

>>> d = {1:2, 3:4}
>>> d[18]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 18

您可以捕获异常并忽略它,如下所示:

try:
    del self.imageCache[self.imageId]
except KeyError:
    pass
于 2013-06-03T04:55:45.290 回答