如何在IDLE中停止Python程序的执行

发布于 2024-08-25 19:31:26 字数 243 浏览 1 评论 0原文

我有一个 python 脚本,它使用 plt.show() 作为最后一条指令。当它运行时,IDLE 只是在最后一条指令之后挂起。我收到了图像,但没有收到提示。

在其他脚本中,我通常使用 ctrl-c 来中断程序(有时不会立即工作),但如何使用 plt.show() 恢复提示? Ctrl-c 不起作用...

还有其他方法可以停止该程序吗?

如果有什么区别的话,这在 Windows 上是 IDLE。

I have a python script that uses plt.show() as it's last instruction. When it runs, IDLE just hangs after the last instruction. I get the image but I don't get the prompt back.

On other scripts I typically use ctrl-c to break the program (sometimes doesn't work immediately) but how do I get the prompt back with the plt.show()? Ctrl-c doesn't work...

Are there other ways to stop the program?

This is IDLE on Windows, if it makes any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那请放手 2024-09-01 19:31:26

Ctrl+F6
(重新启动外壳)

或外壳->重新启动外壳

Ctrl+F6
(Restart shell)

or Shell->Restart Shell

素食主义者 2024-09-01 19:31:26

我在 Windows 上使用 IDLE 和 matplotlib 时发现了这个问题。我不知道确切的原因,但 Ctrl-c 几次通常对我有用。如果这对您不起作用,您可以使用普通的解释器,而不是将绘图直接写入文件而不是屏幕。

这是 IDLE 的行为与正常 Python 脚本或解释器会话不同的(很多)情况之一。因此,我通常会避免 IDLE。

I have seen this problem with IDLE and matplotlib when using them on Windows. I don't know the exact cause, but Ctrl-c a couple times has typically worked for me. If that doesn't work for you, you can use the normal interpreter instead of write your plot directly to a file instead of the screen.

This is one of those (plentiful) times when IDLE doesn't behave like a normal Python script or interpreter session. Because of this, I usually avoid IDLE.

旧瑾黎汐 2024-09-01 19:31:26

当您使用 plt.show() 时,Python 子进程会进入 GUI 工具包的事件循环并阻塞,直到事件循环退出。当它退出时,您会收到提示。

如果您使用 TkAgg 后端,则需要在按 Ctrl+C 后将鼠标移到图形上。这将导致事件循环停止。 (Tkinter 有其怪癖)

或者,IdleX 使用 EventLoop.py 扩展为 IDLE 提供 Matplotlib 支持。您可以在不使用 plt.show() 的情况下显示图形并与图形交互。请务必在生成数字之前设置plt.interactive(True)

When you use plt.show(), the python subprocess enters the GUI toolkit's event loop and blocks until the event loop exits. When it exits, you get the prompt back.

If you are using the TkAgg backend, you'll need to move your mouse over a figure after you press Ctrl+C. That will cause the event loop to stop. (Tkinter has its quirks)

Alternatively, IdleX offers Matplotlib support with IDLE using the EventLoop.py extension. You can display and interact with figures without using plt.show(). Just be sure to set plt.interactive(True) before generating figures.

寂寞笑我太脆弱 2024-09-01 19:31:26

我在 Canopy Python 编辑器中遇到了同样的问题,并且我能够使用 CTRL+. (“点”按钮)中断 python 会话。希望有帮助,或者他们可能以类似的方式做事

I had same issue in Canopy Python Editor, and I was able to interrupt python session with CTRL+. ("dot" button). Hope that helps, or they probably do things in a similar ways

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文