Center StaticText within a sizer

Hi folks,

I'm trying to horizontally center the text of a wx.StaticText of given
size within a horizontal sizer, e.g. want to have a fixed sized
textbox with the text centered within.
The intuitive solution

class SomeFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1)
        panel = wx.Panel(self, -1, size = (400,400))
        text = wx.StaticText(panel, -1, "some text", size = (200,
100), style = wx.ALIGN_CENTER)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(text,1,flag = wx.CENTER)
        self.SetSizer(sizer)

doesn't work at all (wx version 2.8.10).

I'm obviously missing something and I'd be glad if someone could point
me into the right direction,

thanks
uku

ukulelefant wrote:

I'm trying to horizontally center the text of a wx.StaticText of given
size within a horizontal sizer, e.g. want to have a fixed sized
textbox with the text centered within.
The intuitive solution

class SomeFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1)
        panel = wx.Panel(self, -1, size = (400,400))
        text = wx.StaticText(panel, -1, "some text", size = (200,
100), style = wx.ALIGN_CENTER)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(text,1,flag = wx.CENTER)
        self.SetSizer(sizer)

doesn't work at all (wx version 2.8.10).

I'm obviously missing something and I'd be glad if someone could point
me into the right direction,

A) send *complete* examples:

http://wiki.wxpython.org/MakingSampleApps

B) the source of you problem is that you are putting the StaticText on a Panel, but setting the sizer on the Frame (self.SetSizer). I try to avoid nesting panels like that in one class:

http://wiki.wxpython.org/wxPython%20Style%20Guide

Addresses this and other issues.

try somethign like (untested):

class SomeFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None, -1)
         panel = wx.Panel(self, -1, size = (400,400))
         text = wx.StaticText(panel, -1, "some text", size = (200,
100), style = wx.ALIGN_CENTER)
         sizer = wx.BoxSizer(wx.HORIZONTAL)
         sizer.Add(text, 1, flag = wx.CENTER)
         panel.SetSizer(sizer)

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks Chris,

Fathering the panel with the sizer didn't help though.

A complete example that should work but doesn't (rather, the text
is displayed left aligned) is

···

On Jun 2, 6:29 pm, Christopher Barker <Chris.Bar...@noaa.gov> wrote:

ukulelefant wrote:
> I'm trying to horizontally center the text of a wx.StaticText of given
> size within a horizontal sizer, e.g. want to have a fixed sized
> textbox with the text centered within.
> The intuitive solution

> class SomeFrame(wx.Frame):
> def __init__(self):
> wx.Frame.__init__(self, None, -1)
> panel = wx.Panel(self, -1, size = (400,400))
> text = wx.StaticText(panel, -1, "some text", size = (200,
> 100), style = wx.ALIGN_CENTER)
> sizer = wx.BoxSizer(wx.HORIZONTAL)
> sizer.Add(text,1,flag = wx.CENTER)
> self.SetSizer(sizer)

> doesn't work at all (wx version 2.8.10).

> I'm obviously missing something and I'd be glad if someone could point
> me into the right direction,

A) send *complete* examples:

MakingSampleApps - wxPyWiki

B) the source of you problem is that you are putting the StaticText on a
Panel, but setting the sizer on the Frame (self.SetSizer). I try to
avoid nesting panels like that in one class:

wxPython Style Guide - wxPyWiki

Addresses this and other issues.

try somethign like (untested):

class SomeFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)
panel = wx.Panel(self, -1, size = (400,400))
text = wx.StaticText(panel, -1, "some text", size = (200,
100), style = wx.ALIGN_CENTER)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(text, 1, flag = wx.CENTER)
panel.SetSizer(sizer)

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Bar...@noaa.gov

##########
import wx

class SomeFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, parent = None)
         panel = wx.Panel(self, -1, size = (400,400))
         text = wx.StaticText(panel,-1, "some text", size = (200,100),
style = wx.ALIGN_CENTER)
         sizer = wx.BoxSizer(wx.HORIZONTAL)
         sizer.Add(text, 1, wx.CENTER)
         panel.SetSizer(sizer)

app = wx.App()
frame = SomeFrame()
frame.Show()
app.MainLoop()
##########

To the issue of nested widgets: Up to now, I thought it to be comon
practice to fill your frame with one main panel and further father
that with alle the widgets necessary, grouping it together with
sizers.

Thanks in advance,

uku

Hey again,

after searching through the list again, I've found the problem to be a
known bug for wx in a GTK environment (which is the case for me), see
http://groups.google.com/group/wxpython-users/browse_thread/thread/d1700040920eeef7

The solution so forth (at least the one that works for me out of the
box) is

···

####
from wx.lib.stattext import GenStaticText
####

and use that class instead of wx.StaticText

Thanks though and sorry for posting an already known problem,

uku

On Jun 2, 1:00 pm, ukulelefant <ukulelef...@googlemail.com> wrote:

Hi folks,

I'm trying to horizontally center the text of a wx.StaticText of given
size within a horizontal sizer, e.g. want to have a fixed sized
textbox with the text centered within.
The intuitive solution

class SomeFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)
panel = wx.Panel(self, -1, size = (400,400))
text = wx.StaticText(panel, -1, "some text", size = (200,
100), style = wx.ALIGN_CENTER)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(text,1,flag = wx.CENTER)
self.SetSizer(sizer)

doesn't work at all (wx version 2.8.10).

I'm obviously missing something and I'd be glad if someone could point
me into the right direction,

thanks
uku

ukulelefant wrote:

Fathering the panel with the sizer didn't help though.

darn, but it looks from another post like you found a solution.

To the issue of nested widgets: Up to now, I thought it to be comon
practice to fill your frame with one main panel and further father
that with alle the widgets necessary, grouping it together with
sizers.

yes, you generally don't want to put all your widgets directly on a Frame. What I suggest is that you subclass wx.Panel, put your widgets there, bind them there, then put that Panel on your Frame.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Why is it undesirable to have objects directly within a frame?

-Chris

···

On Thu, Jun 2, 2011 at 12:52 PM, Christopher Barker Chris.Barker@noaa.gov wrote:

ukulelefant wrote:

To the issue of nested widgets: Up to now, I thought it to be comon

practice to fill your frame with one main panel and further father

that with alle the widgets necessary, grouping it together with

sizers.

yes, you generally don’t want to put all your widgets directly on a Frame. What I suggest is that you subclass wx.Panel, put your widgets there, bind them there, then put that Panel on your Frame.

Because the Panel is where things like tab traversal is implemented, and also other things related to being a container of controls.

···

On 6/2/11 12:56 PM, Chris Weisiger wrote:

On Thu, Jun 2, 2011 at 12:52 PM, Christopher Barker > <Chris.Barker@noaa.gov <mailto:Chris.Barker@noaa.gov>> wrote:

    ukulelefant wrote:

        To the issue of nested widgets: Up to now, I thought it to be comon
        practice to fill your frame with one main panel and further father
        that with alle the widgets necessary, grouping it together with
        sizers.

    yes, you generally don't want to put all your widgets directly on a
    Frame. What I suggest is that you subclass wx.Panel, put your
    widgets there, bind them there, then put that Panel on your Frame.

Why is it undesirable to have objects directly within a frame?

--
Robin Dunn
Software Craftsman