Python使用两个窗口进行curses,分别用于显示和输入

时间:2016-09-08 13:11:19

标签: python curses

我想制作一个带有两个窗口的终端应用程序。第一个窗口应该读取用户键入的命令(就像在vim中键入:时一样),而第二个窗口应该显示定期更新的内容(例如:计时器)

为了使显示窗口和命令窗口分开工作,我使用多线程策略。它起初工作,但很快,当我写两个以上的字母时,显示器搞砸了。 (右上图)。

window.getstr部分似乎仍然正常工作,因为当我输入quit + <enter>

时,程序将会终止

enter image description here

显示器出了问题,我认为它与移动光标或其他东西有关。任何人都可以请指出如何正确地做到这一点?或者,是否有任何关于使用python和curses创建类似界面的文章?

以下是代码:

import sys, os, json, time, datetime, math, curses, thread

COUNTER = 0

def my_raw_input(window, r, c, prompt_string):
    curses.echo()
    window.addstr(r, c, prompt_string)
    window.refresh()
    input = window.getstr(r + 1, c)
    return input

def count(window):
    global COUNTER
    while True:
        window.addstr(3, 0, '%d'%(COUNTER))
        if COUNTER >= 1000:
            COUNTER = 0
        COUNTER += 1
        window.refresh()

def main(args):
    # create stdscr
    stdscr = curses.initscr()
    stdscr.clear()

    # allow echo, set colors
    curses.echo()
    curses.start_color()
    curses.use_default_colors()

    # define 2 windows
    command_window = curses.newwin(3, 30, 0, 0)
    display_window = curses.newwin(6, 30, 5, 0)
    command_window.border()
    display_window.border()

    # thread to refresh display_window
    thread.start_new_thread(count, (display_window,))

    # main thread, waiting for user's command.
    while True:
        command = my_raw_input(command_window, 0, 0, 'Enter your command :')
        if command == 'quit':
            break
        else:
            command_window.addstr(1, 0, ' '*len(command))

    curses.endwin()

curses.wrapper(main)

修改:与建议的“重复问题”不同,我需要获取字符串,而不是仅仅检测按键。

再次修改:虽然“重复问题”中的答案对我没有帮助,但它会引导我使用此资源,这可能会有所帮助https://repolinux.wordpress.com/2012/10/09/non-blocking-read-from-stdin-in-python/

谢谢。

0 个答案:

没有答案