You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中Python的GraphWin窗口循环结束后自动关闭求助

问题解决:GraphWin窗口自动关闭的处理

你的问题核心是程序执行完窗口代码后直接结束,导致VSCode中图形窗口被自动销毁(IDLE的运行环境会保留图形上下文,所以表现正常)。以下是具体修复方案:

修复后的histogram_construction函数

def histogram_construction():
    the_window = GraphWin("The Histogram", 500, 500)
    aRectangle = Rectangle(Point(1,3), Point(4,7))

    aRectangle.draw(the_window)
    # 等待用户点击窗口内任意位置后再关闭
    the_window.getMouse()
    # 正确调用close方法(原代码中the_window.close是未执行的方法引用)
    the_window.close()

关键修改点说明

  1. 添加等待交互代码:the_window.getMouse()会让程序暂停执行,直到用户点击窗口内的区域,这样窗口会一直保持打开状态,直到用户主动触发关闭动作。
  2. 修复close方法调用:原代码中的the_window.close只是引用了方法对象,没有实际执行关闭操作,需要改为the_window.close()才能正确调用方法。

如果希望窗口保持到用户手动点击窗口右上角的关闭按钮,也可以替换为以下代码:

def histogram_construction():
    the_window = GraphWin("The Histogram", 500, 500)
    aRectangle = Rectangle(Point(1,3), Point(4,7))

    aRectangle.draw(the_window)
    # 循环等待直到窗口被用户手动关闭
    while not the_window.isClosed():
        pass

内容的提问来源于stack exchange,提问作者Beanerino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:43:28