2016-08-26 1 views
0

Think Python 텍스트 북별로 다음 코드를 입력하면 아래 오류 메시지가 나타납니다."캔버스"명령을 호출 할 수 없습니다. 응용 프로그램이 손상되었습니다.

창이 실제로 표시되지만 원하는 내용이 없습니다.

from swampy.World import World 
world=World() 
world.mainloop() 
canvas = world.ca(width=500, height=500, background='white') 
bbox = [[-150,-100], [150, 100]] 
canvas.rectangle(bbox, outline='black', width=2, fill='green4') 

오류 메시지는 다음과 같이이었다 :

Traceback (most recent call last): 
    File "15.4.py", line 4, in <module> 
    canvas = world.ca(width=500, height=500, background='white') 
    File "/usr/local/lib/python2.7/dist-packages/swampy/Gui.py", line 244, in ca 
    return self.widget(GuiCanvas, width=width, height=height, **options) 
    File "/usr/local/lib/python2.7/dist-packages/swampy/Gui.py", line 359, in widget 
    widget = constructor(self.frame, **widopt) 
    File "/usr/local/lib/python2.7/dist-packages/swampy/Gui.py", line 612, in __init__ 
    Tkinter.Canvas.__init__(self, w, **options) 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2234, in __init__ 
    Widget.__init__(self, master, 'canvas', cnf, kw) 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2094, in __init__ 
    (widgetName, self._w) + extra + self._options(cnf)) 
    _tkinter.TclError: can't invoke "canvas" command: application has been destroyed 

답변

3

의 기본 응용 프로그램 루프가 응용 프로그램에서 실행 거의 마지막 일이 될 필요가있다. 코드에서 어떻게됩니까

from swampy.World import World 

world = World() 

canvas = world.ca(width=500, height=500, background='white') 
bbox = [[-150, -100], [150, 100]] 
canvas.rectangle(bbox, outline='black', width=2, fill='green4') 

world.mainloop() 

world.mainloop()와 라인이 충돌 할 때, 그것은 사용자 인터페이스 요소를 구축하고 지속적으로 메인 루프에 들어가있다 : 그래서 이런 코드의 끝으로 world.mainloop() 이동 응용 프로그램에 사용자 입력을 제공합니다.

평생 동안 메인 루프는 응용 프로그램이 99 %의 시간을 소비하는 곳입니다.

하지만 일단 응용 프로그램을 종료하면 주 루프가 종료되어 모든 사용자 인터페이스 요소와 세계가 찢어집니다. 그런 다음 메인 루프 다음의 나머지 라인이 실행됩니다. 그 중 하나는 이미 파괴 된 세계에 캔버스를 작성하고 연결하려는 것이므로 오류 메시지입니다.

관련 문제