[wxPython-users] Another sizer question

Alun Griffiths alun.griffiths at dsl.pipex.com
Mon Nov 12 11:41:08 PST 2007


Christopher

thanks again for your help.  I just spotted that PlotFrame was based 
on a wx.Frame and not a wx.Panel and have modified the code 
accordingly - that was a big improvement!!  Also changed some of the 
code so that I am ALMOST there - the only problem I now have is that 
the text boxes are on the frame (dark grey in Windows) whereas I 
would like to put them on a panel and have not managed to work out 
how to add a sizer to a panel.  What I would like to do is (a) add 
the text control widgets to LHSIZER (already done) (b) add LHSIZER to 
PANEL (don't know how to do this) then (c) add the PANEL to the box 
sizer BOX.  Revised code is attached

As regards WXMPL, I did use this before but it does not come as part 
of the ENTHOUGHT edition which has been installed on the university 
server.  I though the DIY approach would be easier than hoop-jumping 
associated with getting an "unofficial" package installed on the server.

Best regards

Alun Griffiths

===============================
import wx
import matplotlib
import numpy

# uncomment the following to use wx rather than wxagg
#matplotlib.use('WX')
#from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas

# comment out the following to use wx rather than wxagg
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure



# ==================
# Define main window
# ==================

class TopFrame(wx.Frame):
     """Frame class that acts as top level window"""

     # Constructor for main window

     def __init__(self):

         # Create a Frame instance

         wx.Frame.__init__(self, None, title="Example matplotlib window")

         # Create panel to hold sizer

         panel = wx.Panel(self)

         # Create a sizer to hold the tree and image widgets

         box = wx.BoxSizer(wx.HORIZONTAL)

         # Define data items

         giipLabel  = wx.StaticText(self, wx.ID_ANY, "GIIP (Bscf)")
         giipText   = wx.TextCtrl(self, value="50.0", size=(75,-1))

         presLabel  = wx.StaticText(self, wx.ID_ANY, "initial pressure (psia)")
         presText   = wx.TextCtrl(self, value="3000.0", size=(75,-1))

         tresLabel  = wx.StaticText(self, wx.ID_ANY, "initial 
temperature (deg F)")
         tresText   = wx.TextCtrl(self, value="150.0", size=(75,-1))

         sgLabel    = wx.StaticText(self, wx.ID_ANY, "gas gravity (rel. air)")
         sgText     = wx.TextCtrl(self, value="0.60", size=(75,-1))

         qmaxLabel  = wx.StaticText(self, wx.ID_ANY, "plateau rate (MMscfd)")
         qmaxText   = wx.TextCtrl(self, value="25.0", size=(75,-1))

         nwLabel    = wx.StaticText(self, wx.ID_ANY, "number of wells")
         nwSpinner  = wx.SpinCtrl(self, wx.ID_ANY, size=(50,-1), 
min=1, max=20, name="nwSpinner")


         # Define sizers

         lhsSizer = wx.FlexGridSizer(cols = 3, hgap = 5, vgap = 5)
         lhsSizer.AddGrowableCol(2)      # Allow only data input fields to grow


         # Add widgets to sizers

         lhsSizer.Add((10,10), 1)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add(giipLabel, 0, wx.ALIGN_CENTER_VERTICAL)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add(giipText)
         lhsSizer.Add(presLabel, 0, wx.ALIGN_CENTER_VERTICAL)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add(presText)
         lhsSizer.Add(tresLabel, 0, wx.ALIGN_CENTER_VERTICAL)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add(tresText)
         lhsSizer.Add(sgLabel, 0, wx.ALIGN_CENTER_VERTICAL)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add(sgText)
         lhsSizer.Add(qmaxLabel, 0, wx.ALIGN_CENTER_VERTICAL)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add(qmaxText)
         lhsSizer.Add(nwLabel, 0, wx.ALIGN_CENTER_VERTICAL)
         lhsSizer.Add((10,10), 1)
         lhsSizer.Add(nwSpinner)

         box.Add((10,10),0)
         box.Add(lhsSizer, 0)
         box.Add((10,10),0)

         # Load the plot window and add to sizer

         self.plot = PlotFrame(self)

         box.Add(self.plot, 1, wx.EXPAND | wx.RIGHT | wx.BOTTOM)

         # Add the sizer to the panel

         self.SetSizerAndFit(box)

         # Automatically resize the window

         self.Fit()


# ===============================
# Define frame to display results
# ===============================
#
# Adapted from "embedding_in_wx.py" from MATPLOTLIB example files

class PlotFrame(wx.Panel):
     """Frame class that acts as top level window"""


     # Constructor for main window

     def __init__(self, parent):

         # Create a wxFrame instance

         wx.Panel.__init__(self, parent, wx.ID_ANY, size=(320,100), 
name='Matplotlib window')

         self.SetBackgroundColour(wx.NamedColor("WHITE"))

         # Initialise figure

         self.figure = Figure()

         # Create an Axes object to plot on

         self.ax1 = self.figure.gca()
         self.ax1.yaxis.tick_left()
         self.ax1.xaxis.tick_bottom()

         # Plot the data

         self.lines=[]
         x = numpy.arange( 0.0, 5.0, 0.05)
         y = numpy.sin(2*numpy.pi*x)

         self.lines.append(self.ax1.plot(x, y, '-g'))

         # Set axis titles

         self.ax1.set_xlabel('time (sec)', family='sans-serif')
         self.ax1.set_ylabel('function1', family='sans-serif')

         # Place canvas in sizer in frame

         self.canvas = FigureCanvas(self, -1, self.figure)

         self.sizer = wx.BoxSizer(wx.VERTICAL)
         self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
         self.SetSizer(self.sizer)
         self.Fit()


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




# ========================
# Define application class
# ========================

class App(wx.App):
     """Application class."""

     def OnInit(self):

         # Display top level window

         self.frame = TopFrame()
         self.frame.Show()
         self.SetTopWindow(self.frame)
         return True


def main():
     app = App(False)
     app.MainLoop()

if __name__ == '__main__':
     main()

At 19:21 12/11/2007, you wrote:


>Alun Griffiths wrote:
>>tried both hints but the problem is still 
>>there.  PlotFrame.__init__() also sets a sizer - is the problem 
>>that there is some interference between the sizers?
>
>darn. maybe.
>
>The best way to get help with this sort of thing is to make a 
>small-as-possible, complete runnable app that demonstrates the problem.
>
>I"d start by using a plain wx.Panel instead of a pot panel, to make 
>sure that it's related to the plot window or something else.
>
>But first:
>
> >         self.plot = PlotFrame(self)
>
>are you trying to put a wx.Frame on a wx.Frame? You can't do that! A 
>wx.Frame is top-level window, you need to put a wx.Window or 
>wx.Panel on it, not another wx.Frame.
>
>Are you use wxmpl? I recommend it, it makes it a bit easier to embed 
>MPL in wx.
>
>http://agni.phys.iit.edu/~kmcivor/wxmpl/
>
>-Chris





More information about the wxpython-users mailing list