Customising the Standard Menu Bar

By default, a PyGUI application comes with a standard set of menus containing all the commands that the PyGUI framework itself knows about - Open, Close, Save, Cut, Copy, Paste and so forth. While these may be enough for your application, you're likely to want to add further menu commands of your own. PyGUI currently offers a couple of different ways to do this.

Adding menus

The simplest way is to create one or more extra menus containing your commands, and add them to the end of the application's menu bar. Here's an example of how to do this.

# First, create new menu
my_menu = Menu("Widget", [("Swizzle", 'swiz_cmd'), ("Defibrillate", 'defib_cmd')])
# Then get a list of the standard menus
old_menus = my_app.menus
# Create a new list with our menu at the end
new_menus = old_menus + [my_menu]
# Make it the application's menu bar
my_app.menus = new_menus

Note that instead of modifying the application's menu list in-place, we created a new list and assigned it back to the application's menus property. This is important, because assignment to the menus property is necessary to ensure that the menu bar is updated properly.

Modifying standard menus

Adding your own menus is all well and good, but you may want more control than that. For example, if you have some editing-related commands, you might want to add them to the Edit menu instead of putting them in a menu of their own.

The problem with this is finding the right menu to add them to. PyGUI tries to make as few assumptions as possible about the layout of the standard menus, and if you want your application to be portable, you should do the same. So you shouldn't assume, for example, that the Edit menu is the second menu in the menu bar. (On the Mac, it's not!) You shouldn't even assume that there will be an Edit menu at all.

Rather than a particular menu, it's better to think in terms of putting your commands near existing commands. PyGUI helps you out here by means of the MenuList class. A MenuList is just like an ordinary list, except that it also has a method that will take an internal command name and give you the menu which contains that command. So we can find the menu containing, say, the 'copy_cmd' command, and be fairly sure that it's the Edit menu, or whatever passes for it, on the platform we're running on. Once we've found the menu, we can use its append or extend methods to add our commands to it,

Before we do that, however, we'd better make sure we have a new menu that isn't being used anywhere else. We can ensure that by using the basic_menus() function of the StdMenus submodule, which returns a MenuList full of newly-created standard menus that we can modify freely.

Putting it all together, here's how we can add some commands to the Edit menu:

from GUI.StdMenus import basic_menus
# Get a new list of standard menus
my_menus = basic_menus()
# Find the Edit menu or nearest equivalent
edit_menu = my_menus.menu_with_command('copy_cmd')
# Add some items to it. Start with a separator to
# set it off from the existing items.
edit_menu.extend(["-", ("Biggify", 'enlarge_cmd'), ("Smallify", 'reduce_cmd')])
# Finally, install it as the application's menu bar
my_app.menus = my_menus

Future plans

Two further things you might want to do are inserting commands in the middle of a menu (to get them even closer to an existing command), and removing standard commands that your application doesn't use. These are not currently supported, but are planned for a future version.

---