简体   繁体   中英

graphics.py GraphWin automatically closing

I'm new to Python and trying to learn graphics with John Zelle's graphics.py . I'm writing the Python scripts in MacVim and executing from the terminal (Mac OS 10.9.2) on Python 3. If I try to open a new window with GraphWin() the window opens briefly but then immediately closes.

Ex:

from graphics import *

win = GraphWin("circle",500,500)

c = Circle(point(100,100),30)

c.draw(win)

GUI elements with TkInter work just fine. Any ideas why this might be happening?

Thanks!

If the statements you've shown in your question were entered as a script, and you just ran the script, then the problem is that the window is closed implicitly when the script ends. You will see in the very first example from the documentation (which also appears in the source code for graphics.py itself):

from graphics import *

def main():
    win = GraphWin("My Circle", 100, 100)
    c = Circle(Point(50,50), 10)
    c.draw(win)
    win.getMouse() # Pause to view result
    win.close()

main()

Notice that the author has specifically included a statement to pause before closing the window.

If, instead of a script, you just type the statements in one by one at an interactive Python prompt, then as long as that Python prompt is open, the graphics window stays open (until you explicitly close it, or close the Python session).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM