10.10. Button Boxes

Button Boxes are a convenient way to quickly layout a group of buttons. They come in both horizontal and vertical flavours. You create a new Button Box with one of the following calls, which create a horizontal or vertical box, respectively:
 
hbutton_box = GtkHButtonBox()

vbutton_box = GtkVButtonBox()

The only methods pertaining to button boxes effect how the buttons are laid out. You can change the spacing between the buttons with:
 
button_box.set_spacing_default(spacing)

Similarly, the current spacing values can be queried using:
 
spacing = button_box.get_spacing_default()

The second method affects the layout of the buttons within the box. It is set using:
 
button_box.set_layout_default(layout)

The layout argument can take one of the following values:
 
  BUTTONBOX_DEFAULT_STYLE
  BUTTONBOX_SPREAD
  BUTTONBOX_EDGE
  BUTTONBOX_START
  BUTTONBOX_END

The current layout setting can be retrieved using:
 
layout = button_box.get_layout_default()

Buttons are added to a Button Box using the usual Container method:
 
    button_box.add(child_widget)

The buttonbox.py example program illustrates all the different layout settings for Button Boxes. Figure 10.8 shows the resulting display:

Figure 10.8 Button Box Example

The source code for the buttonbox.py program is:
 
    1   #!/usr/bin/env python
    2   
    3   # example buttonbox.py
    4   
    5   import gtk
    6   
    7   class ButtonBoxExample:
    8       # Create a Button Box with the specified parameters
    9       def create_bbox(self, horizontal, title, spacing,
   10                       child_w, child_h, layout):
   11           frame = gtk.GtkFrame(title)
   12   
   13           if horizontal:
   14               bbox = gtk.GtkHButtonBox()
   15           else:
   16               bbox = gtk.GtkVButtonBox()
   17   
   18           bbox.set_border_width(5)
   19           frame.add(bbox)
   20   
   21           # Set the appearance of the Button Box
   22           bbox.set_layout(layout)
   23           bbox.set_spacing(spacing)
   24           bbox.set_child_size(child_w, child_h)
   25   
   26           button = gtk.GtkButton("OK")
   27           bbox.add(button)
   28   
   29           button = gtk.GtkButton("Cancel")
   30           bbox.add(button)
   31   
   32           button = gtk.GtkButton("Help")
   33           bbox.add(button)
   34   
   35           return frame
   36   
   37       def __init__(self):
   38           window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   39           window.set_title("Button Boxes")
   40   
   41           window.connect("destroy", gtk.mainquit)
   42   
   43           window.set_border_width(10)
   44   
   45           main_vbox = gtk.GtkVBox(gtk.FALSE, 0)
   46           window.add(main_vbox)
   47   
   48           frame_horz = gtk.GtkFrame("Horizontal Button Boxes")
   49           main_vbox.pack_start(frame_horz, gtk.TRUE, gtk.TRUE, 10)
   50   
   51           vbox = gtk.GtkVBox(gtk.FALSE, 0)
   52           vbox.set_border_width(10)
   53           frame_horz.add(vbox)
   54   
   55           vbox.pack_start(self.create_bbox(gtk.TRUE, "Spread (spacing 40)",
   56                                            40, 85, 20, gtk.BUTTONBOX_SPREAD),
   57                           gtk.TRUE, gtk.TRUE, 0)
   58   
   59           vbox.pack_start(self.create_bbox(gtk.TRUE, "Edge (spacing 30)",
   60                                            30, 85, 20, gtk.BUTTONBOX_EDGE),
   61                           gtk.TRUE, gtk.TRUE, 5)
   62   
   63           vbox.pack_start(self.create_bbox(gtk.TRUE, "Start (spacing 20)",
   64                                            20, 85, 20, gtk.BUTTONBOX_START),
   65                           gtk.TRUE, gtk.TRUE, 5)
   66   
   67           vbox.pack_start(self.create_bbox(gtk.TRUE, "End (spacing 10)",
   68                                            10, 85, 20, gtk.BUTTONBOX_END),
   69                           gtk.TRUE, gtk.TRUE, 5)
   70   
   71           frame_vert = gtk.GtkFrame("Vertical Button Boxes")
   72           main_vbox.pack_start(frame_vert, gtk.TRUE, gtk.TRUE, 10)
   73   
   74           hbox = gtk.GtkHBox(gtk.FALSE, 0)
   75           hbox.set_border_width(10)
   76           frame_vert.add(hbox)
   77   
   78           hbox.pack_start(self.create_bbox(gtk.FALSE, "Spread (spacing 5)",
   79                                            5, 85, 20, gtk.BUTTONBOX_SPREAD),
   80                           gtk.TRUE, gtk.TRUE, 0)
   81   
   82           hbox.pack_start(self.create_bbox(gtk.FALSE, "Edge (spacing 30)",
   83                                            30, 85, 20, gtk.BUTTONBOX_EDGE),
   84                           gtk.TRUE, gtk.TRUE, 5)
   85   
   86           hbox.pack_start(self.create_bbox(gtk.FALSE, "Start (spacing 20)",
   87                                            20, 85, 20, gtk.BUTTONBOX_START),
   88                           gtk.TRUE, gtk.TRUE, 5)
   89   
   90           hbox.pack_start(self.create_bbox(gtk.FALSE, "End (spacing 20)",
   91                                            20, 85, 20, gtk.BUTTONBOX_END),
   92                           gtk.TRUE, gtk.TRUE, 5)
   93   
   94           window.show_all()
   95   
   96   def main():
   97       # Enter the event loop
   98       gtk.mainloop()
   99       return 0
  100   
  101   if __name__ == "__main__":
  102       ButtonBoxExample()
  103       main()