简体   繁体   English

_tkinter.TclError:无效的命令名称“ .54600176”错误,这是怎么回事?

[英]_tkinter.TclError: invalid command name “.54600176” error, what is going on?

I am new to python and am trying to code a simple game, but am continuously receiving this error message after updating the main game loop. 我是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"

the lines which apparently have an error are this one: 显然有错误的行是这一行:

#MAIN GAME LOOP
while True:
    if randint(1, BUB_CHANCE) == 1:
        create_bubble()
    move_bubbles()
    window.update()
    sleep(0.01) 

move_bubbles() is line 75 move_bubbles()是第75行

and this: 和这个:

def move_bubbles():
    for i in range(len(bub_id)):
        c.move(bub_id[i], -bub_speed[i], 0)

def move_bubbles(): is line 67 def move_bubbles():是第67行

So far the 'bubbles' I created do as they are supposed to, but when I tried to create a collision event that causes the bubbles to 'pop' when they hit the submarine controller I created, I get this error message. 到目前为止,我创建的“气泡”按预期的方式运行,但是当我尝试创建一个碰撞事件,当气泡撞到我创建的潜艇控制器时导致气泡“弹出”,我收到了此错误消息。 I have checked every line of code and compared it to the tutorial book I am using and I haven't made an error, can someone please help me or explain what the error means? 我已经检查了每一行代码,并将其与我正在使用的教程书进行了比较,但没有出现错误,请有人帮助我或解释该错误是什么意思? It is a very frustrating issue! 这是一个非常令人沮丧的问题!

In Tk the root window is named '.' 在Tk中,根窗口名为“。” (dot) and its children are named as a dot delimited path of parent names. (点)及其子元素被命名为父名称的点分隔路径。 Tkinter generates the names for you using numbers. Tkinter使用数字为您生成名称。 The other thing to note is that in Tk the name of a window is also a command that provides operations on that window. 要注意的另一件事是,在Tk中,窗口的名称也是提供该窗口操作的命令。 So the error you have here is telling you that one of your windows no longer exists as the command that manages it is gone. 因此,此处出现的错误是告诉您,由于管理该窗口的命令已消失,因此其中一个窗口不再存在。 I suggest that your bub_id list is being modified while you iterate over it leaving you with the potential to obtain a window name that has been destroyed elsewhere. 我建议您在迭代bub_id列表时对其进行修改,从而有可能获得已在其他位置破坏的窗口名称。 You can avoid the error using c.winfo_exists which lets you know if the window is actually existing and works even if the window has been destroyed. 您可以使用c.winfo_exists避免该错误,它可以让您知道该窗口是否确实存在并且即使该窗口已被破坏也可以正常工作。 But really you should try to avoid making calls on destroyed windows. 但实际上,您应该尽量避免在损坏的窗口上进行调用。

Here's a small example that produces the same error: 这是一个产生相同错误的小示例:

>>> 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
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM