我的项目有点问题。我的游戏运行正常,但我们的shell中不允许出错。我创造了一个游戏,气球飞向你,你必须用键盘控制的箭头弹出它们。
当我使用move和create时,我的tkinter会抛出一个带有无效命令名的TclError。
这是我的编码:
#Add balloons to the canvas and make them fly
import random
balloon_id = list()
balloon_radius = list()
balloon_speed = list()
MIN_BALLOON_RADIUS = 10
MAX_BALLOON_RADIUS = 30
MAX_BALLOON_SPEED = 10
GAP = 100
def create_balloon():
x = WIDTH + GAP
y = random.randint(0, HEIGHT)
r = random.randint(MIN_BALLOON_RADIUS, MAX_BALLOON_RADIUS)
id1 = c.create_oval(x-r,y-r,x+r,y+r, outline='purple4', fill='MediumPurple2')
balloon_id.append(id1)
balloon_radius.append(r)
balloon_speed.append(random.randint(1, MAX_BALLOON_SPEED))
def move_balloons():
for i in range(len(balloon_id)):
c.move(balloon_id[i], -balloon_speed[i], 0)
它会为create_balloon()抛出这些错误:
File "C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2334, in create_oval
return self._create('oval', args, kw)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2319, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: invalid command name ".55505712"
move_balloon()的错误:
File "C:\Users\xxx\Documents\Varsity\xxx\Balloon Killer - 19342306.py", line 128, in <module>
move_balloons()
File "C:\Users\xxx\Documents\Varsity\xxx\Balloon Killer - 19342306.py", line 66, in move_balloons
c.move(balloon_id[i], -balloon_speed[i], 0)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2430, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".42332976"