Los botones de activación heredan muchas propiedades y métodos de los botones biestado vistos anteriormente, pero su apariencia es un poco diferente. En vez de ser botones con texto dentro de ellos, son pequeñas cajas con un texto a su derecha. Normalmente se utilizan para opciones que pueden estar activadas o desactivadas en las aplicaciones.
El método de creación es similar que el de los botones normales.
check_button = gtk.CheckButton(label=None)
|
Si el argumento label se especifica el método crea un botón de activación con una etiqueta a su lado. El texto de la etiqueta se analiza para comprobar si contiene caracteres memotécnicos con prefijo '_'
Ver y modificar el estado de un botón de activación es idéntico que en un botón biestado.
El programa checkbutton.py proporciona un ejemplo del uso de los botones de activación. La figura Figura 6.3. Ejemplo de Botón de Activación ilustra la ventana resultante:
El código fuente del programa checkbutton.py es:
1 #!/usr/bin/env python
2
3 # example checkbutton.py
4
5 import gtk
6
7 class CheckButton:
8 # Our callback.
9 # The data passed to this methods is printed to stdout
10 def callback(self, widget, data=None):
11 print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()]) 12
13 # This callback quits the program
14 def delete_event(self, widget, event, data=None):
15 gtk.main_quit()
16 return gtk.FALSE
17
18 def __init__(self):
19 # Create a new window
20 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
21
22 # Set the window title
23 self.window.set_title("Check Button")
24
25 # Set a handler for delete_event that immediately
26 # exits GTK.
27 self.window.connect("delete_event", self.delete_event)
28
29 # Sets the border width of the window.
30 self.window.set_border_width(20)
31
32 # Create a vertical box
33 vbox = gtk.VBox(gtk.TRUE, 2)
34
35 # Put the vbox in the main window
36 self.window.add(vbox)
37
38 # Create first button
39 button = gtk.CheckButton("check button 1")
40
41 # When the button is toggled, we call the "callback" method
42 # with a pointer to "button" as its argument
43 button.connect("toggled", self.callback, "check button 1")
44
45
46 # Insert button 1 into the upper left quadrant of the table
47 vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
48
49 button.show()
50
51 # Create second button
52
53 button = gtk.CheckButton("check button 2")
54
55 # When the button is toggled, we call the "callback" method
56 # with a pointer to "button 2" as its argument
57 button.connect("toggled", self.callback, "check button 2")
58 # Insert button 2 into the upper right quadrant of the table
59 vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
60
61 button.show()
62
63 # Create "Quit" button
64 button = gtk.Button("Quit")
65
66 # When the button is clicked, we call the mainquit function
67 # and the program exits
68 button.connect("clicked", lambda wid: gtk.main_quit())
69
70 # Insert the quit button into the both lower quadrants of the table
71 vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
72
73 button.show()
74 vbox.show()
75 self.window.show()
76
77 def main():
78 gtk.main()
79 return 0
80
81 if __name__ == "__main__":
82 CheckButton()
83 main()
|