6

我有以下代码:

num = int(raw_input("input number: "))
print "\b" * 20

控制台输出看起来像

input number: 10

我想input number: 10在用户按下后删除文本ENTER。退格键\b不能这样做。

4

4 回答 4

7

这将适用于大多数 unix 和 windows 终端......它使用非常简单的 ANSI 转义。

num = int(raw_input("input number: "))
print "\033[A                             \033[A"    # ansi escape arrow up then overwrite the line

请注意,在 Windows 上,您可能需要使用以下 http://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html启用 ANSI 支持

"\033[A" 字符串被终端解释为将光标向上移动一行。

于 2012-05-31T08:19:48.993 回答
1

There are control sequences for 'word back' and 'line back' and the like that move the cursor. So, you could try moving the curser back to the start of the text you want to delete, and overriding it with spaces. But this gets complicated very quickly. Thankfully, Python has the standard curses module for "advanced terminal handling".

The only issue with this is that it isn't cross-platform at the moment - that module has never been ported to Windows. So, if you need to support Windows, take a look at the Console module.

于 2012-05-31T08:16:24.113 回答
1
import sys

print "Welcome to a humble little screen control demo program"
print ""

# Clear the screen
#screen_code = "\033[2J";
#sys.stdout.write( screen_code )

# Go up to the previous line and then
# clear to the end of line
screen_code = "\033[1A[\033[2K"
sys.stdout.write( screen_code )
a = raw_input( "What a: " )
a = a.strip()
sys.stdout.write( screen_code )
b = raw_input( "What b: " )
b = b.strip()
print "a=[" , a , "]"
print "b=[" , b , "]"
于 2015-07-02T04:12:09.130 回答
-2

You can use os module

import os
os.system('clear')

The "cls" and "clear" are commands which will clear a terminal (ie a DOS prompt, or terminal window).

For IDLE:The best you could do is to scroll the screen down lots of lines, eg:

print "\n" * 100
于 2012-05-31T08:15:17.763 回答