简体   繁体   English

字典中的wxPython菜单:如何以正确的顺序显示菜单在字典中的显示顺序?

[英]wxPython menu from dictionary: How to display menu in the right order it appear in the dictionary?

I got a dictionary, where the key is the menu and the values are the menu items, and I want use it to create the MenuBar for my program, but I'm getting trouble when the menus are created, they don't appear in order I want, to appear like this: 我有一个字典,其中的键是菜单,值是菜单项,我想用它来为我的程序创建MenuBar,但是在创建菜单时遇到了麻烦,它们没有出现在菜单中我想要的顺序如下所示:

'File | '文件| Edit | 编辑 Tools | 工具| Help' 救命'

Here's my code: 这是我的代码:

# -*- coding: utf-8 -*-
__author__ = 'Nilson Lopes'

import wx


class MainFrame(wx.Frame):
    __menu_options = {'File': ['New', 'Open', 'SEP', 'Quit'],
                  'Edit': ['Copy\tCtrl+C', 'Cut\tCtrl+X', 'Paste\tCtrl+V'],
                  'Tools': ['Settings'],
                  'Help': ['Context', 'About']}

    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)
        self.create_menu()

    '''
    def on_new(self, event):
        print 'New called from function'
        event.Skip()

    def on_open(self, event):
        print 'Open called from function'
        event.Skip()

    __actions__ = {'New': on_new(), 'Open': on_open()}
   '''
   def create_menu(self):
       menu_bar = wx.MenuBar()
       for option, menu_items in self.__menu_options.items():
           #print option
           if type(menu_items) is list:
               main_menu = wx.Menu()
               for menu in self.__menu_options[option]:
                   #print '\t', menu
                   if menu == 'SEP':
                       main_menu.AppendSeparator()
                   else:
                       main_menu.Append(wx.NewId(), str(menu))

           menu_bar.Append(main_menu, '&'+str(option).capitalize())
       self.SetMenuBar(menu_bar)


class MainApp(wx.App):

    def OnInit(self):
        self.frame = MainFrame(None, title='Main Application')
        self.frame.Show()
        return True


def app_run():
    app = MainApp(False)
    app.MainLoop()

if __name__ == '__main__':
    app_run()

Details: I'm using python 2.7 with wxpython(Phoenix) running on windows 8 详细信息:我将python 2.7与在Windows 8上运行的wxpython(Phoenix)一起使用

dict is not an ordered mapping. dict不是有序映射。

>>> d = {'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4}
>>> list(d) # Iterating `dict` yields keys in abitrary order.
['key3', 'key2', 'key1', 'key4']

You should use collections.OrderedDict if you want keep order of items. 如果要保持项目顺序,则应使用collections.OrderedDict

>>> d = OrderedDict([('key1', 1), ('key2', 2), ('key3', 3), ('key4', 4)])
>>> list(d)
['key1', 'key2', 'key3', 'key4']

from collections import OrderedDict

....

__menu_options = OrderedDict([
    ('File', ['New', 'Open', 'SEP', 'Quit']),
    ('Edit', ['Copy\tCtrl+C', 'Cut\tCtrl+X', 'Paste\tCtrl+V']),
    ('Tools', ['Settings']),
    ('Help', ['Context', 'About']),
])

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM