Python 多行终端更新 [英] Python Multiple Line Terminal Update
问题描述
简短问题
在 Windows 终端上编写和更新多行(包含 \n
)字符串是否可能/实际?
Short Question
Is it possible/practical to write and update a multi-line (contains \n
) string on a Windows terminal?
背景
我研究了 curses,但它仅适用于 Unix.我看到了一些其他的 Window 端口,但有点麻烦的是,Windows XP 是其中一个的实验性操作系统.我希望将其用作诊断功能的一部分,以在主要终端应用程序上显示链接状态、消息速率等(请注意,某些变体确实具有 wxPython GUI 输入).话虽如此,使用 Cygwin 并不理想,并且希望找到仅使用 sys 模块的解决方法.
Background
I have looked into curses, but it is Unix only. I saw a few other Window ports, but it was a bit troubling that Windows XP was an experimental OS for one them. I am hoping to use this as part of a diagnostic feature to display link status, message rates, etc on a mainly terminal application (note that some variants do have a wxPython GUI input). That being said, using Cygwin is non-ideal and would love to find a workaround using only the sys module.
我尝试了以下方法:(请注意,我希望它们会失败,但希望我是错的)
尝试 1:更新字符串,但都在 1 行
I have tried the following: (note that I expected them to fail, but hoped I would be wrong)
Attempt 1: Updates the string but it is all on 1 line
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:不更新但打印所有行
Attempt 2: Does not update but prints all the lines
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()
推荐答案
我能找到的最接近 curses(在过去 10 年已经更新)的东西是 Windows 控制台驱动程序.我没有使用这种方法,而是采用了一种不太优雅的方法.
The closest thing I could find to curses (that has been updated in the last 10 years) was Windows Console Driver. Rather than use this approach I went at it was a less elegant method.
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"
这篇关于Python 多行终端更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!