The Itemfactory is much easier to use, and to add new menus to, although writing a few wrapper functions to create menus using the manual method could go a long way towards usability. With the Itemfactory, it is not possible to add images or the character '/' to the menus.
There are three widgets that go into making a menubar and submenus:
a menu, which acts as a container for the menu items, and
a menubar, which is a container for each of the individual menus.
Let's look at the functions that are used
to create menus and menubars. This first function is used to create a new
menu_bar = GtkMenuBar()
This rather self explanatory function
creates a new menubar. You use the GtkContainer add()
method to pack this into a window, or the GtkBox pack
methods to pack it into a box - the same as buttons.
menu = GtkMenu()
This function returns a reference to a new menu; it is never actually shown (with the show() method), it is just a container for the menu items. I hope this will become more clear when you look at the example below.
The next two calls are used to create
menu items that are packed into the menu (and menubar).
menu_item = GtkMenuItem()
menu_item = GtkMenuItem(label)
These calls are used to create the menu items that are to be displayed. Remember to differentiate between a "menu" as created with GtkMenu() and a "menu item" as created by the GtkMenuItem() functions. The menu item will be an actual button with an associated action, whereas a menu will be a container holding menu items.
The GtkMenuItem() calls are just as you'd expect after reading about the buttons. One creates a new menu item with a label already packed into it, and the other just creates a blank menu item.
Once you've created a menu item you have
to put it into a menu. This is done using the append()
method. In order to capture when the item is selected by the user, we need
to connect to the
signal in the usual way. So, if we wanted to create a standard File
menu, with the options Open,
Quit, the code would
look something like:
file_menu = GtkMenu() # Don't need to show menus # Create the menu items open_item = GtkMenuItem("Open") save_item = GtkMenuItem("Save") quit_item = GtkMenuItem("Quit") # Add them to the menu file_menu.append(open_item) file_menu.append(save_item) file_menu.append(quit_item) # Attach the callback functions to the activate signal open_item.connect_object("activate", menuitem_response, "file.open") save_item.connect_object("activate", menuitem_response, "file.save") # We can attach the Quit menu item to our exit function quit_item.connect_object ("activate", destroy, "file.quit") # We do need to show menu items open_item.show() save_item.show() quit_item.show()
At this point we have our menu. Now we
need to create a menubar and a menu item for the File
entry, to which we add our menu. The code looks like this:
menu_bar = GtkMenuBar() window.add(menu_bar) menu_bar.show() file_item = GtkMenuItem("File") file_item.show()
Now we need to associate the menu with
This is done with the method:
So, our example would continue with:
All that is left to do is to add the menu
to the menubar, which is accomplished using the method:
which in our case looks like this:
If we wanted the menu right justified
on the menubar, such as help menus often are, we can use the following
method (again on
in the current example) before attaching it to the menubar.
Here is a summary of the steps needed to create a menu bar with menus attached:
Use multiple calls to GtkMenuItem() for each item you wish to have on your menu. And use the append() method to put each of these new items on to the menu.
Create a menu item using GtkMenuItem(). This will be the root of the menu, the text appearing here will be on the menubar itself.
Use the set_submenu() method to attach the menu to the root menu item (the one created in the above step).
Create a new menubar using GtkMenuBar(). This step only needs to be done once when creating a series of menus on one menu bar.
Use the append() method to put the root menu onto the menubar.
def handler(widget, event):
and it will use the event to find out where to pop up the menu.
In the event handler, if the event is a mouse button press, treat event as a button event (which it is) and use it as shown in the sample code to pass information to the popup() method.
Bind that event handler to a widget with
widget.connect_object("event", handler, menu)
where widget is the widget you are binding to, handler is the handling function, and menu is a menu created with GtkMenu(). This can be a menu which is also posted by a menu bar, as shown in the sample code.