Python Curses:返回上一级菜单

时间:2011-07-09 15:20:03

标签: python menu curses

我在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()

1 个答案:

答案 0 :(得分:1)

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

(您需要在循环周围的每个时间重置x ,而不仅仅是第一个。否则退出第一个菜单x将设置为“退出”,因此也将退出第二个菜单。)