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

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