我9岁的儿子正在学习Python,在他的代码(泡泡破解游戏)中他面临以下问题:
Traceback (most recent call last):
File "M:\Yandex disk\YandexDisk\ДЕТКИ\python modules\Bubble Blaster.py", line 55, in <module>
move_bubbles()
File "M:\Yandex disk\YandexDisk\ДЕТКИ\python modules\Bubble Blaster.py", line 48, in
move_bubbles
c.move(bub_id[i], -bub_speed[i], 0)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line
2949, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".!canvas"*我已经重新检查了整个代码两次,但没有发现任何错误。
以下是整个代码:
from tkinter import *
HEIGHT = 1080
WIDTH = 1920
window = Tk()
window.title('Bubble Blaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='red')
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
SHIP_SPD = 10
def move_ship(event):
if event.keysym == 'Up':
c.move(ship_id, 0, -SHIP_SPD)
c.move(ship_id2, 0, -SHIP_SPD)
elif event.keysym == 'Down':
c.move(ship_id, 0, SHIP_SPD)
c.move(ship_id2, 0, SHIP_SPD)
elif event.keysym == 'Left':
c.move(ship_id, -SHIP_SPD, 0)
c.move(ship_id2, -SHIP_SPD, 0)
elif event.keysym == 'Right':
c.move(ship_id, SHIP_SPD, 0)
c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)
from random import randint
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
def create_bubble():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_BUB_R, MAX_BUB_R)
id1 = c.create_oval(x - r, y - r, x + r, y + r, outline='white')
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_speed[i], 0)
from time import sleep, time
BUB_CHANCE = 10
#MAIN GAME LOOP
while True:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
window.update()
sleep(0.01)
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def del_bubble(I):
del bub_r[I]
del bub_speed[I]
c.delete(bub_id[i])
del bub_id[I]
def clean_up_bubs():
for i in range(len(bub_id)-1, -1, -1):
x, y = get_coords(bub_id[I])
if x < -GAP:
del_bubble(I)
#MAIN GAME LOOP
while True:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
window.update()
sleep(0.01)
from math import sqrt
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def collision():
points = 0
for bub in range(len(bub_id)-1, -1, -1):
if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
points += (bub_r[bub] + bub_speed[bub])
del_bubble(bub)
return points
score = 0
#MAIN GAME LOOP
while True:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
score += collision()
print(score)
window.update()
sleep(0.01)是什么导致了这个错误?
发布于 2022-05-27 19:00:51
主要错误:
from tkinter import *
root = Tk()
root.mainloop()https://tkdocs.com/tutorial/eventloop.html
主循环内部的所有循环都是用https://tkdocs.com/shipman/universal.html方法
当窗口关闭时,当周期继续工作时,_tkinter.TclError: invalid command name ".!canvas"*会弹出一个错误。
c.delete(bub_idi)函数中没有定义变量"i“。
x, y = get_coords(bub_id[I])
if x < -GAP:
del_bubble(I)在这个函数中没有定义一个大的"I“。
https://stackoverflow.com/questions/72395995
复制相似问题