1 #!/usr/bin/env python
2
3 # example clist.py
4
5 import gtk
6
7 class CListExample:
8 # User clicked the "Add List" button.
9 def button_add_clicked(self, data):
10 # Something silly to add to the list. 4 rows of 2 columns each
11 drink = [ [ "Milk", "3 Oz" ],
12 [ "Water", "6 l" ],
13 [ "Carrots", "2" ],
14 [ "Snakes", "55" ] ]
15
16 # Here we do the actual adding of the text. It's done once for
17 # each row.
18 for indx in range(4):
19 data.append(drink[indx])
20 return
21
22 # User clicked the "Clear List" button.
23 def button_clear_clicked(self, data):
24 # Clear the list using clear(). This is much faster than
25 # calling remove() once for each row.
26 data.clear()
27 return
28
29 # The user clicked the "Hide/Show titles" button.
30 def button_hide_show_clicked(self, data):
31 # Just a flag to remember the status. 0 = currently visible
32 if self.flag == 0:
33 # Hide the titles and set the flag to 1
34 data.column_titles_hide()
35 self.flag = self.flag+1
36 else:
37 # Show the titles and reset flag to 0
38 data.column_titles_show()
39 self.flag = self.flag-1
40 return
41
42 # If we come here, then the user has selected a row in the list.
43 def selection_made(self, clist, row, column, event, data=None):
44 # Get the text that is stored in the selected row and column
45 # which was clicked in. We will receive it as a pointer in the
46 # argument text.
47 text = clist.get_text(row, column)
48
49 # Just prints some information about the selected row
50 print ("You selected row %d. More specifically you clicked"
51 " in column %d, and the text in this cell is %s\n" % (
52 row, column, text))
53 return
54
55 def __init__(self):
56 titles = [ "Ingredients", "Amount" ]
57 self.flag = 0
58 window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
59 window.set_usize(300, 150)
60
61 window.set_title("GtkCList Example")
62 window.connect("destroy", gtk.mainquit)
63
64 vbox = gtk.GtkVBox(gtk.FALSE, 5)
65 vbox.set_border_width(5)
66 window.add(vbox)
67 vbox.show()
68
69 # Create a scrolled window to pack the CList widget into
70 scrolled_window = gtk.GtkScrolledWindow()
71 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
72
73 vbox.pack_start(scrolled_window, gtk.TRUE, gtk.TRUE, 0)
74 scrolled_window.show()
75
76 # Create the CList. For this example we use 2 columns
77 clist = gtk.GtkCList( 2, titles)
78
79 # When a selection is made, we want to know about it. The callback
80 # used is selection_made, and its code can be found further down
81 clist.connect("select_row", self.selection_made)
82
83 # It isn't necessary to shadow the border, but it looks nice :)
84 clist.set_shadow_type(gtk.SHADOW_OUT)
85
86 # What however is important, is that we set the column widths as
87 # they will never be right otherwise. Note that the columns are
88 # numbered from 0 and up (to 1 in this case).
89 clist.set_column_width(0, 150)
90
91 # Add the CList widget to the vertical box and show it.
92 scrolled_window.add(clist)
93 clist.show()
94
95 # Create the buttons and add them to the window. See the button
96 # tutorial for more examples and comments on this.
97 hbox = gtk.GtkHBox(gtk.FALSE, 0)
98 vbox.pack_start(hbox, gtk.FALSE, gtk.TRUE, 0)
99 hbox.show()
100
101 button_add = gtk.GtkButton("Add List")
102 button_clear = gtk.GtkButton("Clear List")
103 button_hide_show = gtk.GtkButton("Hide/Show titles")
104
105 hbox.pack_start(button_add, gtk.TRUE, gtk.TRUE, 0)
106 hbox.pack_start(button_clear, gtk.TRUE, gtk.TRUE, 0)
107 hbox.pack_start(button_hide_show, gtk.TRUE, gtk.TRUE, 0)
108
109 # Connect our callbacks to the three buttons
110 button_add.connect_object("clicked", self.button_add_clicked, clist)
111 button_clear.connect_object("clicked", self.button_clear_clicked,
112 clist)
113 button_hide_show.connect_object("clicked",
114 self.button_hide_show_clicked,
115 clist)
116
117 button_add.show()
118 button_clear.show()
119 button_hide_show.show()
120
121 # The interface is completely set up so we show the window and
122 # enter the gtk_main loop.
123 window.show()
124
125 def main():
126 gtk.mainloop()
127 return 0
128
129 if __name__ == "__main__":
130 CListExample()
131 main()
|