Centering a panel/widget within another panel

Aaron MacDonald:

I have a panel with a fixed size that I want to center inside its
parent panel. So far I've been trying BoxSizers, but the inner panel
is always put on one side instead of been centered on all sides.

···

---

If I'm understanding correctly, what you need is an item (widget or sizer)
with a fixed size and *borders* with flexible sizes.
A 3 x 3 flexgridsizer can do the trick, see code below.

Anyway, I will add this "exercise" to http://spinecho.ze.cx/ > LearnSizers*.zip

Jean-Michel Fauth, Switzerland.

# -*- coding: iso-8859-1 -*-
# w2k sp4, Python 2.5.1, wxPython 2.8.7.1-ansi

import wx

class ColWin(wx.Window):

     def __init__(self, parent, id, BackColour):
         wx.Window.__init__(self, parent, id, wx.DefaultPosition, \
             wx.DefaultSize, wx.SIMPLE_BORDER)
         self.SetBackgroundColour(BackColour)

class MyPanel307(wx.Panel):

     def __init__(self, parent):
         wx.Panel.__init__(self, parent)
         self.parent = parent

         ItemSize = (200, 100)

         wblue2 = ColWin(self, wx.NewId(), wx.BLUE)
         wblue2.SetSize(ItemSize)

         hgap, vgap = 0, 0
         nrows, ncols = 3, 3
         fgs = wx.FlexGridSizer(nrows, ncols, hgap, vgap)
         b = 0
         fgs.AddMany([(-1, -1),
                      (-1, -1),

                      (-1, -1),
                      (wblue2, 0, wx.ALL, b),
                      (-1, -1),

                      (-1, -1),
                     ])

         fgs.AddGrowableRow(0)
         fgs.AddGrowableRow(2)
         fgs.AddGrowableCol(0)
         fgs.AddGrowableCol(2)

         self.SetSizer(fgs)

         self.parent.SetSize((500, 400))
         self.parent.CentreOnScreen()

class TestFrame(wx.Frame):

     def __init__(self, parent, id):
         sty = wx.DEFAULT_FRAME_STYLE
         pos = (0, 0)
         si = (-1, -1)
         wx.Frame.__init__(self, parent, id, 'test', pos, si, sty)

         self.panel = MyPanel307(self)

if __name__ == '__main__':
      app = wx.App(False)
      frame = TestFrame(None, wx.ID_ANY)
      frame.Show()
      app.MainLoop()