El programa de ejemplo table.py hace una ventana con tres botones en una tabla 2x2. Los primeros dos botones se colocarán en la fila superior. Un tercer botón, de salir, se coloca en la fila inferior, ocupando las dos columnas. La Figura 4.4. Empaquetamiento usando una Tabla ilustra la ventana resultante:
Aqui va el código fuente:
1 #!/usr/bin/env python
2
3 # example table.py
4
5 import gtk
6
7 class Table:
8 # Our callback.
9 # The data passed to this method is printed to stdout
10 def callback(self, widget, data=None):
11 print "Hello again - %s was pressed" % data
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("Table")
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 2x2 table
33 table = gtk.Table(2, 2, gtk.TRUE)
34
35 # Put the table in the main window
36 self.window.add(table)
37
38 # Create first button
39 button = gtk.Button("button 1")
40
41 # When the button is clicked, we call the "callback" method
42 # with a pointer to "button 1" as its argument
43 button.connect("clicked", self.callback, "button 1")
44
45
46 # Insert button 1 into the upper left quadrant of the table
47 table.attach(button, 0, 1, 0, 1)
48
49 button.show()
50
51 # Create second button
52
53 button = gtk.Button("button 2")
54
55 # When the button is clicked, we call the "callback" method
56 # with a pointer to "button 2" as its argument
57 button.connect("clicked", self.callback, "button 2")
58 # Insert button 2 into the upper right quadrant of the table
59 table.attach(button, 1, 2, 0, 1)
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", gtk.mainquit)
69
70 # Insert the quit button into the both lower quadrants of the table
71 table.attach(button, 0, 2, 1, 2)
72
73 button.show()
74
75 table.show()
76 self.window.show()
77
78 def main():
79 gtk.main()
80 return 0
81
82 if __name__ =="__main__":
83 Table()
84 main()
|
La clase Table se define entre las lineas 7-76. Las lineas 10-11 definen el método callback() que se llama cuando se hace "click" en dos de los botones. La retrollamada sólo imprime un mensaje en la consola indicando qué botón fue pulsado usando los datos que se le pasan.
Las lineas 14-16 definen el método delete_event() que se llama cuando el manejador de ventanas le pide a la ventana que se elimine.
Las lineas 18-76 definen el constructor de la clase Table __init__() . Crea una ventana (linea 20), le pone el título (linea 23), conecta la retrollamada delete_event() a la señal "delete_event" (linea 27), y le pone el ancho al borde (linea 30). Se crea una gtk.Table en la linea 33 y se añade a la ventana en la linea 36.
Los dos botones superiores se crean (lineas 39 y 53), sus señales "clicked" se conectan al método callback() (lineas 43 y 57), y se añaden a la tabla en la primera fila (lineas 47 y 59). Las lineas 64-71 crean el botón "Quit", conectan su señal "clicked" a la función mainquit() y lo añaden a la tabla ocupando la fila inferior completa.