::-- ehu4ever [2005-08-09 13:57:07]

1. Toggle Buttons

Toggle Button和普通的按钮在外观上差不多一样,不过你按一下就知道有什么不同了:它有两种状态(一凸一 )。你按一下,它的状态就变化一下。

Toggle Button是Check Button和Radio Button的基础,比如许多Toggle Button的函数都被Check Button和Radio Button继承了。详细情况到时候再说。

下面是建立一个Toggle Button:

  toggle_button = gtk.ToggleButton(label=None)

很明显,这和前面的普通按钮几乎一样。

那么怎样才能知道这种有两种状态的按钮在某一时候处在什么状态呢?就是用get_active()。这种按钮发出的“toggled”事件是我们用得着的,在这个事件的callback中使用get_active()就可以检查按钮的状态了。 这个callback大概是这个样子:

  def toggle_button_callback(widget, data):
      if widget.get_active():
          # If control reaches here, the toggle button is down
      else:
          # If control reaches here, the toggle button is up

如果要强行设置这种双状态按钮的状态,我们可以这样:

  toggle_button.set_active(is_active)

当把is_active定为False或是True时,按钮的状态就被设为弹起或是按下。默认情况下,按钮是弹起的。

当我们用set_active()设定状态时,按钮会发出“clicked”和“toggled”事件。

togglebutton.py示范了Toggle Button的一般使用方法。

Figure 6.2. Toggle Button Example

下面是源代码:

   1 #!/usr/bin/env python
   2 
   3 # example togglebutton.py
   4 
   5 import pygtk
   6 pygtk.require('2.0')
   7 import gtk
   8 
   9 class ToggleButton:
  10     # Our callback.
  11     # The data passed to this method is printed to stdout
  12     def callback(self, widget, data=None):
  13         print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()])
  14 
  15     # This callback quits the program
  16     def delete_event(self, widget, event, data=None):
  17         gtk.main_quit()
  18         return False
  19 
  20     def __init__(self):
  21         # Create a new window
  22         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  23 
  24         # Set the window title
  25         self.window.set_title("Toggle Button")
  26 
  27         # Set a handler for delete_event that immediately
  28         # exits GTK.
  29         self.window.connect("delete_event", self.delete_event)
  30 
  31         # Sets the border width of the window.
  32         self.window.set_border_width(20)
  33 
  34         # Create a vertical box
  35         vbox = gtk.VBox(True, 2)
  36 
  37         # Put the vbox in the main window
  38         self.window.add(vbox)
  39 
  40         # Create first button
  41         button = gtk.ToggleButton("toggle button 1")
  42 
  43         # When the button is toggled, we call the "callback" method
  44         # with a pointer to "button" as its argument
  45         button.connect("toggled", self.callback, "toggle button 1")
  46 
  47 
  48         # Insert button 1
  49         vbox.pack_start(button, True, True, 2)
  50 
  51         button.show()
  52 
  53         # Create second button
  54 
  55         button = gtk.ToggleButton("toggle button 2")
  56 
  57         # When the button is toggled, we call the "callback" method
  58         # with a pointer to "button 2" as its argument
  59         button.connect("toggled", self.callback, "toggle button 2")
  60         # Insert button 2
  61         vbox.pack_start(button, True, True, 2)
  62 
  63         button.show()
  64 
  65         # Create "Quit" button
  66         button = gtk.Button("Quit")
  67 
  68         # When the button is clicked, we call the main_quit function
  69         # and the program exits
  70         button.connect("clicked", lambda wid: gtk.main_quit())
  71 
  72         # Insert the quit button
  73         vbox.pack_start(button, True, True, 2)
  74 
  75         button.show()
  76         vbox.show()
  77         self.window.show()
  78 
  79 def main():
  80     gtk.main()
  81     return 0
  82 
  83 if __name__ == "__main__":
  84     ToggleButton()
  85     main()

The interesting lines are 12-13 which define the callback() method that prints the toggle button label and its state when it is toggled. Lines 45 and 59 connect the "toggled" signal of the toggle buttons to the callback() method.