Using Menus

  1. Menu Bars
  2. Popup Menus
  3. Dynamic Menus


Menu Bars

PyUI has a concept of a main menu bar that goes across the top of the screen. This is a MenuBar instance - of which there can be only one active per application. MenuBars automatically expand to the width of the PyUI application and have a height of the TEXT HEIGHT + 4 pixels. The way to use a MenuBar is to add Menus to it with the addMenu method. The titles of these menus will appear in the menu bar and will function like standard menus.

Menus are instances of the class widgets.Menu. They have menu items added to them with the addItem() method. The addItem method takes a title, a handler method and possibly a sub-menu. The handler method will be called with the menuItem instance as the argument. The sub-menu argument can be used to add sub-menu to the menu allowing hierarchies of menus.

Below is an example of adding a menu to an application:

import pyui
def onMenu(item):
    print "got item", item.text
    
pyui.init(320,320,"gl")
newFrame = pyui.widgets.Frame(100, 80, 150, 100, "menu test")
menu1 = pyui.widgets.Menu("StartMenu")
menu1.addItem("one", onMenu)
menu1.addItem("two", onMenu)
menu1.addItem("three", onMenu)
menu1.addItem("four", onMenu)
menu1.addItem("five", onMenu)

mbar = pyui.widgets.MenuBar()
mbar.addMenu(menu1)

pyui.run()
pyui.quit()

This should look like:

Popup Menus

In addition to menus along the Menu Bar, menus can be displayed as pop-ups over Windows or Frames. To use a popup menu, create an instance of the MenuPopup class and attach it to any PyUI

widgets with the addPopup() method. Then, when the right mouse button is pressed over the widget, the popup menu will be invoked. An example of adding a popup menu to a Frame:

import pyui
def onMenu(item):
    print "got item", item.text
    
pyui.init(320,320,"gl")
newFrame = pyui.widgets.Frame(100, 80, 150, 100, "menu test")
menu1 = pyui.widgets.MenuPopup()
menu1.addItem("one", onMenu)
menu1.addItem("two", onMenu)
menu1.addItem("three", onMenu)
menu1.addItem("four", onMenu)
menu1.addItem("five", onMenu)

newFrame.addPopup(menu1)

pyui.run()
pyui.quit()

This should look like:

Dynamic Menus

PyUI is capable of creating menus procedurally at runtime based on data. This can be useful when application objects have actions that they can perform or be performed on them - menus can be created that are specific for application objects.

TODO: add example of dynamic menus...


(C) Sean Riley 2002