Hello,
My problem is is that I have an area in the window for an image that is sized relative to that window. I want to place a panel below that image a certain distance from the top based on the height of image after resizing the window occurs(on idle event). So is there a way to reposition a panel, say x pixels(the new height of the image after resize) from the top and do this? I've tried doing some things but I get odd results. Or possible other methods?

Recommended Answers

All 6 Replies

If you use sizers you should be able to let the sizers automatically resize themselves so that the image will always have enough space and that the other panel will shrink to accommodate for it.

Have a look at them here:
http://wiki.wxpython.org/UsingSizers

I tried using sizers but the panel wouldn't shift upon resizing the window. I have something like:

class PlotPanel (wx.Panel):
    def __init__( self, parent, **kwargs ):

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(initial_fig, 0, flags) #add image already specified
        vbox.Add(any widget) #adding widget/panel already specified

        self.SetSizer(vbox)
        self.show(True)

        self.Bind(wx.EVT_IDLE, self._onIdle)
        self.Bind(wx.EVT_SIZE, self._onSize)

    def _onIdle(self, event):
        #new image w/ new size stored after resize here

    def_onSize(self,event):
        #store final window size..

It works fine on initial everything in right place, but after the resize the image changes to right size fine but the lower widget/panel I'm trying to add is in the wrong position.

can you post your code? I cannot understand what you posted.
From my little experience, one of common problems is specifying different parent to which the sizer is set. If you use one panel as parent to your widgets then you must use that panel's SetSizer() method. DON'T use panel as parent and use self (which is probably a wx.Frame) to set a sizer.

Wait for your code

I have pasted below a similar example of what I'm trying to do that I found from here: http://www.scipy.org/Matplotlib_figure_in_a_wx_panel

Here the figure re-sizes itself based upon the client window size. How would I add a widget below the figure that works upon re-size.

#!/usr/bin/env python

"""
New version based on the original by Edward Abraham, incorporating some
forum discussion, minor bugfixes, and working for Python 2.5.2,
wxPython 2.8.7.1, and Matplotlib 0.98.3.

I haven't found an advantage to using the NoRepaintCanvas, so it's removed.

John Bender, CWRU, 10 Sep 08
"""

import matplotlib
matplotlib.interactive( True )
matplotlib.use( 'WXAgg' )

import numpy as num
import wx

class PlotPanel (wx.Panel):
    """The PlotPanel has a Figure and a Canvas. OnSize events simply set a 
flag, and the actual resizing of the figure is triggered by an Idle event."""
    def __init__( self, parent, color=None, dpi=None, **kwargs ):
        from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
        from matplotlib.figure import Figure

        # initialize Panel
        if 'id' not in kwargs.keys():
            kwargs['id'] = wx.ID_ANY
        if 'style' not in kwargs.keys():
            kwargs['style'] = wx.NO_FULL_REPAINT_ON_RESIZE
        wx.Panel.__init__( self, parent, **kwargs )

        # initialize matplotlib stuff
        self.figure = Figure( None, dpi )
        self.canvas = FigureCanvasWxAgg( self, -1, self.figure )
        self.SetColor( color )

        self._SetSize()
        self.draw()

        self._resizeflag = False

        self.Bind(wx.EVT_IDLE, self._onIdle)
        self.Bind(wx.EVT_SIZE, self._onSize)

    def SetColor( self, rgbtuple=None ):
        """Set figure and canvas colours to be the same."""
        if rgbtuple is None:
            rgbtuple = wx.SystemSettings.GetColour( wx.SYS_COLOUR_BTNFACE ).Get()
        clr = [c/255. for c in rgbtuple]
        self.figure.set_facecolor( clr )
        self.figure.set_edgecolor( clr )
        self.canvas.SetBackgroundColour( wx.Colour( *rgbtuple ) )

    def _onSize( self, event ):
        self._resizeflag = True

    def _onIdle( self, evt ):
        if self._resizeflag:
            self._resizeflag = False
            self._SetSize()

    def _SetSize( self ):
        pixels = tuple( self.parent.GetClientSize() )
        self.SetSize( pixels )
        self.canvas.SetSize( pixels )
        self.figure.set_size_inches( float( pixels[0] )/self.figure.get_dpi(),
                                     float( pixels[1] )/self.figure.get_dpi() )

    def draw(self): pass # abstract, to be overridden by child classes

if __name__ == '__main__':
    class DemoPlotPanel (PlotPanel):
        """Plots several lines in distinct colors."""
        def __init__( self, parent, point_lists, clr_list, **kwargs ):
            self.parent = parent
            self.point_lists = point_lists
            self.clr_list = clr_list

            # initiate plotter
            PlotPanel.__init__( self, parent, **kwargs )
            self.SetColor( (255,255,255) )

        def draw( self ):
            """Draw data."""
            if not hasattr( self, 'subplot' ):
                self.subplot = self.figure.add_subplot( 111 )

            for i, pt_list in enumerate( self.point_lists ):
                plot_pts = num.array( pt_list )
                clr = [float( c )/255. for c in self.clr_list[i]]
                self.subplot.plot( plot_pts[:,0], plot_pts[:,1], color=clr )

    theta = num.arange( 0, 45*2*num.pi, 0.02 )

    rad0 = (0.8*theta/(2*num.pi) + 1)
    r0 = rad0*(8 + num.sin( theta*7 + rad0/1.8 ))
    x0 = r0*num.cos( theta )
    y0 = r0*num.sin( theta )

    rad1 = (0.8*theta/(2*num.pi) + 1)
    r1 = rad1*(6 + num.sin( theta*7 + rad1/1.9 ))
    x1 = r1*num.cos( theta )
    y1 = r1*num.sin( theta )

    points = [[(xi,yi) for xi,yi in zip( x0, y0 )],
              [(xi,yi) for xi,yi in zip( x1, y1 )]]
    clrs = [[225,200,160], [219,112,147]]

    app = wx.PySimpleApp( 0 )
    frame = wx.Frame( None, wx.ID_ANY, 'WxPython and Matplotlib', size=(300,300) )
    panel = DemoPlotPanel( frame, points, clrs )
    frame.Show()
    app.MainLoop()

Well.. canvas figure is based upon window size, and if there is a widget below it, it would cause the figure to increase size while idle. Adding the figure simply to a sizer rather than basing it upon window size doesn't seem to work either. The figure appears but can't be shrunk less than original declared size. It also slow because matplotlib is memory intensive. I can get a widget to appear below using setposition/move by height offset of figure but when I try doing the exact same thing with a subpanel it doesn't seem to work. I'm not sure what else is needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.