[英]Move the Cursor Back For taking Input Python
我试图这样做:
a = input('Enter your name:_____________________________________________')
但在这里,cursor 位于:
Enter your name:_____________________________________________
^
Here
我如何 position cursor 在:
Enter your name:_____________________________________________
^
Here
我在 IDLE 上做这个。 (命令提示符也很受欢迎:))
编写文本提示、多个下划线和相同数量的退格字符 ( \b
)。
n_underscores = 45
a = input('Enter your name:' + '_' * n_underscores + '\b' * n_underscores)
print('Hi,', a)
您可以使用\r
字符返回到行首并覆盖文本,直到您想要输入 cursor:
print("Enter your name: ____________________", end='\rEnter your name: ', flush=True)
a = input()
值得注意的是,移动一个 cursor 只适用于某些控制台,例如它在 IDLE 下不起作用,但在 windows 命令提示符下会起作用
进一步研究后,我的最终解决方案是创建自己的自定义输入。 注意:这只适用于 windows 命令提示符。 如果您希望在 Unix 中执行此操作,您将不得不寻找替代方案。 我稍后可能会更新此答案以支持 Unix。 [编辑:更新以支持 Unix 和 Windows]
同样,这在 IDLE 中不起作用
这个 function 为您的输入使用前缀、输入的 max_length 数量以及用于填充输入长度的空白字符。
首先,我们捕获用户输入,如果它是一个可显示的字符,我们将它添加到我们的单词中。 每次我们得到一个新字符时,我们都会覆盖现有的输入行,以与单词的当前 state 保持同步。
我们还寻找b'\x08'
字符(退格),这样我们就可以从单词中删除最后一个字符,然后返回字符来查看我们的用户是否按下了 Enter。
按下回车后,返回单词。
还有一个内置限制,用户只能输入与下划线一样多的字符,因为这会导致字母逗留,您始终可以通过增加下划线的数量或类似的东西来修改它。
仅限 WINDOWS:
import msvcrt
def windows_get_input(prefix="", underscores=20, blank_char='_'):
word = ""
print(prefix + (underscores - len(word)) * blank_char, end='\r', flush=True)
# Reprint prefix to move cursor
print(prefix, end="", flush=True)
while True:
ch = msvcrt.getch()
if ch in b'\x08':
# Remove character if backspace
word = word[:-1]
elif ch in b'\r':
# break if enter pressed
break
else:
if len(word) == underscores:
continue
try:
char = str(ch.decode("utf-8"))
except:
continue
word += str(char)
# Print `\r` to return to start of line and then print prefix, word and underscores.
print('\r' + prefix + word + (underscores - len(word)) * blank_char, end='\r', flush=True)
# Reprint prefix and word to move cursor
print(prefix + word, end="", flush=True)
print()
return word
print(windows_get_input("Name: ", 20, '_'))
WINDOWS 和 UNIX:
def unix_getch():
import termios
import sys, tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch.encode("utf-8")
return _getch()
def get_input(prefix="", underscores=20, blank_char='_'):
word = ""
try:
import msvcrt
func = msvcrt.getch
except:
func = unix_getch
print(prefix + (underscores - len(word)) * blank_char, end='\r', flush=True)
# Reprint prefix to move cursor
print(prefix, end="", flush=True)
while True:
ch = func()
if ch in b'\x08\x7f':
# Remove character if backspace
word = word[:-1]
elif ch in b'\r':
# break if enter pressed
break
else:
if len(word) == underscores:
continue
try:
char = str(ch.decode("utf-8"))
except:
continue
word += str(char)
# Print `\r` to return to start of line and then print prefix, word and underscores.
print('\r' + prefix + word + (underscores - len(word)) * blank_char, end='\r', flush=True)
# Reprint prefix and word to move cursor
print(prefix + word, end="", flush=True)
print()
return word
print(get_input("Name: ", 20, '_'))
使用 IDLE,这会失败。 感谢 Python 给了我们这个......无论如何,如果你想处理在 IDLE 中运行,只需在get_input()
function 顶部添加以下内容:
import sys
if 'idlelib.run' in sys.modules:
return input(prefix)
这将检查您是否正在使用 IDLE,并仅提示用户正常输入。
更新 1:将 cursor 移动到下一个字符位置
更新 2:添加了 Unix 支持
更新 2.1:分离原始 Windows 唯一答案和 Windows 和 Unix 答案
更新 3:处理 IDLE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.