问题描述
试图弄清楚如何在两个单独的菜单之间进行切换并且有些困难。
# 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,element)
main_win.refresh()
# 2nd window.
def secondMenu(root,curses.COLOR_WHITE)
second_win = curses.newwin(h - 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,element)
second_win.attroff(curses.color_pair(1))
else:
second_win.addstr(y,element)
second_win.refresh()
def main(root):
curses.curs_set(0)
h,w = root.getmaxyx()
current_row = 0
mainMenu(root,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,w)
secondMenu(root,w)
root.refresh()
curses.wrapper(main)
这将输出在两个窗口中都突出显示的垂直菜单。但是,当我输入时,两个菜单会同时滚动。我不太确定如何将控件隔离到一个窗口或另一个窗口。非常感谢您的帮助。
解决方法
问题是
如何将控件隔离到一个窗口或另一个窗口。
curses库会将光标移动到调用getch
的窗口中的当前位置。您的示例在每种情况下都使用 root 窗口,并在每次getch
调用之后重新创建两个菜单(使用被丢弃的newwin
创建一个新窗口)。无需创建(并丢弃窗口信息),先进行newwin
并在循环中使用main_win
或second_win
变量来确定要使用哪个变量,您可以在菜单(例如,当您阅读制表符时)。