Python Curses:返回上一级菜单

发布于 2024-11-19 06:14:44 字数 2336 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-11-26 06:14:44

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文