有谁知道这个错误? KeyError:18

时间:2013-06-03 04:48:05

标签: python dictionary tkinter

我正在使用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

1 个答案:

答案 0 :(得分: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