User Tools

Site Tools


python:pygtk_menus

HOW TO BUILD MENUS WITH PYGTK

Intro

This is how it looks like (just to make you curious how it's made):

DemoMenu Screenshot

We'll use for this:

Step by step tutorial

So, we start with the menubar:

menubar = gtk.MenuBar()

Now, we put there some top level menus: File, Edit, About.

filem = gtk.MenuItem("File")
editm = gtk.MenuItem("Edit")
aboutm = gtk.MenuItem("About")

Later, we'll add these to the menubar this way:

menubar.append(filem)
menubar.append(editm)
menubar.append(aboutm)

Create for File item, the menu which will contain four entries: Open, Save with submenu, one separator and the last item Exit

filemenu = gtk.Menu()
filem.set_submenu(filemenu)
 
open = gtk.MenuItem("Open")
filemenu.append(open)
 
savesub = gtk.Menu()
savesub.append(gtk.MenuItem("Save Now..."))
savesub.append(gtk.MenuItem("Save Later..."))
 
save = gtk.MenuItem("Save")
save.set_submenu(savesub)
filemenu.append(save)
 
separat = gtk.SeparatorMenuItem()
filemenu.append(separat)

The last entry, Exit, is an item with an image and also with accelerator key. Also, it's connected to main_quit event.

agr = gtk.AccelGroup()
self.add_accel_group(agr)
 
quitImg = gtk.ImageMenuItem(gtk.STOCK_QUIT, agr)
key, mod = gtk.accelerator_parse("Q")
quitImg.add_accelerator("activate", agr, key, mod, gtk.ACCEL_VISIBLE)
filemenu.append(quitImg)

We've finished for the moment with File menu. Now we deal with the next one, Edit in which we'll put a single item, check box type:
(change_message is a custom function called on activate event; check the full source code below for this)

editmenu = gtk.Menu()
editm.set_submenu(editmenu) 
 
stat = gtk.CheckMenuItem("Check example")
stat.set_active(True)
stat.connect("activate", self.change_message)
editmenu.append(stat)

Full Source code

#!/usr/bin/env python
 
import pygtk
pygtk.require('2.0')
import gtk
 
class MenuApp(gtk.Window):
    def __init__(self):
        super(MenuApp, self).__init__()
 
        self.set_title("Demo Menus")
        self.set_size_request(400, 500)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
        self.set_position(gtk.WIN_POS_CENTER)
 
        # top level menu bar
        menubar = gtk.MenuBar()
 
        # top items on the menu bar
        filem = gtk.MenuItem("File")
        editm = gtk.MenuItem("Edit")
        aboutm = gtk.MenuItem("About")
 
        # now, create for FILE item the menu
        filemenu = gtk.Menu()
        filem.set_submenu(filemenu)        
 
        # create the items for File menu
        open = gtk.MenuItem("Open")
        filemenu.append(open)
 
        savesub = gtk.Menu()
        savesub.append(gtk.MenuItem("Save Now..."))
        savesub.append(gtk.MenuItem("Save Later..."))
        # attach the submenu to Save Item
        save = gtk.MenuItem("Save")
        save.set_submenu(savesub)
        filemenu.append(save)
 
        # separator
        separat = gtk.SeparatorMenuItem()
        filemenu.append(separat)
 
        # now, Quit item with accelerator and image
        # generic accelerator
        agr = gtk.AccelGroup()
        self.add_accel_group(agr)
 
        quitImg = gtk.ImageMenuItem(gtk.STOCK_QUIT, agr)
        key, mod = gtk.accelerator_parse("Q")
        quitImg.add_accelerator("activate", agr, key, mod, gtk.ACCEL_VISIBLE)
        filemenu.append(quitImg)
 
        # connect to Quit the activate event
        quitImg.connect("activate", gtk.main_quit)
 
        # EDIT item
        # create the edit menu and attach it to the top item
        editmenu = gtk.Menu()
        editm.set_submenu(editmenu) 
 
        # create the check menu item and put it on edit menu previously created
        stat = gtk.CheckMenuItem("Check example")
        stat.set_active(True)
        stat.connect("activate", self.change_message)
        editmenu.append(stat)
 
 
        # append the top items
        menubar.append(filem)
        menubar.append(editm)
        menubar.append(aboutm)
 
        # pack in a vbox
        vbox = gtk.VBox(False, 2)
        vbox.pack_start(menubar, False, False, 0)
 
        # add the vbox to the window
        self.add(vbox)
 
        self.connect("destroy", gtk.main_quit)
        self.show_all()
 
    def change_message(self, widget):
        ''' used for CheckMenuItem '''
        if widget.active:
            msg = gtk.MessageDialog(self, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Checked")
        else:
            msg = gtk.MessageDialog(self, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Unchecked")
 
        msg.show()
 
 
MenuApp()
gtk.main()

python/pygtk_menus.txt · Last modified: 2013/03/16 17:40 (external edit)