我是 python 新手,正在尝试编写一个简单的游戏,但在更新主游戏循环后不断收到此错误消息。
Traceback (most recent call last):
File "D:\python shell\Bubble Blaster.py", line 75, in <module>
move_bubbles()
File "D:\python shell\Bubble Blaster.py", line 67, in move_bubbles
c.move(bub_id[i], -bub_speed[i], 0)
File **not displaying for privacy**
\lib\tkinter\__init__.py", line 2430, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".54600176"
明显有错误的行是这一行:
#MAIN GAME LOOP
while True:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
window.update()
sleep(0.01)
move_bubbles() 是第 75 行
还有这个:
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_speed[i], 0)
def move_bubbles(): 是第 67 行
到目前为止,我创建的“气泡”按照预期进行,但是当我尝试创建一个碰撞事件,导致气泡在击中我创建的潜艇 Controller 时“弹出”时,我收到此错误消息。我已经检查了每一行代码并将其与我正在使用的教程书进行了比较,并且没有犯错误,有人可以帮助我或解释错误的含义吗?这是一个非常令人沮丧的问题!
最佳答案
在 Tk 中,根窗口被命名为“.” (点) 及其子项被命名为父项名称的点分隔路径。 Tkinter 使用数字为您生成名称。另一件需要注意的事情是,在 Tk 中,窗口的名称也是提供对该窗口的操作的命令。因此,您在这里遇到的错误是告诉您,您的一个窗口不再存在,因为管理它的命令已经消失。我建议您在迭代时修改您的 bub_id
列表,从而使您有可能获得已在其他地方销毁的窗口名称。您可以使用 c.winfo_exists
来避免错误,它可以让您知道窗口是否确实存在并且即使窗口已被销毁也可以正常工作。但实际上,您应该尽量避免在损坏的窗口上进行调用。
这是一个产生相同错误的小示例:
>>> import tkinter as tk
>>> main = tk.Tk()
>>> b = tk.Label(main, text="hello")
>>> b.destroy()
>>> b.configure()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/tkinter/__init__.py", line 1322, in configure
return self._configure('configure', cnf, kw)
File "/usr/lib/python3.4/tkinter/__init__.py", line 1310, in _configure
return self._getconfigure(_flatten((self._w, cmd)))
File "/usr/lib/python3.4/tkinter/__init__.py", line 1294, in _getconfigure
for x in self.tk.splitlist(self.tk.call(*args)):
_tkinter.TclError: invalid command name ".140685140686048"
>>> b.winfo_exists()
0
>>>
关于python - _tkinter.TclError : invalid command name ".54600176" error, 这是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37904750/