From: John H. <ec...@ya...> - 2011-04-13 06:29:07
|
Glad to announce that I got it working!!! -- John Henry > >From: John Henry <ec...@ya...> >To: pyt...@li... >Sent: Tue, April 12, 2011 12:59:22 PM >Subject: Fw: Adding MatplotLib widget to Pythoncard - and ALMOST works perfectly > > >Oops. The image was too big. Reattach. > -- >John Henry > > > >> >>----- Forwarded Message ---- >>From: John Henry <ec...@ya...> >>To: pyt...@li... >>Sent: Tue, April 12, 2011 12:46:40 PM >>Subject: Adding MatplotLib widget to Pythoncard - and ALMOST works perfectly >> >> >>I'm *this* close to a working Matplotlib widget in Pythoncard (see screen shot >>attached). Everything works except somehow all of my mouse events are gone, >>plus the canvas is not getting the the size and paint events. I wish somebody >>more knowledgeable in Pythoncard can help (via private email if you like). >> >> >>From the user perspective, the code looks like below (with some non-essential >>details omitted) - look how simple it is (compare with the original code >>at http://eli.thegreenplace.net/files/prog_code/wx_mpl_bars.py.txt >> >> >> >> >>############## >>""" >>This demo demonstrates how to embed a matplotlib (mpl) plot >>into a Pythoncard GUI application, including: >> >> >>* Using the navigation toolbar >>* Adding data to the plot >>* Dynamically modifying the plot's properties >>* Processing mpl events >>* Saving the plot to a file from a menu >> >> >>The main goal is to serve as a basis for developing rich Pythoncard GUI >>applications featuring mpl plots (using the mpl OO API). >> >> >>Eli Bendersky (el...@gm...) >>License: this code is in the public domain >>Last modified: 30.07.2008 >>""" >>import os >>import pprint >>import random >>import wx >> >> >># The recommended way to use wx with mpl is with the WXAgg >># backend. >># >>import matplotlib >>matplotlib.use('WXAgg') >>from matplotlib.figure import Figure >>from matplotlib.backends.backend_wxagg import \ >> NavigationToolbar2WxAgg as NavigationToolbar >> >> >>from PythonCard import model, dialog >> >> >>from pc_mpl_bars_rsrc import rsrc as BarsFrame_rsrc >> >> >>class BarsFrame(model.MDIParentBackground): >>def __init__(self, parent, rsrc): >># Create the mpl Figure and FigCanvas objects. >># 5x4 inches, 100 dots-per-inch >># >>model.MDIParentBackground.__init__(self, parent, aBgRsrc=rsrc) >>def on_initialize(self, event): >>self.components.slider_width.SetTickFreq(10, 1) >> >> >># Since we have only one plot, we can use add_axes >># instead of add_subplot, but then the subplot >># configuration tool in the navigation toolbar wouldn't >># work. >># >>figure=self.components.canvas.fig >>self.axes = figure.add_subplot(111) >># Create the navigation toolbar, tied to the canvas >># >>self.toolbar = NavigationToolbar(self.components.canvas) >>self.sizer(event) >>self.data = [5, 6, 9, 14] >>self.components.textbox.text=(' '.join(map(str, self.data))) >> >> >>self.draw_figure() >> >> >>def sizer(self, event): >><...sizer code skipped ...> >> >> >>def draw_figure(self): >>""" Redraws the figure >>""" >>str = self.components.textbox.text >>self.data = map(int, str.split()) >>x = range(len(self.data)) >> >> >># clear the axes and redraw the plot anew >># >>self.axes.clear() >>self.axes.grid(self.components.cb_grid.checked) >>self.axes.bar( >>left=x, >>height=self.data, >>width=self.components.slider_width.GetValue() / 100.0, >>align='center', >>alpha=0.44, >>picker=5) >>self.components.canvas.draw() >>def on_exit(self, event): >>self.Destroy() >> >> >>if __name__ == '__main__': >>app = model.Application(BarsFrame, rsrc=BarsFrame_rsrc) >>app.MainLoop() >> >> -- >>John Henry |