【问题标题】:graphics.py GraphWin automatically closinggraphics.py GraphWin 自动关闭
【发布时间】:2014-04-11 00:51:50
【问题描述】:

我是 Python 新手,正在尝试通过 John Zelle 的 graphics.py 学习图形。我正在 MacVim 中编写 Python 脚本并在 Python 3 上从终端 (Mac OS 10.9.2) 执行。如果我尝试使用 GraphWin() 打开一个新窗口,该窗口会短暂打开,然后立即关闭。

例如:

from graphics import *

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

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

c.draw(win)

带有 TkInter 的 GUI 元素可以正常工作。 任何想法为什么会发生这种情况?

谢谢!

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    如果您在问题中显示的语句是作为脚本输入的,而您只是运行了该脚本,那么问题是在脚本结束时窗口已隐式关闭。您将在documentation 的第一个示例中看到(它也出现在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()
    

    请注意,作者特意加入了关闭窗口前暂​​停的声明。

    如果您只是在交互式 Python 提示符下逐一键入语句而不是脚本,那么只要该 Python 提示符打开,图形窗口就会保持打开状态(直到您明确关闭它,或关闭Python 会话)。

    【讨论】:

    • 即使在窗口内部单击,如何才能永远暂停结果?
    • while True:\n\twindow.getMouse() ?
    猜你喜欢
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2012-02-21
    • 2015-02-14
    • 2011-01-15
    • 2017-08-04
    • 2010-11-03
    • 2018-07-21
    相关资源
    最近更新 更多