matplotlib, wxPanel inside a wxPanel

S

Sam

Hello,

I'm currently creating a GUI, which consists of a main frame that
contains a menu bar,a toolbar and an empty panel, let's call it
main_panel.
Ideally, I'd like the following functionality: upon choosing an item
from the menu bar, a particular graph will be displayed inside
main_panel.

Now I've taken a look at the 'embedding_in_wx' example files that come
with matplotlib. Using this example, I can place a graph in a panel
(let's call it graph_panel), and display this in a frame. I can also
get NavigationToolbar2 to work completely.

On the other hand, when i create graph_panel and put this inside of
main_panel, NavigationToolbar2 does not work. The graph and toolbar are
both displayed, but none of the buttons on the toolbar do anything.

I'm beginning to think that what i'm trying to do isn't actually
possible, and that i'll need to put it in a frame instead, which is a
pity.

Can anyone please advise if what i'm trying to do is possible and if
so, provide a small example? I'm running windows XP, python 2.4,
wxpython 2.6.
 
A

ajaksu

Sam said:

Hi there Sam :)
I'm beginning to think that what i'm trying to do isn't actually
possible, and that i'll need to put it in a frame instead, which is a
pity.

Indeed, if that is the case... as I'll need to do exactly that! But see
below ;)
On the other hand, when i create graph_panel and put this inside of
main_panel, NavigationToolbar2 does not work. The graph and toolbar are
both displayed, but none of the buttons on the toolbar do anything.

I've been bitten by things that sounded similar to this and were
related to sloppy cut'n'paste that resulted in different hierarchies of
wx elements. If you post your code we can try to spot mistakes like
those.

As a general advice, go for a clean frame with a main_panel, a
graph_panel and sprinkle "print" statements (specially around events)
to find out what is happening.
Can anyone please advise if what i'm trying to do is possible and if
so, provide a small example? I'm running windows XP, python 2.4,
wxpython 2.6.

I'll try to get around that one later today, but on win98 :)

Daniel
 
A

ajaksu

It seems to work (only tested with embedding_in_wx4.py). I guess it's
something related to things nesting in a slightly wrong way, right
enough to show up but wrong enough to only show up :)

I hope this helps.
Daniel

Substitute embedding_in_wx4.py's CanvasFrame with:

class CanvasFrame(wxFrame):
def __init__(self):
# Begin just like embedding_in_wx4.py
wxFrame.__init__(self,None,-1,
'CanvasFrame',size=(550,350))

self.figure = Figure(figsize=(5,4), dpi=100)
self.axes = self.figure.add_subplot(111)
t = range(0,30)
s = [randint(1, 30)* random() * x for x in t]
self.axes.plot(t,s)
# Add panels from deepest level
self.main_panel = wxPanel(self, -1)
# Parent is main_panel:
self.graph_panel = wxPanel(self.main_panel, -1)
# self.canvas is child of graph_panel...
self.canvas = FigureCanvas(self.graph_panel, -1, self.figure)
# ... as is textgraph
self.text_graph = wxTextCtrl(self.graph_panel, -1, "Hello!\n" +
"I'm in graph_panel",
style=wxTE_MULTILINE)
self.toolbar = MyNavigationToolbar(self.canvas, True)
self.toolbar.Realize()
self.graph_panel_sizer = wxBoxSizer(wxVERTICAL)

tw, th = self.toolbar.GetSizeTuple()
fw, fh = self.canvas.GetSizeTuple()
self.toolbar.SetSize(wxSize(fw, th))
self.graph_panel_sizer.Add(self.toolbar, 0, wxEXPAND)

# After the toolbar, add its siblings to graph_panel_sizer
self.graph_panel_sizer.Add(self.canvas, 1, wxEXPAND)
self.graph_panel_sizer.Add(self.text_graph)
self.graph_panel.SetSizer(self.graph_panel_sizer)
self.graph_panel.Fit()
# graph_panel is done, just add it to main_panel's sizer.

self.main_panel_sizer = wxBoxSizer(wxVERTICAL)
self.text_in = wxTextCtrl(self.main_panel, -1,
"Inside main_panel",
style=wxTE_MULTILINE)
# Here:
self.main_panel_sizer.Add(self.graph_panel,1, wxEXPAND)
self.main_panel_sizer.Add(self.text_in,0, wxEXPAND)
self.main_panel.SetSizer(self.main_panel_sizer)
self.main_panel.Fit()

self.sizer = wxBoxSizer(wxVERTICAL)
self.sizer.Add(self.main_panel, 1, wxEXPAND)
self.sizer_text = wxBoxSizer(wxHORIZONTAL)
self.text_hi = wxTextCtrl(self, -1, "Hello :)",
style=wxTE_MULTILINE)
self.text_out = wxTextCtrl(self, -1,"Outside main_panel",
style=wxTE_MULTILINE|wxEXPAND)

self.sizer_text.Add(self.text_hi, 0, wxEXPAND)
self.sizer_text.Add(self.text_out, 0, wxEXPAND)
self.sizer.Add(self.sizer_text, 0, wxEXPAND)
EVT_PAINT(self, self.OnPaint)
self.toolbar.update()
self.SetSizer(self.sizer)
self.Fit()

def OnPaint(self, event):
self.canvas.draw()
event.Skip()
 
S

Sam

Hi Daniel,

Thanks very much for your quick response! You're right, it was a case
of the cut-and-paste blues, and me not really knowing what each part of
the code in the examples was actually doing.
A couple of things caught me out: first of all, i wasn't calling
SetSizer and Fit on the right panels...
Secondly, I stuffed up when trying to give graph_panel a parent of
main_panel. It turns out I was actually never doing this, so the
toolbar would appear, but none of the buttons would work.

Thanks again for your comments, they were very helpful,
Cheers
Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top