| PyGTK Tutorial | ||
|---|---|---|
| <<< Previous | Chapter 4. Packing Widgets | Next >>> |

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.mainquit()
16 return gtk.FALSE
17
18 def __init__(self):
19 # Create a new window
20 self.window = gtk.GtkWindow(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.GtkTable(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.GtkButton("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.GtkButton("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.GtkButton("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.mainloop()
80 return 0
81
82 if __name__ =="__main__":
83 Table()
84 main()
|
The Table class is defined in line 7-76. Lines 10-11 define the callback() method which is called when two of the buttons are "clicked". The callback just prints a message to the console indicating which button was pressed using the passed in string data.
Lines 14-16 define the delete_event() method which is called when the window is slated for deleteion by the window manager.
Lines 18-76 define the Table instance initialization method __init__(). It creates a window (line 20), sets the window title (line 23), connects the delete_event() callback to the "delete_event" signal (line 27), and sets the border width (line 30). A GtkTable is created in line 33 and added to the window in line 36.
The two upper buttons are created (lines 39 and 53), their "clicked" signals are connected to the callback() method (lines 43 and 57), and attached to the table in the first row (lines 47 and 59). Lines 64-71 create the "Quit" button, connect it's "clicked" signal to the mainquit() function and attach it to the table spanning the whole second row.
| <<< Previous | Home | Next >>> |
| Packing Using Tables | Up | Widget Overview |