Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0dccead

Browse files
committed
Added embedding_in_wx3 example, which is a pretty full-featured wx/XRC
app. - ADS svn path=/trunk/matplotlib/; revision=397
1 parent 8e1d147 commit 0dccead

3 files changed

Lines changed: 219 additions & 0 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
New entries should be added at the top
22

3+
2004-07-10 Added embedding_in_wx3 example - ADS
4+
35
2004-07-09 Added dynamic_image_wxagg to examples - ADS
46

57
2004-07-09 added support for embedding TrueType fonts in PS files - PEB

examples/data/embedding_in_wx3.xrc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" ?>
2+
<resource>
3+
<object class="wxFrame" name="MainFrame">
4+
<title>embedding_in_wx3</title>
5+
<object class="wxPanel" name="MainPanel">
6+
<object class="wxBoxSizer">
7+
<orient>wxVERTICAL</orient>
8+
<object class="sizeritem">
9+
<object class="wxStaticText">
10+
<label>Check out this whizz-bang stuff!</label>
11+
<style>wxALIGN_CENTRE</style>
12+
</object>
13+
<option>0</option>
14+
<flag>wxALL|wxEXPAND</flag>
15+
<border>5</border>
16+
</object>
17+
<object class="sizeritem">
18+
<object class="wxBoxSizer">
19+
<orient>wxHORIZONTAL</orient>
20+
<object class="sizeritem">
21+
<object class="wxButton" name="whiz_button">
22+
<label>whiz</label>
23+
</object>
24+
<option>0</option>
25+
<flag>wxALL|wxEXPAND</flag>
26+
<border>2</border>
27+
</object>
28+
<object class="sizeritem">
29+
<object class="wxButton" name="bang_button">
30+
<label>bang</label>
31+
</object>
32+
<option>0</option>
33+
<flag>wxALL|wxEXPAND</flag>
34+
<border>2</border>
35+
</object>
36+
<object class="sizeritem">
37+
<object class="wxStaticText" name="">
38+
<label>bang count:</label>
39+
<style>wxALIGN_RIGHT</style>
40+
</object>
41+
<option>1</option>
42+
<flag>wxALL|wxEXPAND</flag>
43+
<border>2</border>
44+
</object>
45+
<object class="sizeritem">
46+
<object class="wxTextCtrl" name="bang_count">
47+
<value>0</value>
48+
</object>
49+
<option>0</option>
50+
<flag>wxEXPAND</flag>
51+
</object>
52+
</object>
53+
<option>0</option>
54+
<flag>wxLEFT|wxRIGHT|wxEXPAND</flag>
55+
<border>5</border>
56+
</object>
57+
<object class="sizeritem">
58+
<object class="wxPanel" name="plot_container_panel"/>
59+
<option>1</option>
60+
<flag>wxEXPAND</flag>
61+
</object>
62+
</object>
63+
</object>
64+
</object>
65+
</resource>

examples/embedding_in_wx3.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env python
2+
"""
3+
Copyright (C) 2003-2004 Andrew Straw, Jeremy O'Donoghue and others
4+
5+
License: This work is licensed under the PSF. A copy should be included
6+
with this source code, and is also available at
7+
http://www.python.org/psf/license.html
8+
9+
This is yet another example of using matplotlib with wx. Hopefully
10+
this is pretty full-featured:
11+
12+
- both matplotlib toolbar and WX buttons manipulate plot
13+
- full wxApp framework, including widget interaction
14+
- XRC (XML wxWidgets resource) file to create GUI (made with XRCed)
15+
16+
This was derived from embedding_in_wx and dynamic_image_wxagg.
17+
18+
Thanks to matplotlib and wx teams for creating such great software!
19+
20+
"""
21+
import sys, time, os, gc
22+
import matplotlib
23+
matplotlib.use('WXAgg')
24+
from matplotlib import rcParams
25+
rcParams['image.origin'] = 'upper' # 'lower': nav toolbar problem -ADS
26+
import matplotlib.cm as cm
27+
from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg
28+
from matplotlib.figure import Figure
29+
import matplotlib.numerix as numerix
30+
from wxPython.wx import *
31+
from wxPython.xrc import *
32+
33+
ERR_TOL = 1e-5 # floating point slop for peak-detection
34+
35+
class PlotPanel(wxPanel):
36+
37+
def __init__(self, parent):
38+
wxPanel.__init__(self, parent, -1)
39+
40+
self.fig = Figure((5,4), 75)
41+
self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
42+
self.toolbar = Toolbar(self.canvas) #matplotlib toolbar
43+
self.toolbar.Realize()
44+
#self.toolbar.set_active([0,1])
45+
46+
# Now put all into a sizer
47+
sizer = wxBoxSizer(wxVERTICAL)
48+
# This way of adding to sizer allows resizing
49+
sizer.Add(self.canvas, 1, wxLEFT|wxTOP|wxGROW)
50+
# Best to allow the toolbar to resize!
51+
sizer.Add(self.toolbar, 0, wxGROW)
52+
self.SetSizer(sizer)
53+
self.Fit()
54+
55+
def init_plot_data(self):
56+
a = self.fig.add_subplot(111)
57+
self.x = numerix.arange(120.0)*2*numerix.pi/60.0
58+
self.x.resize((100,120))
59+
self.y = numerix.arange(100.0)*2*numerix.pi/50.0
60+
self.y.resize((120,100))
61+
self.y = numerix.transpose(self.y)
62+
z = numerix.sin(self.x) + numerix.cos(self.y)
63+
self.im = a.imshow( z, cmap=cm.jet)#, interpolation='nearest')
64+
65+
zmax = numerix.max(numerix.max(z))-ERR_TOL
66+
ymax_i, xmax_i = numerix.nonzero(
67+
numerix.greater_equal(z, zmax))
68+
if self.im.origin == 'upper':
69+
ymax_i = z.shape[0]-ymax_i
70+
self.lines = a.plot(xmax_i,ymax_i,'ko')
71+
72+
self.toolbar.update() # Not sure why this is needed - ADS
73+
74+
def GetToolBar(self):
75+
# You will need to override GetToolBar if you are using an
76+
# unmanaged toolbar in your frame
77+
return self.toolbar
78+
79+
def OnWhiz(self,evt):
80+
self.x += numerix.pi/15
81+
self.y += numerix.pi/20
82+
z = numerix.sin(self.x) + numerix.cos(self.y)
83+
self.im.set_array(z)
84+
85+
zmax = numerix.max(numerix.max(z))-ERR_TOL
86+
ymax_i, xmax_i = numerix.nonzero(
87+
numerix.greater_equal(z, zmax))
88+
if self.im.origin == 'upper':
89+
ymax_i = z.shape[0]-ymax_i
90+
self.lines[0].set_data(xmax_i,ymax_i)
91+
92+
self.canvas.draw()
93+
94+
def onEraseBackground(self, evt):
95+
# this is supposed to prevent redraw flicker on some X servers...
96+
pass
97+
98+
class MyApp(wxApp):
99+
def OnInit(self):
100+
self.res = wxXmlResource("data/embedding_in_wx3.xrc")
101+
102+
# main frame and panel ---------
103+
104+
self.frame = self.res.LoadFrame(None,"MainFrame")
105+
self.panel = XRCCTRL(self.frame,"MainPanel")
106+
107+
# matplotlib panel -------------
108+
109+
# container for matplotlib panel (I like to make a container
110+
# panel for our panel so I know where it'll go when in XRCed.)
111+
plot_container = XRCCTRL(self.frame,"plot_container_panel")
112+
sizer = wxBoxSizer(wxVERTICAL)
113+
114+
# matplotlib panel itself
115+
self.plotpanel = PlotPanel(plot_container)
116+
self.plotpanel.init_plot_data()
117+
118+
# wx boilerplate
119+
sizer.Add(self.plotpanel, 1, wxEXPAND)
120+
plot_container.SetSizer(sizer)
121+
122+
# whiz button ------------------
123+
124+
whiz_button = XRCCTRL(self.frame,"whiz_button")
125+
EVT_BUTTON(whiz_button, whiz_button.GetId(),
126+
self.plotpanel.OnWhiz)
127+
128+
# bang button ------------------
129+
130+
bang_button = XRCCTRL(self.frame,"bang_button")
131+
EVT_BUTTON(bang_button, bang_button.GetId(),
132+
self.OnBang)
133+
134+
# final setup ------------------
135+
136+
sizer = self.panel.GetSizer()
137+
self.frame.Show(1)
138+
139+
self.SetTopWindow(self.frame)
140+
141+
return True
142+
143+
def OnBang(self,event):
144+
bang_count = XRCCTRL(self.frame,"bang_count")
145+
bangs = bang_count.GetValue()
146+
bangs = int(bangs)+1
147+
bang_count.SetValue(str(bangs))
148+
149+
if __name__ == '__main__':
150+
app = MyApp(0)
151+
app.MainLoop()
152+

0 commit comments

Comments
 (0)