是否可以在Python 3的命令行界面中预填充input()? [英] Is it possible to prefill a input() in Python 3's Command Line Interface?

查看:117
本文介绍了是否可以在Python 3的命令行界面中预填充input()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu 11.10(Linux)上使用Python 3.2。我的一段新代码如下:

I'm using Python 3.2 on Ubuntu 11.10 (Linux). A piece of my new code looks like this:

text = input( TEXT =)

是否可以在提示后获取一些预定义的字符串,所以我可以根据需要进行调整?应该是这样的:

Is it possible to get some predefined string after the prompt, so I can adjust it if needed? It should be like this:

python3 file
TEXT=thepredefinedtextishere

现在我按 Backspace 3次

TEXT=thepredefinedtextish

现在我按 Enter ,然后变量 text 应该是 thepredefinedtextish

Now I press Enter, and the variable text should be thepredefinedtextish

推荐答案

如果您的Python解释器链接到GNU readline,则 input()将使用它。在这种情况下,以下方法应该起作用:

If your Python interpreter is linked against GNU readline, input() will use it. In this case, the following should work:

import readline

def input_with_prefill(prompt, text):
    def hook():
        readline.insert_text(text)
        readline.redisplay()
    readline.set_pre_input_hook(hook)
    result = input(prompt)
    readline.set_pre_input_hook()
    return result

这篇关于是否可以在Python 3的命令行界面中预填充input()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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