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.x<self.x+50 \
and (s.y<self.l or 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>", up)
w.bind("<KeyPress-r>",reset)
w.bind("<q>",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:
#print("ok")
w.update()
except:
w.update()
print("TclError")
#w.mainloop()
w.update()
RESET=False
报错:
TclError Traceback (most recent call last)
<ipython-input-1-fa67abaa92d1> in <module>
97 print("TclError")
98 #w.mainloop()
---> 99 w.update()
100 RESET=False
~\anaconda3\lib\tkinter\__init__.py in update(self)
1303 def update(self):
1304 """Enter event loop until all pending events have been processed by Tcl."""
-> 1305 self.tk.call('update')
1306
1307 def update_idletasks(self):
TclError: can't invoke "update" command: application has been destroyed