기본적으로 루프를 계속 실행하려고하는 프로그램의 끝에 while 루프를 추가 할 때까지 다음 코드가 제대로 작동합니다. 창을 닫을 때까지 영원히 (화면 업데이트). 기존 코드에 위의 코드를 추가하는 방법에 대한_tkinter.TclError : "업데이트"명령을 호출 할 수 없습니다. 응용 프로그램이 손상되었습니다. 오류
while 1:
tk.update_idletasks()
tk.update()
time.sleep(0.01)
, 프로그램은 실행하지만 출구에 ...이 오류와 함께 제공 :이와 비슷한 질문을 본
_tkinter.TclError: can't invoke "update" command: application has been destroyed error
SO에 대한 오류는 있지만이 특정 문제에 대한 답변은 없으며 어떤 답변도 내 구체적인 사례에 도움이되지 않습니다.
전체 코드는 다음과 같습니다 :
질문/문제 :이 오류를 일으키는 내가 그것을 어떻게 해결할 수 무엇?
from tkinter import *
import random
import time
tk=Tk()
tk.title("My 21st Century Pong Game")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas=Canvas(tk,bg="white",width=500,height=400,bd=0,highlightthickness=0)
canvas.pack()
tk.update()
class Ball: #create a ball class
def __init__(self,canvas,color): #initiliased with the variables/attributes self, canvas, and color
self.canvas=canvas #set the intiial values for the starting attributes
self.id=canvas.create_oval(30,30,50,50,fill=color) #starting default values for the ball
""" Note: x and y coordinates for top left corner and x and y coordinates for the bottom right corner, and finally the fill colour for the oval
"""
self.canvas.move(self.id,0,0) #this moves the oval to the specified location
def draw(self): #we have created the draw method but it doesn't do anything yet.
pass
ball1=Ball(canvas,'green') #here we are creating an object (green ball) of the class Ball
while 1:
tk.update_idletasks()
tk.update()
time.sleep(0.01)
설명 업데이트 : 그것은 또한 설명 할 가치가
:
메인 루프가 프로그램의 중심 부분이며, IDLE 이미 메인 루프를 가지고 -하지만 당신은이를 실행하는 경우 IDLE 외부에서 프로그램을 실행하면 캔버스가 나타나고 잠시 후에 사라집니다. 창을 닫지 않게하려면 애니메이션 루프가 필요합니다. 따라서 while 1 : ..... 그렇지 않으면 아래에 설명 된대로 잠시 기다릴 필요가 없습니다. IDLE에 이미이 위치가 있습니다 (1 : .. 등등을 사용하지 않고 IDLE에서 잘 작동 함)
단지 고정 된 코드 들여 쓰기를 - 미안 – MissComputing
사이드 참고 : Tkinter를 함께'절전()'와'대기()'사용하지 마십시오. 이러한 방법은 tkinter와 잘 작동하지 않습니다. 대신'after()'를 사용하십시오. –