[英]output on a new line in python curses
I am using curses module in python to display output in real time by reading a file. 我在python中使用curses模块通过读取文件实时显示输出。 The string messages are output to the console using addstr() function but I am not able to achieve printing to a newline wherever I need. 字符串消息使用addstr()函数输出到控制台,但我无法在任何需要的地方实现打印。
sample code: 示例代码:
import json
import curses
w=curses.initscr()
try:
while True:
with open('/tmp/install-report.json') as json_data:
beta = json.load(json_data)
w.erase()
w.addstr("\nStatus Report for Install process\n=========\n\n")
for a1, b1 in beta.iteritems():
w.addstr("{0} : {1}\n".format(a1, b1))
w.refresh()
finally:
curses.endwin()
The above is not really outputting the strings to a new line (notice the \\n in addstr()) with each iteration. 上面并没有真正将字符串输出到新行(注意每个迭代中的\\ n in addstr())。 On the contrary, the script fails off with error if I resize the terminal window. 相反,如果我调整终端窗口的大小,脚本将失败并显示错误。
w.addstr("{0} ==> {1}\n".format(a1, b1))
_curses.error: addstr() returned ERR
There's not enough program to offer more than general advice: 没有足够的计划提供一般建议:
window.scroll
). 如果脚本未启用滚动,则在打印到屏幕末尾时会出现错误(请参阅window.scroll
)。 KEY_RESIZE
(and ignore errors). 如果你调整终端窗口的大小,你将不得不读取键盘来处理任何KEY_RESIZE
(并忽略错误)。 Regarding the expanded question, these features would be used something like this: 关于扩展的问题,这些功能将使用如下:
import json
import curses
w=curses.initscr()
w.scrollok(1) # enable scrolling
w.timeout(1) # make 1-millisecond timeouts on `getch`
try:
while True:
with open('/tmp/install-report.json') as json_data:
beta = json.load(json_data)
w.erase()
w.addstr("\nStatus Report for Install process\n=========\n\n")
for a1, b1 in beta.iteritems():
w.addstr("{0} : {1}\n".format(a1, b1))
ignore = w.getch() # wait at most 1msec, then ignore it
finally:
curses.endwin()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.