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()
关键修改点说明
- 添加等待交互代码:
the_window.getMouse()会让程序暂停执行,直到用户点击窗口内的区域,这样窗口会一直保持打开状态,直到用户主动触发关闭动作。 - 修复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
相关产品推荐
相关产品推荐

