在没有提示的情况下关闭 Python IDLE shell [英] Close Python IDLE shell without prompt

查看:63
本文介绍了在没有提示的情况下关闭 Python IDLE shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个脚本(脚本 A),它需要打开一个新的 Python IDLE shell,在其中自动运行另一个脚本(脚本 B),然后关闭它.以下代码是我用于此目的的代码:

I am working on a script (script A), that needs to open a new Python IDLE shell, automatically run another script (script B) in it and then close it. The following code is what I use for this purpose:

import sys
sys.argv=['','-n','-t','My New Shell','-c','execfile("VarLoader.py")']
import idlelib.PyShell
idlelib.PyShell.main()

但是我无法自动关闭新外壳.我曾尝试将以下内容添加到脚本 B 中,但它要么没有关闭新的 shell,要么弹出一个窗口询问我是否要杀死它.

However I can't get the new shell close automatically. I have tried adding the following to script B but either it doesn't close the new shell or a windows pops up asking whether I want to kill it.

exit()

.

import sys
sys.exit()

推荐答案

我建议你创建一个 PyShell 的子类,而不是使用猴子补丁或修改 IDLE 源代码来让你的程序跳过提示退出code> 覆盖 close 方法您希望它如何工作:

Instead of monkeypatching or modifying the IDLE source code to make your program skip the prompt to exit I'd recommend you create a subclass of PyShell that overrides the close method how you want it to work:

import idlelib.PyShell
class PyShell_NoExitPrompt(idlelib.PyShell.PyShell):
    def close(self):
        "Extend EditorWindow.close(), does not prompt to exit"
##        if self.executing:
##            response = tkMessageBox.askokcancel(
##                "Kill?",
##                "Your program is still running!\n Do you want to kill it?",
##                default="ok",
##                parent=self.text)
##            if response is False:
##                return "cancel"
        self.stop_readline()
        self.canceled = True
        self.closing = True
        return idlelib.PyShell.EditorWindow.close(self)

最初的问题是使用 idlelib.PyShell.main 不会使用您的子类,实际上您可以创建函数的副本 - 无需修改原始 - 通过使用将使用您修改后的类的 FunctionType 构造函数:

The original issue with this was that then using idlelib.PyShell.main would not use your subclass, you can in fact create a copy of the function - without modifying the original - by using the FunctionType constructor that will use your modified class:

import functools
from types import FunctionType

def copy_function(f, namespace_override):
    """creates a copy of a function (code, signature, defaults) with a modified global scope"""
    namespace = dict(f.__globals__)
    namespace.update(namespace_override)
    new_f = FunctionType(f.__code__, namespace, f.__name__, f.__defaults__, f.__closure__)
    return functools.update_wrapper(f, new_f)

然后你可以像这样运行你额外的 IDLE shell:

Then you can run your extra IDLE shell like this:

import sys
#there is also a way to prevent the need to override sys.argv but that isn't as concerning to me.
sys.argv = ['','-n','-t','My New Shell','-c','execfile("VarLoader.py")']
hacked_main = copy_function(idlelib.PyShell.main,
                            {"PyShell":PyShell_NoExitPrompt})

hacked_main()

现在您可以让 IDLE 保持原样,让您的程序也以您想要的方式工作.(它也兼容其他版本的python!)

Now you can leave IDLE the way it is and have your program work the way you want it too. (it is also compatible with other versions of python!)

这篇关于在没有提示的情况下关闭 Python IDLE shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆