您确实有两个选择。您可以离开诅咒模式,执行程序,然后恢复诅咒。第二,您可以异步执行程序,解析其输出并将其写入屏幕。
第一个选项的好消息是,您实际上不需要为UI编写任何幻想save_state / load_state方法。诅咒为您做到这一点。这是一个简单的例子来表明我的观点
import curses, time, subprocess
class suspend_curses():
"""Context Manager to temporarily leave curses mode"""
def __enter__(self):
curses.endwin()
def __exit__(self, exc_type, exc_val, tb):
newscr = curses.initscr()
newscr.addstr('Newscreen is %s\n' % newscr)
newscr.refresh()
curses.doupdate()
def main(stdscr):
stdscr.addstr('Stdscreen is %s\n' % stdscr)
stdscr.refresh()
time.sleep(1)
with suspend_curses():
subprocess.call(['ls'])
time.sleep(1)
stdscr.refresh()
time.sleep(5)
curses.wrapper(main)
如果运行示例,您会注意到由 curses.wrapper
以及创建的 curses.initscr
恢复时是同一对象。也就是说,窗口返回 curses.initscr
是单身人士。这使我们能够像上面一样退出诅咒和简历,而无需更新每个小部件 self.screen
每次参考。
第二种选项更多地参与,但更灵活。以下是为了代表基本思想。
class procWidget():
def __init__(self, stdscr):
# make subwindow / panel
self.proc = subprocess.Popen(my_args, stdout=subprocess.PIPE)
def update(self):
data = self.proc.stdout.readline()
# parse data as necessary
# call addstr() and refresh()
然后您的程序中的某个地方您需要致电 update
在计时器上的所有procwidgets上。这使您可以选择使您的子窗口任何尺寸/地点,因此您可以拥有尽可能多的procwidgets。当然,当过程终止和其他类似事件时,您将不得不添加一些处理。