简短问题
在Windows终端上编写和更新多行(包含\n
)字符串是否可行/切实可行?
背景
我查看了curses,但它只是Unix。我看到了一些其他的Window端口,但有点令人不安的是Windows XP是一个实验性操作系统。我希望将其用作诊断功能的一部分,以在主要终端应用程序上显示链接状态,消息速率等(请注意,某些变体确实具有wxPython GUI输入)。话虽这么说,使用Cygwin是不理想的,并且希望仅使用sys模块找到解决方法。
我尝试过以下方法:(请注意,我希望他们失败,但希望我错了)
尝试1:更新字符串,但它全部在1行
sys.stdout.write("\r")
sys.stdout.write("This is a multi-line screen print test")
sys.stdout.write("Line 1")
sys.stdout.write("Line 2")
sys.stdout.flush()
尝试2:不更新但打印所有行
sys.stdout.write("\r")
sys.stdout.write("This is a multi-line screen print test\n")
sys.stdout.write("Line 1 \n")
sys.stdout.write("Line 2\n")
sys.stdout.flush()
答案 0 :(得分:1)
我能找到的最接近诅咒(已在过去10年中更新过)的是Windows Console Driver。我没有使用这种方法,而是采用了一种不太优雅的方法。
import os
import time
while(1):
time.sleep(.05)
os.system('cls')
print "This is a multi-line screen print test"
print "Line 1"
print "Line 2"
答案 1 :(得分:0)
您可能必须使用Windows Console API。例如,SetConsoleCursorPosition。其他人似乎已经实现了Python模块来支持此API:1,2