2015-10-10 22 views
1

我试图找到一种方式来凝聚和自动化主菜单的结构(标题栏的下方,与文件编辑帮助等)在wxPython中。自动化wxPython主菜单设置?

写出每一个菜单项都是直接的,但我注意到我在重复自己,重复添加,排序ID等等之后,跟着其他独特的坑,如果我想添加一个图标到特定的菜单,或如果我有子菜单,并且它们可能有子菜单等。如果没有一个一致的方法来逐项列出所有内容,只需将信息添加到列表或字典或两者的组合中,我的wx.Frame对象就会变得非常密集。

我看不到一个干净的有组织的方式做到这一点,缺乏一个三维数组。即使如此,我也不知道如何统一组织这个3D阵列,所以每个项目都准备好了。

这里是我到目前为止(请原谅任何缩进错误;它正常工作对我):

class frameMain(wx.Frame): 
    """The main application frame.""" 
    def __init__(self, 
       parent=None, 
       id=-1, 
       title='TITLE', 
       pos=wx.DefaultPosition, 
       size=wx.Size(550, 400), 
       style=wx.DEFAULT_FRAME_STYLE): 
     """Initialize the Main frame structure.""" 
     wx.Frame.__init__(self, parent, id, title, pos, size, style) 
     self.Center() 
     self.CreateStatusBar() 

     self.buildMainMenu() 

    def buildMainMenu(self): 
     """Creates the main menu at the top of the screen.""" 
     MainMenu = wx.MenuBar() 

     # Establish menu item IDs. 
     menuID_File = ['exit'] 
     menuID_Help = ['about'] 
     menuID_ALL = [menuID_File, 
         menuID_Help] 

     # Make a dictionary of the menu item IDs. 
     self.menuID = {} 
     for eachmenu in menuID_ALL: 
      for eachitem in eachmenu: 
       self.menuID[eachitem] = wx.NewId() 

     # Create the menus. 
     MM_File = wx.Menu() 
     FILE = {} 
     MM_File.AppendSeparator() 
     FILE['exit'] = MM_File.Append(self.menuID['exit'], 
             'Exit', 
             'Exit application.') 
     self.Bind(wx.EVT_MENU, self.onExit, FILE['exit']) 
     MainMenu.Append(MM_File, 'File') 

     MM_Help = wx.Menu() 
     HELP = {} 
     MM_Help.AppendSeparator() 
     HELP['about'] = MM_Help.Append(self.menuID['about'], 
             'About', 
             'About the application.') 
     self.Bind(wx.EVT_MENU, self.onAbout, HELP['about']) 
     MainMenu.Append(MM_Help, 'Help') 

     # Install the Main Menu. 
     self.SetMenuBar(MainMenu) 

我尝试使用列表对字典的东西做,所以我不需要一个特定的索引号在引用一个ID时,只需写入一个关键字并获得该ID。我写了一次,它应用于函数的其余部分。

请注意,我必须创建一个全新的变量并重复自己,比如MM_File,MM_Edit,MM_Help,并且每次我都输入类似的信息来追加和绑定。请记住,某些菜单可能需要分隔符,或菜单中有菜单,或者我可能想要在这些菜单项旁边使用精灵,所以我正在尝试计算如何组织我的数组来执行此操作。

将此组织成一个简洁的系统的适当方法是什么,所以它不会膨胀这个类?

回答

0

您可以采取几种方法。如果你喜欢,你可以把菜单生成代码放入一个辅助函数中。像这样的东西应该工作:

def menu_helper(self, menu, menu_id, name, help, handler, sep=True): 
    menu_obj = wx.Menu() 
    if sep: 
     menu_obj.AppendSeparator() 
    menu_item = menu_obj.Append(menu_id, name, help) 
    self.Bind(wx.EVT_MENU, handler, menu_item) 
    self.MainMenu.Append(menu_obj, menu) 

这里有一个完整的例子:

import wx 

class frameMain(wx.Frame): 
    """The main application frame.""" 
    def __init__(self, 
       parent=None, 
       id=-1, 
       title='TITLE', 
       pos=wx.DefaultPosition, 
       size=wx.Size(550, 400), 
       style=wx.DEFAULT_FRAME_STYLE): 
     """Initialize the Main frame structure.""" 
     wx.Frame.__init__(self, parent, id, title, pos, size, style) 
     self.Center() 
     self.CreateStatusBar() 

     self.buildMainMenu() 

    def buildMainMenu(self): 
     """Creates the main menu at the top of the screen.""" 
     self.MainMenu = wx.MenuBar() 

     # Establish menu item IDs. 
     menuID_File = 'exit' 
     menuID_Help = 'about' 
     menuID_ALL = [menuID_File, 
         menuID_Help] 

     # Make a dictionary of the menu item IDs. 
     self.menuID = {item: wx.NewId() for item in menuID_ALL} 


     # Create the menus. 

     self.menu_helper('File', self.menuID['exit'], 'Exit', 
         'Exit application', self.onExit) 


     self.menu_helper('Help', self.menuID['about'], 'About', 
         'About the application.', self.onAbout) 

     # Install the Main Menu. 
     self.SetMenuBar(self.MainMenu) 

    def menu_helper(self, menu, menu_id, name, help, handler, sep=True): 
     """ 
     """ 
     menu_obj = wx.Menu() 
     if sep: 
      menu_obj.AppendSeparator() 
     menu_item = menu_obj.Append(menu_id, name, help) 
     self.Bind(wx.EVT_MENU, handler, menu_item) 
     self.MainMenu.Append(menu_obj, menu) 

    #---------------------------------------------------------------------- 
    def onExit(self, event): 
     pass 

    def onAbout(self, event): 
     pass 

if __name__ == '__main__': 
    app = wx.App(False) 
    frame = frameMain() 
    frame.Show() 
    app.MainLoop() 

或者你可以创建一个可以处理所有的菜单中创建一个类。您也可以创建一个配置文件,其中包含您读取的所有信息以创建菜单。另一种选择是使用XRC,但我个人觉得这有点限制。

+0

我修改了你为辅助函数所做的代码,把它放在从清单读取的for循环中。 –