123
[meabook] / list.py
1 #!/usr/bin/python
2 import pygtk
3 pygtk.require('2.0')
4 import gtk
5
6 from database import db 
7
8 class HelloWorld:
9
10     def hello(self, widget, data=None):
11         print "Hello World"
12
13     def delete_event(self, widget, event, data=None):
14         # If you return FALSE in the "delete_event" signal handler,
15         # GTK will emit the "destroy" signal. Returning TRUE means
16         # you don't want the window to be destroyed.
17         # This is useful for popping up 'are you sure you want to quit?'
18         # type dialogs.
19         print "delete event occurred"
20
21         # Change FALSE to TRUE and the main window will not be destroyed
22         # with a "delete_event".
23         return False
24
25     def destroy(self, widget, data=None):
26         print "destroy signal occurred"
27         gtk.main_quit()
28
29     def __init__(self):
30
31         gtk.rc_parse_string("style 'scroll' {\n"
32                             "   engine 'sapwood'{\n"
33                             "       GtkRange::slider-width = 40\n"
34                             "   }\n"
35                             "}\n"
36                             "class 'GtkRange' style 'scroll'")        
37
38         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
39         
40         self.window.set_size_request(600, 360)
41         
42         
43
44         # When the window is given the "delete_event" signal (this is given
45         # by the window manager, usually by the "close" option, or on the
46         # titlebar), we ask it to call the delete_event () function
47         # as defined above. The data passed to the callback
48         # function is NULL and is ignored in the callback function.
49         self.window.connect("delete_event", self.delete_event)
50     
51         # Here we connect the "destroy" event to a signal handler.  
52         # This event occurs when we call gtk_widget_destroy() on the window,
53         # or if we return FALSE in the "delete_event" callback.
54         self.window.connect("destroy", self.destroy)
55     
56         # Sets the border width of the window.
57         self.window.set_border_width(10)
58
59         vbox = gtk.VBox(False, 8)
60
61         sw = gtk.ScrolledWindow()
62         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
63         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
64         #a = gtk.Adjustment(0, 100, 100, 0, 10, 10)
65         #sw.set_vadjustment()
66
67
68         vbox.pack_start(sw, True, True, 0)
69         
70         self.dbo = db.DbSqlite()
71         self.data = self.dbo.get_allrecords('cn')
72
73         store = gtk.ListStore(int, str)
74         for d in self.data:
75             store.append([d, self.data[d]])
76         
77         tree = gtk.TreeView(store)
78         tree.set_rules_hint(True)
79
80         tree.connect("row-activated", self.select_item)
81
82         t1 = gtk.CellRendererText()
83         column = gtk.TreeViewColumn("Id", t1, text=0)
84         column.set_sort_column_id(0)
85         column.set_visible(False)
86         tree.append_column(column)
87
88         t1 = gtk.CellRendererText()
89         column = gtk.TreeViewColumn("Name", t1, text=1)
90         column.set_sort_column_id(0)
91         tree.append_column(column)
92
93
94         sw.add(tree)
95
96        
97
98         self.window.add(vbox)
99         self.window.show_all()
100     
101     
102         # and the window
103         self.window.show()
104
105     def select_item(self, widget, path, column):
106         print "select_item path:%s" % path
107         self.dialog = gtk.Dialog("Item descrition", 
108                                 self.window, 
109                                 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
110                                 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
111         model = widget.get_model()
112         id = model[path][0]
113         rec = self.dbo.get_record(id)
114         print rec
115         text = ""
116         for r in rec:
117             print "%s -> %s" % (r, rec[r])
118             text += "%s:  %s\n" % (r, rec[r])
119
120         #sw = gtk.ScrolledWindow()
121         #sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
122         #sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
123  
124         label = gtk.Label(text)
125         #sw.add(label)
126         #label.set_markup(text)
127         self.dialog.vbox.pack_start(label)
128         label.show()
129         response = self.dialog.run()
130         self.dialog.destroy()
131         print "response= %s" % response
132
133
134     def main(self):
135         # All PyGTK applications must have a gtk.main(). Control ends here
136         # and waits for an event to occur (like a key press or mouse event).
137         gtk.main()
138
139 if __name__ == "__main__":
140     hello = HelloWorld()
141     hello.main()
142