10.10. Button Boxes

ButtonBoxes are a convenient way to quickly layout a group of buttons. They come in both horizontal and vertical flavors. You create a new ButtonBox with one of the following calls, which create a horizontal or vertical box, respectively:

  hbutton_box = gtk.HButtonBox()

  vbutton_box = gtk.VButtonBox()

The only methods pertaining to button boxes effect how the buttons are laid out.

The layout of the buttons within the box is set using:

  button_box.set_layout(layout_style)

The layout_style argument can take one of the following values:

  BUTTONBOX_DEFAULT_STYLE
  BUTTONBOX_SPREAD
  BUTTONBOX_EDGE
  BUTTONBOX_START
  BUTTONBOX_END

The current layout_style setting can be retrieved using:

  layout_style = button_box.get_layout()

Buttons are added to a ButtonBox using the usual Container method:

  button_box.add(widget)

The buttonbox.py example program illustrates all the different layout settings for ButtonBoxes. The resulting display is:

The source code for the buttonbox.py program is:

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