2

我在 ncurses 中有一个菜单系统。选择其中一个选项会将您带到另一个菜单。但是我怎么回去?

import curses

def Main():
   x = 0
   while x!= ord('2'):
       x = screen.getch()
       screen.clear();screen.border();
       screen.addstr(1,1, "Please choose:")
       screen.addstr(3,1, "1 - Another Menu")
       screen.addstr(4,1, "2 - Exit")

       if x==ord('1'):
           y = 0
           while y!= ord('2'):
              y = screen.getch()
              screen.clear();screen.border();
              screen.addstr(1,1, "Please choose from new menu:")
              screen.addstr(3,1, "1 - Do Something new")
              screen.addstr(4,1, "2 - Previous Menu")
              if y == ord('1'): doSomething()

           #Here I exit the internal loop. I need to go back to the previous menu, but I don't know how.
           ##
   ##exit outside loop and close program
   ##
   curses.endwin(); exit();

screen = curses.initscr()
Main()

理想情况下,我需要使用 GOTO 模块在代码行之间跳转,但我使用的设备没有内置该模块。

大家还知道其他方法吗?非常感谢任何帮助。

============ 更新:==================

好的,我还意识到您可以轻松地重新生成两个菜单:

import curses

def Main():
   x = 0
   while x!= ord('2'):           #draws 1st menu
       screen.clear();screen.border();
       screen.addstr(1,1, "Please choose:")
       screen.addstr(3,1, "1 - Another Menu")
       screen.addstr(4,1, "2 - Exit")
       x = screen.getch()         #grab input AFTER first giving options :)
       if x==ord('1'):            
           y = 0
           z = 0
           while y!= ord('2'):    #draws 2nd menu
               screen.clear();screen.border();
               screen.addstr(1,1, "Please choose from new menu:")
               screen.addstr(3,1, "1 - Do Something new")
               screen.addstr(4,1, "2 - Previous Menu")
               screen.addstr(6,1, "current loop : "+str(z))
               y = screen.getch();      #grabs new input
               while z!= -1:            #never breaks from loop unless 'break' is called
                   if y == ord('1'):
                       z += 1           
                       break   #regenerates 2nd menu
                   break   #regenerates 1st menu

           #Here we exit the internal loop.
           ##
##exit outside loop and close program
curses.endwin(); exit();

screen = curses.initscr()
Main()
4

1 回答 1

1

x = 0在第二个 while 循环结束后添加。

x (每次循环都需要重置,而不仅仅是第一个。否则从第一个菜单退出将x设置为“退出”,因此也会退出第二个菜单。)

于 2011-07-09T15:32:17.130 回答