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

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

Check buttons

Yes-no selection widgets.
Check buttons provide a simple way to allow the user to select one of two possible states. The program can at any time query the state of the check button to modify its behavious accordingly.
Expand/Shrink
Zdroj: (gtk8-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( 200, 100)
 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       # first check button
 22       self.check1 = gtk.CheckButton( "Option 1")
 23       self.mainbox.pack_start( self.check1, expand=False)
 24       self.check1.show()
 25       # second check button
 26       self.check2 = gtk.CheckButton( "Option 2")
 27       self.mainbox.pack_start( self.check2, expand=False)
 28       self.check2.set_active( True)
 29       self.check2.show()
 30       # normal button
 31       button = gtk.Button( "Check selection")
 32       button.connect( "clicked", self.check_selection)
 33       self.mainbox.pack_end( button, expand=False)
 34       button.show()
 35       # show the box
 36       self.mainbox.show()
 37   
 38     def main( self):
 39       gtk.main()
 40   
 41     def destroy( self, w):
 42       gtk.main_quit()
 43   
 44     def check_selection( self, w, data=None):
 45       print "Option 1 get_active() is %s;" % self.check1.get_active(),
 46       print "Option 2 get_active() is %s" % self.check2.get_active()
 47   
 48   
 49   if __name__ == "__main__":
 50     m = MyGUI( "Check buttons")
 51     m.main()
stdout:
Option 1 get_active() is False; Option 2 get_active() is True
Option 1 get_active() is True; Option 2 get_active() is True
Screenshot:
Program screenshot gtk8-1.pngProgram screenshot gtk8-1a.png
Doba běhu: 14929.1 ms
The check button also sends a signal "toggled" when its state was changed. We can use this to react immediately to such action.
Expand/Shrink
Zdroj: (gtk8-2.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( 200, 80)
 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( "Check button is <b>off</b>")
 23       self.label.set_use_markup( True)
 24       self.mainbox.pack_start( self.label)
 25       self.label.show()
 26       # first check button
 27       check = gtk.CheckButton( "Check button")
 28       self.mainbox.pack_start( check, expand=False)
 29       check.show()
 30       check.connect( "toggled", self.check_button_toggled)
 31       # show the box
 32       self.mainbox.show()
 33   
 34     def main( self):
 35       gtk.main()
 36   
 37     def destroy( self, w):
 38       gtk.main_quit()
 39   
 40     def check_button_toggled( self, w, data=None):
 41       if w.get_active():
 42         active = "on"
 43       else:
 44         active = "off"
 45       self.label.set_label( "Check button is <b>%s</b>" % active) 
 46   
 47   
 48   if __name__ == "__main__":
 49     m = MyGUI( "Check buttons II.")
 50     m.main()
Screenshot:
Program screenshot gtk8-2.pngProgram screenshot gtk8-2a.png
Doba běhu: 8304.0 ms