home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / sizegroup.py < prev    next >
Text File  |  2010-05-11  |  4KB  |  109 lines

  1. #!/usr/bin/env python
  2. """Size Group
  3.  
  4. GtkSizeGroup provides a mechanism for grouping a number of widgets together so
  5. they all request the same amount of space. This is typically useful when you
  6. want a column of widgets to have the same size, but you can't use a GtkTable
  7. widget.
  8.  
  9. Note that size groups only affect the amount of space requested, not the size
  10. that the widgets finally receive. If you want the widgets in a GtkSizeGroup to
  11. actually be the same size, you need to pack them in such a way that they get
  12. the size they request and not more. For example, if you are packing your
  13. widgets into a table, you would not include the GTK_FILL flag."""
  14.  
  15. import gtk
  16.  
  17. class SizeGroupDemo(gtk.Dialog):
  18.     def __init__(self, parent=None):
  19.         gtk.Dialog.__init__(self, "Size Groups", parent,
  20.             0,
  21.             (gtk.STOCK_CLOSE,  gtk.RESPONSE_CLOSE))
  22.         try:
  23.             self.set_screen(parent.get_screen())
  24.         except AttributeError:
  25.             self.connect('destroy', lambda *w: gtk.main_quit())
  26.         self.connect('response', lambda d, r: d.destroy())
  27.         self.set_resizable(False)
  28.  
  29.         vbox = gtk.VBox(False, 5)
  30.         self.vbox.pack_start(vbox, True, True, 0)
  31.         vbox.set_border_width(5)
  32.  
  33.         self.size_group = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
  34.  
  35.         # Create one frame holding color options
  36.         frame = gtk.Frame("Color options")
  37.         vbox.pack_start(frame, True, True, 0)
  38.  
  39.         table = gtk.Table(2, 2, False)
  40.         table.set_border_width(5)
  41.         table.set_row_spacings(5)
  42.         table.set_col_spacings(10)
  43.         frame.add(table)
  44.  
  45.         color_options = ("Red", "Green", "Blue")
  46.         self.__add_row(table, 0, "_Foreground", color_options)
  47.         self.__add_row(table, 1, "_Background", color_options)
  48.  
  49.         # And another frame holding line style options
  50.         frame = gtk.Frame("Line options")
  51.         vbox.pack_start(frame, False, False, 0)
  52.  
  53.         table = gtk.Table(2, 2, False)
  54.         table.set_border_width(5)
  55.         table.set_row_spacings(5)
  56.         table.set_col_spacings(10)
  57.         frame.add(table)
  58.  
  59.         dash_options  = ("Solid", "Dashed", "Dotted")
  60.         end_options   = ("Square", "Round", "Arrow")
  61.         self.__add_row(table, 0, "_Dashing", dash_options)
  62.         self.__add_row(table, 1, "_Line ends", end_options)
  63.  
  64.         # And a check button to turn grouping on and off
  65.  
  66.         check_button = gtk.CheckButton("_Enable grouping")
  67.         vbox.pack_start(check_button, False, False, 0)
  68.         check_button.set_active(True)
  69.         check_button.connect('toggled', self.on_toggle_grouping)
  70.  
  71.         self.show_all()
  72.  
  73.     def __create_option_menu(self, options):
  74.  
  75.         option_menu = gtk.combo_box_new_text()
  76.         for opt in options:
  77.             option_menu.append_text(opt)
  78.  
  79.         option_menu.set_active(0)
  80.         return option_menu
  81.  
  82.     def __add_row(self, table, row, label_text, options):
  83.         label = gtk.Label(label_text)
  84.         label.set_use_underline(True)
  85.         label.set_alignment(0, 1)
  86.         table.attach(label, 0, 1, row, row + 1, gtk.EXPAND | gtk.FILL, 0, 0, 0)
  87.  
  88.         option_menu = self.__create_option_menu(options)
  89.         label.set_mnemonic_widget(option_menu)
  90.         self.size_group.add_widget(option_menu)
  91.         table.attach(option_menu, 1, 2, row, row + 1, 0, 0, 0, 0)
  92.  
  93.     def on_toggle_grouping(self, check_button):
  94.  
  95.         # gtk.SIZE_GROUP_NONE is not generally useful, but is useful
  96.         # here to show the effect of gtk.SIZE_GROUP_HORIZONTAL by
  97.         # contrast.
  98.         if check_button.get_active():
  99.             self.size_group.set_mode(gtk.SIZE_GROUP_HORIZONTAL)
  100.         else:
  101.             self.size_group.set_mode(gtk.SIZE_GROUP_NONE)
  102.  
  103. def main():
  104.     SizeGroupDemo()
  105.     gtk.main()
  106.  
  107. if __name__ == '__main__':
  108.     main()
  109.