Centering a StaticText on a panel

I’m trying to center a static text on a panel (where the panel is a wx.Frame) using:

    panel = wx.Panel(self, -1, pos=(0,0), size=(1000,100))
    text = wx.StaticText(self.topPanel, -1, "Some text", style=wx.ALIGN_CENTER)
    text.CenterOnParent()

But the text is not centered in that there is more space on the left than on the right. Is this a bug or am I missing a step? What is the “standard” way to center a widget in a panel?

Any help appreciated!

Ron

···

Get more from your digital life. Find out how.

Try with a sizer (see attached code).

Center.py (882 Bytes)

···

2008/8/2 Ron Kneusel oneelkruns@hotmail.com

I’m trying to center a static text on a panel (where the panel is a wx.Frame) using:

    panel = wx.Panel(self, -1, pos=(0,0), size=(1000,100))
    text = wx.StaticText(self.topPanel, -1, "Some text", style=wx.ALIGN_CENTER)

    text.CenterOnParent()

But the text is not centered in that there is more space on the left than on the right. Is this a bug or am I missing a step? What is the “standard” way to center a widget in a panel?

Any help appreciated!

Ron


Get more from your digital life. Find out how.


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Thanks for the code snippet. On its own, your code works wonderfully, but when placed into my application my text is still not centered and when self.Fit() is called my window decorations disappear and there is no close box.

Alternatively, is there a way to get the size of the static text in pixels? That would allow me to center it since the panel is of a fixed size.

Ron

···

Date: Sat, 2 Aug 2008 17:05:09 +0200
From: barbarossa.platz@gmail.com
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Centering a StaticText on a panel

Try with a sizer (see attached code).

2008/8/2 Ron Kneusel oneelkruns@hotmail.com

I’m trying to center a static text on a panel (where the panel is a wx.Frame) using:

    panel = wx.Panel(self, -1, pos=(0,0), size=(1000,100))
    text = wx.StaticText(self.topPanel, -1, "Some text", style=wx.ALIGN_CENTER)

    text.CenterOnParent()

But the text is not centered in that there is more space on the left than on the right. Is this a bug or am I missing a step? What is the “standard” way to center a widget in a panel?

Any help appreciated!

Ron


Get more from your digital life. Find out how.


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


Get more from your digital life. Find out how.

I replied too soon. If I call Fit() on the panel, instead of the frame, and then set the panel size back to what it was originally, the text is centered using the GridSizer. Thanks!

Ron

···

Date: Sat, 2 Aug 2008 17:05:09 +0200
From: barbarossa.platz@gmail.com
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Centering a StaticText on a panel

Try with a sizer (see attached code).

2008/8/2 Ron Kneusel oneelkruns@hotmail.com

I’m trying to center a static text on a panel (where the panel is a wx.Frame) using:

    panel = wx.Panel(self, -1, pos=(0,0), size=(1000,100))
    text = wx.StaticText(self.topPanel, -1, "Some text", style=wx.ALIGN_CENTER)

    text.CenterOnParent()

But the text is not centered in that there is more space on the left than on the right. Is this a bug or am I missing a step? What is the “standard” way to center a widget in a panel?

Any help appreciated!

Ron


Get more from your digital life. Find out how.


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


Get more from your digital life. Find out how.


Get more from your digital life. Find out how.

Ron Kneusel wrote:

Thanks for the code snippet. On its own, your code works wonderfully, but when placed into my application my text is still not centered and when self.Fit() is called my window decorations disappear and there is no close box.

Alternatively, is there a way to get the size of the static text in pixels? That would allow me to center it since the panel is of a fixed size.

I see you fixed your problem, but I just wanted to point you to wxPython Style Guide - wxPyWiki, particularly #5. You may think your text is a "fixed" size, but this is not the case for users using a different font/theme, a different platform, or users with different DPIs. Among many other things, you will utterly break a11y. Not to mention your code will probably be a nightmare to maintain later :slight_smile:

Absolute positioning is almost never what you want. If you take the time to properly learn Sizers now, you will be greatly rewarded later, I promise!

- Mike

Mike wrote:

I see you fixed your problem, but I just wanted to point you to
wxPython Style Guide - wxPyWiki, particularly #5. You
may think your text is a “fixed” size, but this is not the case for
users using a different font/theme, a different platform, or users with
different DPIs. Among many other things, you will utterly break a11y.
Not to mention your code will probably be a nightmare to maintain later :slight_smile:

Absolute positioning is almost never what you want. If you take the time
to properly learn Sizers now, you will be greatly rewarded later, I promise!

Thanks for the link. Some good suggestions in there.

Ron

···

Reveal your inner athlete and share it with friends on Windows Live. Share now!