使用字典的Python控制台菜单

使用字典的Python控制台菜单,python,windows,menu,console,Python,Windows,Menu,Console,我正在使用Python 2.7构建一个Windows应用程序,它需要一个简单的控制台菜单,例如: 做点什么 做点别的 出口 将有多个菜单,主菜单将链接到其他菜单。因此,我试图避免在input==“1”代码时出现一堆代码的情况。与此链接类似。下面的代码正在跳过主菜单并执行第二个菜单中的每个选项。我已经看过了,但是我没有看到为什么它会这样做的逻辑 computer = "" # need a class for each of the options in power_menu class pow

我正在使用Python 2.7构建一个Windows应用程序,它需要一个简单的控制台菜单,例如:

  • 做点什么
  • 做点别的
  • 出口
  • 将有多个菜单,主菜单将链接到其他菜单。因此,我试图避免在input==“1”代码时出现一堆
    代码的情况。与此链接类似。下面的代码正在跳过主菜单并执行第二个菜单中的每个选项。我已经看过了,但是我没有看到为什么它会这样做的逻辑

    computer = ""
    
    # need a class for each of the options in power_menu
    class power:
        def logoff(self, computer):
            print "logging off " + computer
    
        def restart(self, computer):
            print "restarting " + computer
    
        def shutdown(self, computer):
            print "shutting down " + computer
    
    def set_computer():
        global computer
        #os.system("cls")
        # optionally print the banner
        computer = raw_input("Computer: ")
        # check the computer is online
    
        # move to the main menu with the computer parameter
        menu().menu_main(computer)
    
    def func_quit():
        sys.exit()
    
    def invalid(computer):
        #os.system("cls")
        print "INVALID CHOICE!"
        menu().menu_main(computer)
    
    class menu():
        def menu_main(self, computer):
            opts_main = {"1":("Power Options", self.menu_power(computer)),
                "2":("Service Options", self.menu_service(computer)),
                "3":("Service Tag & Warranty", self.menu_warranty(computer)),
                "4":("User Options", self.menu_user(computer)),
                "5":("Change Computer", set_computer),
                "6":("Quit hd-con", func_quit)
                }
            for key in sorted(opts_main.keys()):
                print "\t" + key + ":  " + opts_main[key][0]
            ans = raw_input("Selection: ")
            try:
                opts_main.get(ans, [None, invalid])[1]()
            except Exception, e:
                print e
            #men_sel()
    
        def menu_power(self, computer):
            opts_power = {"1":("Logoff", power().logoff(computer)),
                "2":("Restart", power().restart(computer)),
                "3":("Shutdown", power().shutdown(computer)),
                "4":("Main Menu", menu.menu_main),
                "5":("Quit hd-con", func_quit)
                }
            for key2 in sorted(opts_power.keys()):
                print "\t" + key2+":  " + opts_power[key2][0]
            ans2 = raw_input("Selection: ")
            try:
                opts_power.get(ans2)[1]()
                #pow_sel()
            except:
                raise
    
    上面的输出如下所示

    Computer: asdf
    logging off asdf
    restarting asdf
    shutting down asdf
            1:  Logoff
            2:  Restart
            3:  Shutdown
            4:  Main Menu
            5:  Quit 
    Selection:
    
    我正在寻找关于在控制台菜单中使用字典的指导,现有代码的修复,或者推荐的方向,而不是我所看到的


    提前感谢。

    您的词典作业:

    opts_main = {"1":("Power Options", self.menu_power(computer)), ...}
    
    正在调用
    菜单\u power
    ,并将返回值(
    None
    )存储在元组中。为了避免这种情况,您可以使用以下方法:

    from functools import partial 
    
    opts_main = {"1":("Power Options", partial(self.menu_power, computer)), ...}
    

    一旦我弄清楚partial期望什么以及如何将函数和值传递给它,您的答案就非常有效了。Python的另一个很棒的内置特性我不知道。感谢您的快速回复和详细的样品使用。