User Tools

Site Tools


python:pygtk_menus

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:pygtk_menus [2013/03/16 17:40] (current)
Line 1: Line 1:
 +=== HOW TO BUILD MENUS WITH PYGTK ===
  
 +
 +==== Intro ====
 +
 +This is how it looks like (just to make you curious how it's made):
 +
 +{{:​python:​demomenus.png|DemoMenu Screenshot}}
 +
 +
 +We'll use for this:
 +
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkmenubar.html | gtk.MenuBar() for the menu bar, top level (a widget that displays gtk.MenuItem widgets horizontally)
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkmenu.html | gtk.Menu() a drop down menu widget for the menu items
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkmenuitem.html | gtk.MenuItem() for each item on the toplevel menubar, or the items from submenus
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkimagemenuitem.html | gtk.ImageMenuItem() same as previous, but with image
 +  * http://​www.pygtk.org/​docs/​pygtk/​class-gtkcheckmenuitem.html | gtk.CheckMenuItem() checkbox item
 +
 +
 +==== Step by step tutorial ====
 +
 +So, we start with the menubar:
 +
 +<code python>
 +menubar = gtk.MenuBar()
 +</​code>​
 +
 +Now, we put there some top level menus: File, Edit, About.
 +
 +<code python>
 +filem = gtk.MenuItem("​File"​)
 +editm = gtk.MenuItem("​Edit"​)
 +aboutm = gtk.MenuItem("​About"​)
 +</​code>​
 +
 +Later, we'll add these to the menubar this way:
 +
 +<code python>
 +menubar.append(filem)
 +menubar.append(editm)
 +menubar.append(aboutm)
 +</​code>​
 +
 +
 +Create for File item, the menu which will contain four entries: Open, Save with submenu, one separator and the last item Exit
 +<code python>
 +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)
 +</​code>​
 +
 +
 +
 +The last entry, Exit, is an item with an image and also with accelerator key. Also, it's connected to main_quit event.
 +
 +<code python>
 +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)
 +</​code>​
 +
 +
 +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)
 +
 +<code python>
 +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)
 +</​code>​
 +
 +==== Full Source code ====
 +
 +
 +
 +<code python>
 +#​!/​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()
 +</​code>​
python/pygtk_menus.txt ยท Last modified: 2013/03/16 17:40 (external edit)