简体   繁体   English

graphics.py GraphWin自动关闭

[英]graphics.py GraphWin automatically closing

I'm new to Python and trying to learn graphics with John Zelle's graphics.py . 我是Python的新手,正在尝试通过John Zelle的graphics.py学习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. 我正在MacVim中编写Python脚本,并在Python 3上从终端(Mac OS 10.9.2)执行。如果尝试使用GraphWin()打开新窗口, GraphWin()该窗口会短暂打开,然后立即关闭。

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. 使用TkInter的GUI元素可以正常工作。 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): 您将在文档的第一个示例中看到该示例(该示例也出现在graphics.py本身的源代码中):

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). 如果您只是在交互式Python提示符下逐个键入语句(而不是脚本),则只要该Python提示符处于打开状态,图形窗口就会保持打开状态(直到您明确关闭它或关闭Python会话) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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