2011-07-09 71 views
2

我在ncurses中有一個菜單系統。 選擇其中一個選項可帶您到另一個菜單。但我怎麼回來?Python詛咒:回到以前的菜單

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

在第二個while循環結束後添加x = 0

(你需要x時間重置周圍循環,不只是第一個。從第一個菜單,否則退出將x設置爲「退出」,所以也將退出第二個菜單。)

+0

但我真的不需要使用'break',因爲當有人選擇「2-Previous Menu」時(因爲while!= ord('2')),它會突破內部循環。我只需要以某種方式重新生成第一個菜單。 – tetris11

+0

@tetris:哎呀,對不起,你是對的。看到新的答案。 – katrielalex

+0

啊是的!謝謝! – tetris11