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

<-^^->

ComboBoxEntry

More powerful replacement for Combo.
The new incarnation of Combo - the ComboBoxEntry and related ComboBox use the TreeModel for data description.
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       # model creation
 34       model = gtk.ListStore( str) 
 35       for text in ["USD","EUR","CZK","GBP"]:
 36         model.append( [text])
 37       combobox = gtk.ComboBoxEntry( model, column=0) 
 38       box.pack_end( combobox, expand=False)
 39       combobox.show()
 40       combobox.connect( "changed", self.selection_changed)
 41       # show the box
 42       self.mainbox.show()
 43   
 44     def selection_changed( self, w):
 45       text = w.get_child().get_text() 
 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( "ComboBoxEntry example")
 57     m.main()
Screenshot:
Program screenshot gtk-comboboxentry1-1.pngProgram screenshot gtk-comboboxentry1-1a.pngProgram screenshot gtk-comboboxentry1-1b.png
Doba běhu: 1288.5 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   import os
  7   
  8   class MyGUI:
  9   
 10     def __init__( self, title):
 11       self.window = gtk.Window()
 12       self.title = title
 13       self.window.set_title( title)
 14       self.window.set_size_request( -1, -1)
 15       self.window.connect( "destroy", self.destroy)
 16       self.prepare_flags()
 17       self.create_interior()
 18       self.window.show_all()
 19   
 20     def prepare_flags( self):
 21       os.system( 'unzip -q flags.zip') # taken from "http://www.markfennell.com/flags/"
 22       f = file( 'flags/countries.csv')
 23       self.flags = {}
 24       for line in f:
 25         code, name = line.strip().split(",")
 26         self.flags[code] = name
 27   
 28     def create_interior( self):
 29       self.mainbox = gtk.VBox()
 30       self.window.add( self.mainbox)
 31       # label
 32       self.label = gtk.Label( "")
 33       self.label.show()
 34       self.mainbox.pack_start( self.label, expand=False, padding=5)
 35       # hbox
 36       box = gtk.HBox()
 37       self.mainbox.pack_end( box, expand=False)
 38       box.show()
 39       # anonymous label before the Combo
 40       l = gtk.Label( "Select country: ")
 41       l.show()
 42       box.pack_start( l, expand=False)
 43       # model creation
 44       model = gtk.ListStore( str, gtk.gdk.Pixbuf) 
 45       keys = self.flags.keys()
 46       keys.sort()
 47       for code in keys:
 48         p = gtk.gdk.pixbuf_new_from_file( "flags/flags/%s.gif" % code)
 49         model.append( [self.flags[code], p])
 50       combobox = gtk.ComboBoxEntry( model, column=0) 
 51       combobox.clear()
 52       cell = gtk.CellRendererPixbuf()
 53       combobox.pack_start( cell, False)
 54       combobox.add_attribute( cell, 'pixbuf', 1)
 55       cell = gtk.CellRendererText()
 56       combobox.pack_start( cell, False)
 57       combobox.add_attribute( cell, 'text', 0)
 58       # pack it
 59       box.pack_end( combobox, expand=False)
 60       combobox.show()
 61       combobox.connect( "changed", self.selection_changed)
 62       # show the box
 63       self.mainbox.show()
 64   
 65     def selection_changed( self, w):
 66       text = w.get_child().get_text() 
 67       self.label.set_markup( "Country <b>%s</b> selected" % text) 
 68   
 69     def main( self):
 70       gtk.main()
 71   
 72     def destroy( self, w):
 73       gtk.main_quit()
 74   
 75   
 76   if __name__ == "__main__":
 77     m = MyGUI( "ComboBoxEntry example II.")
 78     m.main()
Screenshot:
Program screenshot gtk-comboboxentry1-2.pngProgram screenshot gtk-comboboxentry1-2a.pngProgram screenshot gtk-comboboxentry1-2b.png
Doba běhu: 519.2 ms