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

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Combo

Select from several options in a drop-down meny.
Although the Combo widget is deprecated in the newer relases of GTK, the simplicity of its use makes it still a viable option for your GUI.
Expand/Shrink
Zdroj: (gtk-combo1-1.py) přispěl(a) Petr Čech
  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( 310, 160)
 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       
 22       # label
 23       self.label = gtk.Label( "")
 24       self.label.set_use_markup( True)
 25       self.mainbox.pack_start( self.label, expand=False, padding=10)
 26       self.label.show()
 27       # form
 28       form = gtk.HBox()
 29       # form label
 30       label = gtk.Label( "Your education")
 31       form.pack_start( label, padding=10)
 32       label.show()
 33       # combo
 34       self.combo = gtk.Combo()
 35       form.pack_start( self.combo, padding=10)
 36       self.combo.show()
 37       self.mainbox.pack_start( form, expand=False)
 38       form.show()
 39       # checkbutton
 40       editable = gtk.CheckButton( "Editable combo")
 41       self.mainbox.pack_end( editable, expand=False)
 42       editable.show()
 43       editable.connect( "toggled", self.editable_toggled)
 44       editable.set_active( True)
 45       # combo settings
 46       self.items = ["Basic", "Secondary", "Bachelor", "Master"]
 47       self.combo.set_popdown_strings(self.items)
 48       self.combo.entry.set_text("Select one")
 49       self.combo.entry.connect( "changed", self.selection_changed)
 50       self.combo.entry.set_editable(True)
 51       # useful methods
 52       self.combo.set_use_arrows(True)      # change using arrow keys 
 53       self.combo.disable_activate()        # prevents showing list
 54       self.combo.set_case_sensitive(False) # case sensitivity
 55   
 56       self.combo.show()
 57   
 58       # show the box
 59       self.mainbox.show()
 60   
 61     def main( self):
 62       gtk.main()
 63   
 64     def destroy( self, w):
 65       gtk.main_quit()
 66   
 67     def selection_changed( self, w):
 68       string = self.combo.entry.get_text()
 69       if string in self.items:
 70           self.label.set_label(string)
 71       else:
 72           self.label.set_label(string + " <b>(User defined)</b>")
 73           
 74     def editable_toggled( self, w):
 75       self.combo.entry.set_editable( w.get_active())
 76   
 77   if __name__ == "__main__":
 78     m = MyGUI( "Combo")
 79     m.main()
stderr:
gtk-combo1-1.py:34: DeprecationWarning: use gtk.ComboBoxEntry
  self.combo = gtk.Combo()
Screenshot:
Program screenshot gtk-combo1-1.pngProgram screenshot gtk-combo1-1a.png
Doba běhu: 5456.8 ms
Check the new version of Combo - the ComboBoxEntry and related ComboBox.