Python-Ref > GUI programming with PyGTK > Widgets > Radio buttons
 
 

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

Radio buttons

Selection between several possibilities.
Check buttons provide a way to select between two possible states. When more options are needed radio buttons may be used.
Expand/Shrink
Zdroj: (gtk-radio1-1.py)
  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( 260, -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       label = gtk.Label( "Select file format:")
 23       # the following aligns the text to left 
 24       label.set_alignment( 0, 0)  
 25       self.mainbox.pack_start( label, padding=10)
 26       label.show()
 27       # first radio button - it is special
 28       # None means that it is not part of any group
 29       radio1 = gtk.RadioButton( None, "Python script")
 30       self.mainbox.pack_start( radio1, expand=False)
 31       radio1.show()
 32       radio1.connect( "toggled", self.selection_changed, "Python script")
 33       for text in ["XML","HTML","Bash script"]:
 34         radio = gtk.RadioButton( radio1, text)
 35         self.mainbox.pack_start( radio, expand=False)
 36         radio.connect( "toggled", self.selection_changed, text)
 37         radio.show()
 38       # current selection
 39       self.label = gtk.Label()
 40       self.label.set_use_markup( True)
 41       self.mainbox.pack_start( self.label, padding=10)
 42       self.label.show()
 43       # set the first button active - emits a toggled signal,
 44       # but only if it changes the current state
 45       radio1.set_active( True)
 46       # this forces the 'toggled' signal to be emitted 
 47       radio1.toggled()  
 48       # show the box
 49       self.mainbox.show()
 50   
 51     def main( self):
 52       gtk.main()
 53   
 54     def destroy( self, w):
 55       gtk.main_quit()
 56   
 57     def selection_changed( self, w, data=None):
 58       self.label.set_label( "Current selection: <b>%s</b>" % data)
 59   
 60   
 61   if __name__ == "__main__":
 62     m = MyGUI( "Radio buttons")
 63     m.main()
Screenshot:
Program screenshot gtk-radio1-1.pngProgram screenshot gtk-radio1-1a.png
Doba běhu: 10500.3 ms