风城烟雨 独自徘徊 2022-02-20 20:49 采纳率: 80%
浏览 65
已结题

为何会有TCL error?(语言-python)


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


  • 写回答

2条回答 默认 最新

  • 关注

    是不是你关闭窗口之后无法再访问窗口中对象了吧
    w.update() 放在 try except 中
    或者应该改用 w.after() 定时器执行重复操作, 不要用死循环的while True:

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月7日
  • 已采纳回答 2月27日
  • 创建了问题 2月20日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效