grail.widgets.gmenu module

Version: 16.2

Table of Contents

Description

Defines a set of widgets useful for constructing a user menu.

This was made primarily because in some cases in the MineSight® Grail library we need to allow for delayed construction of widgets, but still provide the ability to make an advanced layout. See GPanelApp definemenus method for an example of this concept.

Example

An example of defining a menu structure can be found in the grail.widgets.gpanelapp module. But a small example is illustrated here.

Suppose we want to make a menu bar with a "File" and "Help" menu. The File menu will contain a "Open", "Save", "Save As..." and "Exit" options. The "Help" menu will contain the "About" and "Help" options. Pretty standard menu structure.

We will start with the imports,

from grail.widgets import *

Then we will define our call back functions, for now we will simply leave them empty.

def onopen():
    
pass

def onsave():
    
pass

def onsaveas():
    
pass

def onexit():
    
pass

def onabout():
    
pass

def onhelp():
    
pass

These can be filled in at a later time.

Next, we will define our menu items as a list of GMenuItem objects,

filemenuitems = [GMenuItem("Open", onopen),
                 
GMenuSeparator(),
                 
GMenuItem("Save", onsave),
                 
GMenuItem("Save As", onsaveas),
                 
GMenuSeparator(),
                 
GMenuItem("Exit", onexit)]
helpmenuitems = [GMenuItem("Help", onhelp),
                 
GMenuSeparator(),
                 
GMenuItem("About", onabout)]

Now, lets define our menus, they will each contain the items we just finished writing out.

filemenu = GMenu("File", filemenuitems)
helpmenu = GMenu("Help", helpmenuitems)

Yes, one of those rare cases of limited typing. Soak it up.

All right, back to the building the menu, now the next step is to construct the menu bar.

root = gwidgetinit()
menubar = GMenuBar(root, menus=[filemenu, helpmenu]
menubar.pack(side='top', anchor='nw')
root.mainloop()

The root will be the parent application window and we will have packed the menubar into the top right corner.