【问题标题】:Controlling multiple menus with curses individually单独控制带有诅咒的多个菜单
【发布时间】:2020-09-23 13:22:04
【问题描述】:

一直试图弄清楚如何在 curses 中的 2 个单独的菜单之间切换并且遇到了一些困难。

我有 2 个菜单,如下所示:

# 1st Window.
def mainMenu(root, current_row, h, w):
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)

    main_win = curses.newwin(h - 1, w // 2, 1, 1)
    main_win.border()

    main_options = ["Enter data", "View data", "Exit"]

    for idx, element in enumerate(main_options):
        x = 1 + idx
        y = 1

        if idx == current_row:
            main_win.attron(curses.color_pair(1))
            main_win.addstr(y, x, element)
            main_win.attroff(curses.color_pair(1))
        else:
            main_win.addstr(y, x, element)

    main_win.refresh()

# 2nd window.
def secondMenu(root, current_row, h, w):
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)

    second_win = curses.newwin(h - 1, w // 2, 1, w // 2 + 1)
    second_win.border()

    second_options = ["Option 1", "Option 2", "Option 3"]

    for idx, element in enumerate(second_options):
        x = 1 + idx
        y = 1

        if idx == current_row:
            second_win.attron(curses.color_pair(1))
            second_win.addstr(y, x, element)
            second_win.attroff(curses.color_pair(1))
        else:
            second_win.addstr(y, x, element)

    second_win.refresh()


def main(root):
    curses.curs_set(0)

    h, w = root.getmaxyx()

    current_row = 0

    mainMenu(root, current_row, h, w)

    while True:
        key = root.getch()

        if key == curses.KEY_UP and current_row > 0:
            current_row -= 1
        elif key == curses.KEY_DOWN and current_row < 2:
            current_row += 1
        elif key == ord("q"):
            break

        mainMenu(root, current_row, h, w)
        secondMenu(root, current_row, h, w)

    root.refresh()

curses.wrapper(main)

这会输出一个在两个窗口上都可以突出显示的垂直菜单。但是,当我输入时,两个菜单同时滚动。我不确定如何将控制权隔离到一个窗口或另一个。非常感谢任何帮助,谢谢。

【问题讨论】:

    标签: python python-3.x ncurses curses


    【解决方案1】:

    问题是

    如何将控件隔离到一个窗口或另一个。

    curses 库会将光标移动到窗口中调用getch 的当前位置。您的示例在每种情况下都使用 root 窗口,并在每次调用 getch 后重新创建两个菜单(使用已丢弃引用的 newwin 创建一个新窗口)。与其创建(并丢弃窗口信息),不如先执行newwin,然后在循环中使用main_winsecond_win 变量来决定使用哪个,您可以在菜单之间切换(例如,当您读取一个制表符)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2015-06-12
      • 2017-10-14
      • 2010-09-24
      相关资源
      最近更新 更多