开发者

Python Curses: Returning to the Previous Menu

I have a menu system in ncurses. Choosing one of the options takes you to another menu. But how do I get back?

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

Ideally I'd need to use the GOTO module to jump between lines of code, but the device I'm using does not come with that module built-in.

Do you guys know any other methods? Really appreciate any help.

============ Update: ==================

Okay, I also realized that you can regenerate both menu's with ease:

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'):       开发者_StackOverflow中文版     
           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()


Add x = 0 after the second while loop ends.

(You need to reset x every time around the loop, not just the first. Otherwise exiting from the first menu will x set to "exit", so will also exit the second menu.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜