简体   繁体   English

以编程方式终止Python Shell(IDLE)

[英]Terminate Python Shell (IDLE) programmatically

I am using IDLE (Python 3.6 64-bit). 我正在使用IDLE(Python 3.6 64位)。 Currently, I am using Tkinter to create a GUI. 目前,我正在使用Tkinter创建一个GUI。 I have a button in my code that is supposed to terminate the program. 我的代码中有一个按钮应该会终止程序。 As of now, it only closes out the "tk" root window (using root.destroy() ). 到目前为止,它仅关闭“ tk”根窗口(使用root.destroy() )。 I want it to also close out the shell and go back to where I can edit the code (ie back to my IDLE). 我希望它也关闭外壳并返回我可以编辑代码的位置(即回到我的IDLE)。 How can I do this? 我怎样才能做到这一点?

When you run code from an IDLE editor window, any existing program run from IDLE is terminated. 当您从IDLE编辑器窗口运行代码时,任何从IDLE运行的现有程序都会终止。 Unless your code somehow grabs control of the mouse, you should always be able to click on any editor window or its taskbar icon (on Windows) and start editing some more. 除非您的代码以某种方式可以控制鼠标,否则您应该始终能够单击任何编辑器窗口或其任务栏图标(在Windows上),然后开始进行更多编辑。 Example: run the following from an IDLE editor. 示例:从IDLE编辑器运行以下命令。

import tkinter as tk
root = tk.Tk()
#root.destroy()

A tk window is dispayed. tk窗口被支付。 Click on the editor window, remove the '#', and run again. 单击编辑器窗口,删除“#”,然后再次运行。 The first tk window disappears and a >>> prompt appears in the Shell window. 第一个tk窗口消失,并且>>>提示符出现在Shell窗口中。

A python program run from IDLE, cannot* close its parent IDLE, anymore than a python program run in a console can close the parent console. 从IDLE运行的python程序不能*关闭其父IDLE,而在控制台中运行的python程序可以关闭父控制台。

  • I am excluding things like, on *nix, getting the parent process number and issuing a 'kill ' command. 我排除了在* nix上获得父进程号并发出“ kill”命令之类的东西。

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

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