[英]Removing input() text from console Python3
I want to clear the line of text created after the input() function.我想清除 input() function 之后创建的文本行。 I've tried \033[a, \r, end='' but either I'm doing it wrong or it doesn't work.我试过 \033[a, \r, end='' 但要么我做错了,要么它不起作用。 Is there a better way:有没有更好的办法:
Code is:代码是:
rawguess = input('Enter a 5 letter word: ')
print('words go here')
I've tried:我试过了:
rawguess = input('\033[A' + 'Enter a 5 letter word: ' + '\033[A')
print('words go here')
rawguess = input('Enter a 5 letter word: ')
print('\rwords go here')
rawguess = input('Enter a 5 letter word: ')
print('\r', end='')
print('words go here')
And other variations of these three but nothing's worked so far.以及这三个的其他变体,但到目前为止没有任何效果。
I didn't know this before, but apparently Windows now supports Console Virtual Terminal Sequences , which include using ESC [ <n> A
to move the cursor up (like on *nix).我以前不知道这一点,但显然 Windows 现在支持控制台虚拟终端序列,其中包括使用ESC [ <n> A
将 cursor 向上移动(如 *nix 上)。 So we can call the Win32 API with ctypes.windll
and enable Virtual Terminal Sequences, which will allow us to use '\x1b[1A'
.所以我们可以使用 ctypes.windll 调用 Win32 ctypes.windll
并启用虚拟终端序列,这将允许我们使用'\x1b[1A'
。
import sys
# Only do this on Windows, so that *nix users
# can also run the program without errors
if sys.platform.startswith('win'):
import ctypes
from ctypes.wintypes import *
STD_OUTPUT_HANDLE = -11
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
kernel32 = ctypes.windll.kernel32
h = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
m = DWORD()
kernel32.GetConsoleMode(h, ctypes.pointer(m))
m.value |= ENABLE_VIRTUAL_TERMINAL_PROCESSING
kernel32.SetConsoleMode(h, m)
rawguess = input('Enter a 5 letter word: ')
print('\x1b[1A' + 'words go here')
Well I just found a piece of dark magic allowing you to enable terminal controls in Python:好吧,我刚刚发现了一个黑魔法,可以让你在 Python 中启用终端控制:
import subprocess
subprocess.run("", shell=True)
input('Enter a 5 letter word: ')
print('\x1b[1A' + 'words go here ')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.