When I have one item in a sizer, why won't it center in both axes?

All I want is one widget in my Frame. I tried a few variations with just a single sizer, and also two sizers… neither is centering the widget.

import wx

class testFrame(wx.Frame):

def __init__(self):

    wx.Frame.__init__(self, parent=None, title="animate test", size=(200, 200))

    panel = wx.Panel(self, -1)

    sizer1 = wx.BoxSizer(wx.VERTICAL)

    sizer2 = wx.BoxSizer(wx.HORIZONTAL)

    btn = wx.Button(panel, -1, "Simple Widget")

    sizer1.AddF(btn, wx.SizerFlags(0).Center())

    sizer2.AddF(sizer1,wx.SizerFlags(1).Expand().Center())

    panel.SetSizer(sizer1)

    panel.Layout()

if name == “main”:

app = wx.App(False)

frame = testFrame()

frame.Show()

app.MainLoop()

my Animation widget to be in the

I don't know why the BoxSizers don't work as one would expect,
but:

class testFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, parent=None, title="animate test", size=(200, 200))
         panel = wx.Panel(self, -1)
         btn = wx.Button(panel, -1, "Simple Widget")
         sizer = wx.GridSizer(1, 1, 5, 5)
         sizer.Add(btn, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTRE_VERTICAL )
         panel.SetSizer(sizer)

does it.
Adapted from here:
http://wxpython-users.1045709.n5.nabble.com/Centering-a-StaticText-on-a-panel-tp2367054p2367055.html

Michael

···

On Wed, 16 Jul 2014 21:05:45 +0200, Nathan McCorkle <nmz787@gmail.com> wrote:

All I want is one widget in my Frame. I tried a few variations with just a
single sizer, and also two sizers... neither is centering the widget.

I use wx.ALIGN_CENTRE which combines the two above, and it works well for
me. I did not see any specific alignment directives in Michael's posted
code.

Rich

···

On Wed, 16 Jul 2014, Michael Ross wrote:

      sizer.Add(btn, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTRE_VERTICAL )

Thanks, I ended up using AddStretchSpacer(1) before and after the widget, and this seems to have worked for the square GIF I was placing in my real app code. The problem was when I Hide() the GIF and Show() a progress Gauge, the Gauge would obviously not be as large as the Frame. I got around this by calling Remove() on the indices of the StretchSpacers after I Hide and Show the GIF and Gauge.

Glad to find this snippet, as I may need to do this with other widgets that don’t behave like the GIF does!

···

On Wednesday, July 16, 2014 1:28:59 PM UTC-7, Michael Ross wrote:

On Wed, 16 Jul 2014 21:05:45 +0200, Nathan McCorkle nmz...@gmail.com > > wrote:

All I want is one widget in my Frame. I tried a few variations with just

a

single sizer, and also two sizers… neither is centering the widget.

I don’t know why the BoxSizers don’t work as one would expect,

but:

class testFrame(wx.Frame):

 def __init__(self):

     wx.Frame.__init__(self, parent=None, title="animate test",  

size=(200, 200))

     panel = wx.Panel(self, -1)

     btn = wx.Button(panel, -1, "Simple Widget")

     sizer = wx.GridSizer(1, 1, 5, 5)

     sizer.Add(btn, 0, wx.ALIGN_CENTER_HORIZONTAL |  

wx.ALIGN_CENTRE_VERTICAL )

     panel.SetSizer(sizer)

does it.

Adapted from here:

http://wxpython-users.1045709.n5.nabble.com/Centering-a-StaticText-on-a-panel-tp2367054p2367055.html

Michael

Hello Nathan,

your sizer2 isn’t used. Verify it with WidgetInspectionTool (WIT). If you SetSizer(sizer2) it will be used, but because of the Expand() in sizer2, the button won’t be centered vertically.

test5.py (662 Bytes)

···

Am Mittwoch, 16. Juli 2014 21:05:45 UTC+2 schrieb Nathan McCorkle:

All I want is one widget in my Frame. I tried a few variations with just a single sizer, and also two sizers… neither is centering the widget.

import wx

class testFrame(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, parent=None, title="animate test", size=(200, 200))
    panel = wx.Panel(self, -1)
    sizer1 = wx.BoxSizer(wx.VERTICAL)
    sizer2 = wx.BoxSizer(wx.HORIZONTAL)
    btn = wx.Button(panel, -1, "Simple Widget")
    sizer1.AddF(btn, wx.SizerFlags(0).Center())
    sizer2.AddF(sizer1,wx.SizerFlags(1).Expand().Center())
    panel.SetSizer(sizer1)
    panel.Layout()

if name == “main”:

app = wx.App(False)
frame = testFrame()
frame.Show()
app.MainLoop()