2011-12-17

PyGTK Alignment Class

In order to understand the layout management in PyGTK, I built a form to experiment:
 
 
It is just a simple form with two buttons at the bottom right corner.  The following is the code:


import gtk
class App(gtk.Window):

    def __init__(self):
        super(App, self).__init__()

        self.set_title('Alignment')
        self.set_size_request(320, 200)
        self.set_position(gtk.WIN_POS_CENTER)

        ok = gtk.Button('OK')
        close = gtk.Button('Close')

        hbox = gtk.HBox(True) # homogeneous children
        hbox.add(ok)
        hbox.add(close)

        align = gtk.Alignment(1, 1, 0, 0) # push to right bottom
        align.add(hbox)
       
        vbox = gtk.VBox()
        vbox.add(align)
        self.add(vbox)

        self.connect('destroy', gtk.main_quit)
        self.show_all()
 
App()
gtk.main()


The layout is managed by a HBox inside an Alignment widget inside a VBox:


The Alignment widget controls the alignment and size of its child.  Let's look at the Alignment widget creation:

    align = gtk.Alignment(1, 1, 0, 0) # push to right bottom


The first "1" effectively pushes the HBox to the right, while the second "1" push the containing HBox to the bottom.

The gtk.Alignment class reference can be found here.