Python-Ref > GUI programming with PyGTK > Widgets > ComboBox
 
 

<-^^->

ComboBox

Drop-down selection from several possibilities.
The new incarnation of Combo - the ComboBoxEntry and related ComboBox use the TreeModel for data description. This makes for a much more powerful widget, with many new features, but it also adds to the complexity of using these widgets.
Fortunately, the developers have added convenience functions for creation of the most common types of ComboBoxes.
The difference between using a helper function and creating the witget on our own the normal way is demonstrated in the code below.
Expand/Shrink
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   
  7   class MyGUI:
  8   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.set_size_request( -1, -1)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       # label
 22       self.label = gtk.Label( "No currency selected")
 23       self.label.show()
 24       self.mainbox.pack_start( self.label, expand=False, padding=5)
 25       # hbox
 26       box = gtk.HBox()
 27       self.mainbox.pack_end( box, expand=False)
 28       box.show()
 29       # anonymous label before the Combo
 30       l = gtk.Label( "Select currency: ")
 31       l.show()
 32       box.pack_start( l, expand=False)
 33       # here we use a convenience function
 34       combobox = gtk.combo_box_new_text() 
 35       for text in ["USD","EUR","CZK","GBP"]:
 36         combobox.append_text( text) 
 37       box.pack_end( combobox, expand=False)
 38       combobox.show()
 39       combobox.connect( "changed", self.selection_changed)
 40       # show the box
 41       self.mainbox.show()
 42   
 43     def selection_changed( self, w):
 44       index = w.get_active() 
 45       text = w.get_model()[index][0] 
 46       self.label.set_markup( "Currency <b>%s</b> selected" % text)
 47   
 48     def main( self):
 49       gtk.main()
 50   
 51     def destroy( self, w):
 52       gtk.main_quit()
 53   
 54   
 55   if __name__ == "__main__":
 56     m = MyGUI( "ComboBox example")
 57     m.main()
Screenshot:
Program screenshot gtk-combobox1-1.pngProgram screenshot gtk-combobox1-1a.pngProgram screenshot gtk-combobox1-1b.png
Doba běhu: 1684.5 ms
Expand/Shrink
Rozdíl proti: gtk-combobox1-1.py
@@ -30,10 +30,15 @@
     l = gtk.Label( "Select currency: ")
     l.show()
     box.pack_start( l, expand=False)
-    # here we use a convenience function
-    combobox = gtk.combo_box_new_text() 
+    # without the convenience function
+    # model creation
+    model = gtk.ListStore( str) 
     for text in ["USD","EUR","CZK","GBP"]:
-      combobox.append_text( text) 
+      model.append( [text])
+    combobox = gtk.ComboBox( model) 
+    cell = gtk.CellRendererText() 
+    combobox.pack_start( cell, True) 
+    combobox.add_attribute( cell, 'text', 0) 
     box.pack_end( combobox, expand=False)
     combobox.show()
     combobox.connect( "changed", self.selection_changed)
Screenshot:
Program screenshot gtk-combobox1-2.pngProgram screenshot gtk-combobox1-2a.pngProgram screenshot gtk-combobox1-2b.png
Doba běhu: 1953.6 ms
Expand/Shrink
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   
  7   class MyGUI:
  8   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.set_size_request( -1, -1)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       # label
 22       self.label = gtk.Label( "No icon selected")
 23       self.label.show()
 24       self.mainbox.pack_start( self.label, expand=False, padding=5)
 25       # hbox
 26       box = gtk.HBox()
 27       self.mainbox.pack_end( box, expand=False)
 28       box.show()
 29       # anonymous label before the Combo
 30       l = gtk.Label( "Select an icon: ")
 31       l.show()
 32       box.pack_start( l, expand=False)
 33       # without the convenience function
 34       # model creation
 35       model = gtk.ListStore( int, gtk.gdk.Pixbuf) 
 36       for i,name in enumerate( gtk.stock_list_ids()):
 37         pixbuf = l.render_icon( stock_id=name, size=gtk.ICON_SIZE_MENU) 
 38         model.append( [i, pixbuf])
 39       combobox = gtk.ComboBox( model) 
 40       cell = gtk.CellRendererText() 
 41       combobox.pack_start( cell, True) 
 42       combobox.add_attribute( cell, 'text', 0) 
 43       cell = gtk.CellRendererPixbuf() 
 44       combobox.pack_start( cell, True) 
 45       combobox.add_attribute( cell, 'pixbuf', 1) 
 46       # set the number of columns to use for display
 47       combobox.set_wrap_width( 5) 
 48       box.pack_end( combobox, expand=False)
 49       combobox.show()
 50       combobox.connect( "changed", self.selection_changed)
 51       # show the box
 52       self.mainbox.show()
 53   
 54     def selection_changed( self, w):
 55       index = w.get_active()
 56       text = w.get_model()[index][0]
 57       self.label.set_markup( "Icon <b>%s</b> selected" % text)
 58   
 59     def main( self):
 60       gtk.main()
 61   
 62     def destroy( self, w):
 63       gtk.main_quit()
 64   
 65   
 66   if __name__ == "__main__":
 67     m = MyGUI( "ComboBox example")
 68     m.main()
Screenshot:
Program screenshot gtk-combobox1-3.pngProgram screenshot gtk-combobox1-3a.pngProgram screenshot gtk-combobox1-3b.png
Doba běhu: 1657.4 ms