Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > matplotlib, wxPanel inside a wxPanel

Reply
Thread Tools

matplotlib, wxPanel inside a wxPanel

 
 
Sam
Guest
Posts: n/a
 
      08-12-2006
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.

 
Reply With Quote
 
 
 
 
ajaksu
Guest
Posts: n/a
 
      08-12-2006
Sam wrote:
> Hello,


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

 
Reply With Quote
 
 
 
 
ajaksu
Guest
Posts: n/a
 
      08-12-2006
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()

 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      08-13-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with wxPanel derivation class none Python 6 10-12-2007 08:31 PM
[wxPython] Many wxPanel forms in 1 wxFrame CYBER Python 2 05-02-2005 11:59 PM
wxNotebook on a wxPanel.. how? phark52@yahoo.com Python 1 03-15-2005 12:41 AM
wxPython: Fit() works on wxPanel, but not on surrounding wxFrame Piet Python 1 05-20-2004 05:34 PM
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl Andy Eshtry ASP .Net 0 03-01-2004 11:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57