/[ascend]/trunk/pygtk/matplotlib-example/pygladematplotlib.py
ViewVC logotype

Contents of /trunk/pygtk/matplotlib-example/pygladematplotlib.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download) (as text)
Mon Apr 24 02:23:08 2006 UTC (14 years, 5 months ago) by johnpye
File MIME type: text/x-python
File size: 9929 byte(s)
Removed 'interface' directory in trunk/pygtk/interface (moved everything up a level)
Made corresponding changes to SCons* and spec files.
1 #!/usr/bin/env python
2 import sys
3
4
5 import matplotlib
6 matplotlib.use('GTK')
7
8 from matplotlib.figure import Figure
9 from matplotlib.axes import Subplot
10 from matplotlib.backends.backend_gtk import FigureCanvasGTK, NavigationToolbar
11
12 from matplotlib.numerix import arange, sin, pi
13
14 try:
15 import pygtk
16 pygtk.require("2.0")
17 except:
18 pass
19 try:
20 import gtk
21 import gtk.glade
22 except:
23 sys.exit(1)
24
25 host = "***"
26 user = "***"
27 passwd = "***"
28 db = "***"
29
30 class appGui:
31 def __init__(self):
32 gladefile = "project2.glade"
33 self.windowname = "gtkbench"
34 self.wTree = gtk.glade.XML(gladefile, self.windowname)
35 self.win = self.wTree.get_widget("gtkbench")
36 self.win.maximize()
37 dic = {"on_window1_destroy" : gtk.main_quit,
38 "on_button1_clicked" : self.submitDB,
39 "on_button3_clicked" : self.fillTree,
40 "on_notebook1_switch_page" : self.selectNotebookPage,
41 "on_treeview1_button_press_event" : self.clickTree,
42 "on_button2_clicked" : self.createProjectGraph
43 }
44
45 self.wTree.signal_autoconnect(dic)
46 # start with database selection
47 self.wDialog = gtk.glade.XML("project2.glade", "dbSelector")
48
49 # setup matplotlib stuff on first notebook page (empty graph)
50 self.figure = Figure(figsize=(6,4), dpi=72)
51 self.axis = self.figure.add_subplot(111)
52
53 self.axis.set_xlabel('Yepper')
54 self.axis.set_ylabel('Flabber')
55 self.axis.set_title('An Empty Graph')
56 self.axis.grid(True)
57
58 self.canvas = FigureCanvasGTK(self.figure) # a gtk.DrawingArea
59 self.canvas.show()
60 self.graphview = self.wTree.get_widget("vbox1")
61 self.graphview.pack_start(self.canvas, True, True)
62
63 # setup listview for database
64 self.listview = self.wTree.get_widget("treeview1")
65 self.listmodel = gtk.ListStore(str, int, int, str, str)
66 self.listview.set_model(self.listmodel)
67
68 renderer = gtk.CellRendererText()
69
70 column = gtk.TreeViewColumn("Name",renderer, text=0)
71 column.set_clickable(True)
72 column.set_sort_column_id(0)
73 column.connect("clicked", self.createDBGraph)
74 column.set_resizable(True)
75 self.listview.append_column(column)
76 #renderer = gtk.CellRendererText()
77
78 column = gtk.TreeViewColumn("Age",renderer, text=1)
79 column.set_clickable(True)
80 column.set_sort_column_id(1)
81 column.connect("clicked", self.createDBGraph)
82 column.set_resizable(True)
83 self.listview.append_column(column)
84 #self.listview.show()
85
86 column = gtk.TreeViewColumn("Shoesize",renderer, text=2)
87 column.set_clickable(True)
88 column.set_sort_column_id(2)
89 column.connect("clicked", self.createDBGraph)
90 column.set_resizable(True)
91 self.listview.append_column(column)
92 #self.listview.show()
93
94 column = gtk.TreeViewColumn("Created",renderer, text=3)
95 column.set_clickable(True)
96 column.set_sort_column_id(3)
97 column.connect("clicked", self.createDBGraph)
98 column.set_resizable(True)
99 self.listview.append_column(column)
100 #self.listview.show()
101 #renderer = gtk.CellRendererText()
102
103 column = gtk.TreeViewColumn("Updated",renderer, text=4)
104 column.set_clickable(True)
105 column.set_sort_column_id(4)
106 column.connect("clicked", self.createDBGraph)
107 column.set_resizable(True)
108 self.listview.append_column(column)
109
110
111 return
112
113 # callbacks.
114 def submitDB(self, widget):
115 while True:
116 try:
117 name = self.wTree.get_widget("entry1").get_text()
118 age = self.wTree.get_widget("entry2").get_text()
119 size = self.wTree.get_widget("entry3").get_text()
120 assert name != ""
121 assert age != ""
122 assert size != ""
123 dataUsr = name, age, size
124 sd = DBStuff.Eb_db(host, user, passwd, db)
125 sd.subMit(dataUsr)
126 break
127 except AssertionError:
128 self.wDialog = gtk.glade.XML("project2.glade", "dbWarningEmpty")
129 close = self.wDialog.get_widget("dbWarningEmpty")
130 response = close.run()
131 if response == gtk.RESPONSE_CLOSE:
132 close.destroy()
133 break
134 except DBStuff.MySQLdb.IntegrityError:
135 def callback():
136 self.wDialog = gtk.glade.XML("project2.glade", "dbWarningOverwrite")
137 close = self.wDialog.get_widget("dbWarningOverwrite")
138 response = close.run()
139 if response == gtk.RESPONSE_CANCEL:
140 close.destroy()
141 if response == gtk.RESPONSE_OK:
142 sd.delRow(name)
143 wd = DBStuff.Eb_db(host, user, passwd, db)
144 wd.subMit(dataUsr)
145 close.destroy()
146 callback()
147 break
148
149 def fillTree(self, widget):
150 model = self.listmodel
151 self.listmodel.clear()
152 fg = DBStuff.Eb_db(host, user, passwd, db)
153 fg.getData("Age")
154
155 for i in range(len(fg.data)):
156 # note: all data from table "bench" is appended, but that is something you don't want
157 # possible solution: create a seperate table for the listbox (eg. ommit Timestamp, keep it in another table)
158 self.listmodel.append(fg.data[i])
159
160 self.createDBGraph(self)
161
162 def selectNotebookPage(self, widget, notebookpage, page_number):
163 # if database page is selected (nr. 1 for now!), retrieve data
164 if page_number == 1:
165 self.fillTree(self)
166
167 if page_number == 0:
168 print "clicked first tab"
169
170 def clickTree(self, treeview, event):
171 if event.button == 3:
172 x = int(event.x)
173 y = int(event.y)
174 time = event.time
175 pthinfo = treeview.get_path_at_pos(x, y)
176 if pthinfo != None:
177 path, col, cellx, celly = pthinfo
178 treeview.grab_focus()
179 treeview.set_cursor( path, col, 0)
180 self.wDialog = gtk.glade.XML("project2.glade", "treeRightClick")
181 close = self.wDialog.get_widget("treeRightClick")
182 response = close.run()
183 if response == gtk.RESPONSE_OK:
184 close.destroy()
185
186 print x
187 print y
188 print pthinfo
189 #self.popup.popup( None, None, None, event.button, time)
190 return 1
191
192 def createProjectGraph(self, widget):
193
194 while True:
195 try:
196 # empty axis if neccesary, and reset title and stuff
197 self.axis.clear()
198 self.axis.set_xlabel('Yepper')
199 self.axis.set_ylabel('Flabber')
200 self.axis.set_title('A Graph')
201 self.axis.grid(True)
202
203 # get data
204 age = self.wTree.get_widget("entry2").get_text()
205 size = self.wTree.get_widget("entry3").get_text()
206 age != ""
207 size != ""
208
209 N = 1
210 ind = arange(N) # the x locations for the groups
211 width = 0.35 # the width of the bars
212
213 p1 = self.axis.bar(ind, int(age), width, color='r')
214 p2 = self.axis.bar(ind+width, int(size), width, color='y')
215
216 self.axis.legend((p1[0], p2[0]), ("Age", "Size"), shadow = True)
217 #self.axis.set_xticks(ind+width, ('G1') )
218 self.axis.set_xlim(-width,len(ind))
219
220 self.canvas.destroy()
221 self.canvas = FigureCanvasGTK(self.figure) # a gtk.DrawingArea
222 self.canvas.show()
223 self.grahview = self.wTree.get_widget("vbox1")
224 self.grahview.pack_start(self.canvas, True, True)
225 break
226
227 except ValueError:
228 self.wDialog = gtk.glade.XML("project2.glade", "cannotCreateProjGraph")
229 close = self.wDialog.get_widget("cannotCreateProjGraph")
230 response = close.run()
231 if response == gtk.RESPONSE_OK:
232 close.destroy()
233 break
234
235 def createDBGraph(self, widget):
236 self.axis.clear()
237 self.axis.set_xlabel('Samples (n)')
238 self.axis.set_ylabel('Value (-)')
239 self.axis.set_title('Another Graph (click on the columnheader to sort)')
240 self.axis.grid(True)
241
242 # get columns from listmodel
243 age = []
244 for row in self.listmodel:
245 age.append(row[1])
246 size = []
247 for row in self.listmodel:
248 size.append(row[2])
249
250 # get number of rows
251 N = len(age)
252
253 ind = arange(N) # the x locations for the groups
254 width = 0.35 # the width of the bars
255 p1 = self.axis.bar(ind, age, width, color='b')
256 p2 = self.axis.bar(ind+width, size, width, color='r')
257 # destroy graph if it already exists
258 while True:
259 try:
260 self.canvas2.destroy()
261 break
262 except:
263 print "nothing to destroy"
264 break
265
266 self.canvas2 = FigureCanvasGTK(self.figure) # a gtk.DrawingArea
267 self.canvas2.show()
268 self.grahview = self.wTree.get_widget("vbox2")
269 self.grahview.pack_start(self.canvas2, True, True)
270
271 app = appGui()
272 gtk.main()
273
274

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22