简体   繁体   English

以相同的诅咒多次打印-python

[英]print in same curses multiple times - python

I'm showing some live stats from a process with curses. 我正在通过诅咒展示一些实时统计数据。 I'm newby with this library, so I followed this example to implement what i need. 我是这个库的新手,所以我按照这个例子来实现我所需要的。

I write this small version which summarizes my problem: 我写了这个小版本,总结了我的问题:

import time
import curses

def draw_menu(stdscr):
    global data
    # Turn cursor off
    curses.curs_set(False)

    # Rendering text
    stdscr.addstr(2, 5, "Help me please")
    stdscr.addstr(5, 5, data)
    # Refresh the screen
    stdscr.refresh()
    time.sleep(3)

data = 'some initial value'
for i in xrange(2):
    curses.wrapper(draw_menu)
    # Do some stuff to update values shown in the menu
    data = 'updated value {}'.format(i)
    time.sleep(3)

After initial call to draw_menu, I need to update values displayed on menu. 最初调用draw_menu之后,我需要更新菜单上显示的值。 While update is running (in this example I used time.sleep), values are removed from window and it goes back to 'normal terminal mode', I don't know why. 当更新运行时(在本示例中,我使用time.sleep),值从窗口中删除,并返回“正常终端模式”,我不知道为什么。

I would like to update data while staying all time with this 'help me please' message & data displayed. 我想全天候更新数据,并显示此“请帮助我”消息和数据。

I think that I could solve this using threads but, since complete code is far more complicated, I would like to avoid threads. 我认为我可以使用线程解决此问题,但是由于完整的代码要复杂得多,因此我想避免使用线程。

How can I solve this? 我该如何解决?

Your example should put the loop inside the call to curses.wrapper, eg, something like 您的示例应将循环放入对curses.wrapper的调用中,例如,类似

import curses

def draw_menu(stdscr):
    global data
    # Turn cursor off
    curses.curs_set(False)

    # Rendering text
    stdscr.addstr(2, 5, "Help me please")
    stdscr.addstr(5, 5, data)
    # Refresh the screen
    stdscr.refresh()
    curses.napms(3000)

def draw_menu_loop(w):
    global data
    data = 'some initial value'
    for i in xrange(2):
        draw_menu(w)
        # Do some stuff to update values shown in the menu
        data = 'updated value {}'.format(i)
    curses.napms(3000)

curses.wrapper(draw_menu_loop)

By the way (actually should be a new question), your example used time.sleep , which interferes with timely updates to the screen. 顺便说一句(实际上应该是一个新问题),您的示例使用了time.sleep ,它会干扰屏幕的及时更新。 Use curses.napms , which does not have that drawback. 使用curses.napms ,它没有这个缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM