Show default value for editing on Python input pos

2019-01-01 13:19发布

Is it possible for python to accept input like this:

Folder name: Download

But instead of the user typing "Download" it is already there as a initial value. If the user wants to edit it as "Downloads" all he has to do is add a 's' and press enter.

Using normal input command:

folder=input('Folder name: ')

all I can get is a blank prompt:

Folder name:

Is there a simple way to do this that I'm missing?

标签: python input
11条回答
其实,你不懂
2楼-- · 2019-01-01 14:05

I would like to suggest using the clipboard to solve this problem. Paste the clipboard into the input line, edit as required, press enter. Variable clpstack is used to protect existing clipboard contents. This code is for Windows. Linux could use import clipboard.

import pyperclip as clp
clpstack=clp.paste()
clp.copy("192.168.4.1")
HOST = input("Enter telnet host: ")
clp.copy(clpstack)
查看更多
冷夜・残月
3楼-- · 2019-01-01 14:07

I'm assuming you mean from the command-line. I've never seen initial values for command line prompts, they're usually of the form:

     Folder [default] : 

which in code is simply:

     res = raw_input('Folder [default] : ')
     res = res or 'default'

Alternatively, you can try to do something using the curses module in Python.

查看更多
不再属于我。
4楼-- · 2019-01-01 14:09

We can use Tkinter and use a StringVar to do this. The limitation is that the input is through a Tkinter window.

from tkinter import Tk, LEFT, BOTH, StringVar
from tkinter.ttk import Entry, Frame


class Example(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Entry")
        self.pack(fill=BOTH, expand=1)
        self.contents = StringVar()
        # give the StringVar a default value
        self.contents.set('test')
        self.entry = Entry(self)
        self.entry.pack(side=LEFT, padx=15)
        self.entry["textvariable"] = self.contents
        self.entry.bind('<Key-Return>', self.on_changed)

    def on_changed(self, event):
        print('contents: {}'.format(self.contents.get()))
        return True


def main():
    root = Tk()
    ex = Example(root)
    root.geometry("250x100+300+300")
    root.mainloop()


if __name__ == '__main__':
    main()
查看更多
ら面具成の殇う
5楼-- · 2019-01-01 14:09

This is not a very Good Answer but it is a work around for windows. As hard as I tried, I could not get Readline or pyReadline to work on my Windows10 computer with Python Ver 3.5. So I wrote this instead. Not the best code in the world since I've only been using Python for 3 months. But it works.

    import os

    def note_input(defaultvalue):
        #Create a textfile
        txtfile = open("txtfile.txt", "w")
        #
        # populate it with the default value
        txtfile.write(defaultvalue)
        txtfile.close()
        #
        # call Notepad
        os.system("notepad.exe txtfile.txt") 
        # input("Just holding until notepad is close : ") (did not need this line) 
        #
        # get the Value Entered/Changed in Notepad
        txtfile = open("txtfile.txt", "r")
        func_value = txtfile.read()
        txtfile.close()
        return func_value
    # END DEF

Notepad stopped the program from running until it was closed, so the input() line below it was not needed. Once notepad was opened the first time and placed where I wanted it on the screen, It was like a popup input window. I assume you can use any text editor like Notepad++ or Scripe or Code Writer, etc.

查看更多
还给你的自由
6楼-- · 2019-01-01 14:13

I like this, It works on window

def inputWdefault(prompt, default):
    bck = chr(8) * len(default)
    ret = input(prompt + default + bck)
    return ret or default
查看更多
登录 后发表回答