python TclError错误

我现在在写一个flappy bird的游戏,但每次按下r重启游戏后,都会出现这样的问题

img

版本信息:
python3.85
Anaconda3
Windows10

原本的代码是这样的:


import tkinter as tk
import sys
from random import randint
import time
w=tk.Tk()
w.geometry('800x500')
canvas=tk.Canvas(w, width=800, height=500, bg="white")
SIZE=40
def close():
    sys.exit()
class S:
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.lose=False
        self.rect=canvas.create_rectangle(x,y,x+SIZE,y+SIZE,fill="blue")
        self.speed=1
    def move(self):
        self.y+=self.speed
        if self.y<0:
            self.speed=1
        elif self.y>500:
            self.lose=True
        self.speed+=0.1
        canvas.coords(self.rect,self.x,self.y,self.x+SIZE,self.y+SIZE)
class B:
    def __init__(self):
        self.l=randint(150,250)
        self.x=800
        self.des=False
        self.rect1=canvas.create_rectangle(800,0,850,self.l,fill="yellow")
        self.rect2=canvas.create_rectangle(800,self.l+150,850,500,fill="yellow")
    def move(self):
        self.x-=5
        if self.x<-50:
            self.des=True
            return
        canvas.coords(self.rect1,self.x,0,self.x+50,self.l)
        canvas.coords(self.rect2,self.x,self.l+150,self.x+50,500)
    def hit(self):
        if s.x+SIZE>self.x and s.x50 \
            and (s.yor s.y+SIZE>self.l+150):
            s.lose=True
canvas.focus_set()
s=S(0,0)
bs=[]
canvas.pack()
RESET=False
def up(e):
    s.speed=-4
def rm():
    i=0
    while i<len(bs):
        if bs[i].des:
            bs.pop(i)
        else:
            i+=1
def reset(e):
    global RESET,s
    canvas.delete(tk.ALL)
    del s
    s=S(0,0)
    bs=[]
    RESET=True
w.bind("", up)
w.bind("",reset)
w.bind("",close)
s.move()
w.update()
time.sleep(1)
last=0
while True:
    while True:
        s.move()
        if time.time()-last>1:
            bs.append(B())
            last=time.time()
        for b in bs:
            b.move()
            b.hit()
        rm()
        w.update()
        if s.lose:
            canvas.create_text(400,225,text="You lose!",font=('Arial',50))
            break
        time.sleep(0.005)
    while True:
        if RESET:
            w.update()
            break
        
       
        try:
            w.update()
        except:
            break
    RESET=False


运行完后,会报这样的错:

---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
6-bce175cd58e0> in <module>
     72 while True:
     73     while True:
---> 74         s.move()
     75         if time.time()-last>1:
     76             bs.append(B())

6-bce175cd58e0> in move(self)
     23             self.lose=True
     24         self.speed+=0.1
---> 25         canvas.coords(self.rect,self.x,self.y,self.x+SIZE,self.y+SIZE)
     26 class B:
     27     def __init__(self):

~\anaconda3\lib\tkinter\__init__.py in coords(self, *args)
   2759         return [self.tk.getdouble(x) for x in
   2760                            self.tk.splitlist(
-> 2761                    self.tk.call((self._w, 'coords') + args))]
   2762 
   2763     def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})

TclError: invalid command name ".!canvas"

网上查了很长时间,都没有方法解决。请问有谁能帮忙解决一下这个问题吗?

基于Monster 组和GPT的调写:
因为重启游戏后,画布上所有对象被删除,但对象的实例变量却没有被清除。在reset函数中添加以下行:


global s, bs
del s
del bs[:]
s = S(0, 0)
bs = []

删除现有的s和bs实例,并创建一个新的S实例和一个新的空列表bs。