【问题标题】:Automating a wxPython main menu setup?自动化 wxPython 主菜单设置?
【发布时间】:2015-10-10 20:58:28
【问题描述】:

我正在尝试找到一种方法来压缩和自动构建主菜单(在标题栏下方,带有 fileedithelp 等)在 wxPython 中。

写出每个菜单项都是直接的,但我注意到我在附加、排序 ID 等之间重复了很多次。其次是其他独特的坑,比如我想向特定菜单添加图标,或者如果我有子菜单,它们可能有子菜单等。如果没有一种一致的方式来逐项列出所有内容,只需将信息添加到列表或字典或两者的组合中,我的 wx.Frame 对象将变得非常密集。

除了 3 维数组之外,我看不到一种干净而有组织的方式。即便如此,我也不知道如何统一组织该 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)

我尝试使用 list-to-dictionary 来制作它,因此在引用 ID 时不需要特定的索引号,只需输入关键字即可获取 ID。我只写了一次,它就应用于函数的其余部分。

请注意我必须如何创建一个全新的变量并重复自身,例如 MM_File、MM_Edit、MM_Help,并且每次我都输入类似的信息来追加和绑定。请记住,某些菜单可能需要分隔符,或者菜单中有菜单,或者我可能想在任何这些菜单项旁边使用精灵,所以我试图弄清楚如何组织我的数组来做到这一点.

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

【问题讨论】:

    标签: python user-interface menu wxpython


    【解决方案1】:

    您可以采取多种方法。如果您愿意,可以将菜单生成代码放入辅助函数中。像这样的东西应该可以工作:

    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,尽管我个人觉得这有点限制。

    【讨论】:

    • 我修改了您为辅助函数编写的代码,将其放入从清单读取的 for 循环中。
    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多