Creating a StaticText without having text cut off

Hello,

I am having a weird difficulty with StaticText and BoxSizer.

I'm creating a dialog with a vertical sizer. The first, topmost item
in the sizer is a StaticText with a paragraph of text, and below that
there's a bunch of other widgets, doesn't matter which. (At the bottom
there are the buttons, of course.)

Now, since the text is a full paragraph and not just one line, it
needs to wrap to several lines. And the sizer needs to take into
account the height that the StaticText will require because of this
wrapping, so no text will be hidden.

How do I do this? I tried and haven't been successful.

Thanks,
Ram.

Try this:

1. Set the width to whatever you want but DON'T set the height on your
StaticTextCtrl. You can do that by passing, for example,
size=wx.Size(220,-1).

2. Use the .Wrap(int) function on your staticText to wrap it to what
you want, like

myStaticText.Wrap(210)
#will wrap at 210 pixels, though can be thrown off by really long words.

3. Call .Layout() on the parent of the staticText to make sure
everything is resized in the sizer correctly.

This seems to be working for me.

Che

···

On Wed, Nov 10, 2010 at 11:24 AM, cool-RR <ram.rachum@gmail.com> wrote:

Hello,

I am having a weird difficulty with StaticText and BoxSizer.

I'm creating a dialog with a vertical sizer. The first, topmost item
in the sizer is a StaticText with a paragraph of text, and below that
there's a bunch of other widgets, doesn't matter which. (At the bottom
there are the buttons, of course.)

Now, since the text is a full paragraph and not just one line, it
needs to wrap to several lines. And the sizer needs to take into
account the height that the StaticText will require because of this
wrapping, so no text will be hidden.

How do I do this? I tried and haven't been successful.

Hello,

I am having a weird difficulty with StaticText and BoxSizer.

I’m creating a dialog with a vertical sizer. The first, topmost item

in the sizer is a StaticText with a paragraph of text, and below that

there’s a bunch of other widgets, doesn’t matter which. (At the bottom

there are the buttons, of course.)

Now, since the text is a full paragraph and not just one line, it

needs to wrap to several lines. And the sizer needs to take into

account the height that the StaticText will require because of this

wrapping, so no text will be hidden.

How do I do this? I tried and haven’t been successful.

Try this:

  1. Set the width to whatever you want but DON’T set the height on your

StaticTextCtrl. You can do that by passing, for example,

size=wx.Size(220,-1).

But how can I know the width number? I want it to fit inside the parent dialog, but that might have an unknown size.

···

On Wed, Nov 10, 2010 at 6:45 PM, C M cmpython@gmail.com wrote:

On Wed, Nov 10, 2010 at 11:24 AM, cool-RR ram.rachum@gmail.com wrote:

  1. Use the .Wrap(int) function on your staticText to wrap it to what

you want, like

myStaticText.Wrap(210)

#will wrap at 210 pixels, though can be thrown off by really long words.

  1. Call .Layout() on the parent of the staticText to make sure

everything is resized in the sizer correctly.

This seems to be working for me.

Che

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en


Sincerely,
Ram Rachum

But how can I know the width number? I want it to fit inside the parent
dialog, but that might have an unknown size.

You may have to first show the dialog and .Layout() the .StaticText.
Resetting the StaticText's size will happen so fast you won't be able
to detect the resizing.

dlgSizeX, dlgSizeY = dlg.ClientSize

margin = 20
myStaticText.Wrap( dlgSizeX - margin )

Ray

This seems to be working, I’ll see how stable it is. (Because hard-coding the margin seems like bad practice.)

In the same dialog I have another piece of static text which is in a horizontal sizer next to another widget. And I want to give the same treatment to this static text as well, but I can’t know the width in advance!

What can I do?

Ram.

···

On Wed, Nov 10, 2010 at 7:23 PM, WinCrazy pascor@verizon.net wrote:

But how can I know the width number? I want it to fit inside the parent

dialog, but that might have an unknown size.

You may have to first show the dialog and .Layout() the .StaticText.

Resetting the StaticText’s size will happen so fast you won’t be able

to detect the resizing.

dlgSizeX, dlgSizeY = dlg.ClientSize

margin = 20

myStaticText.Wrap( dlgSizeX - margin )

Ray

Hello,

I am having a weird difficulty with StaticText and BoxSizer.

I'm creating a dialog with a vertical sizer. The first, topmost item
in the sizer is a StaticText with a paragraph of text, and below
that there's a bunch of other widgets, doesn't matter which. (At the
bottom there are the buttons, of course.)

Now, since the text is a full paragraph and not just one line, it
needs to wrap to several lines. And the sizer needs to take into
account the height that the StaticText will require because of this
wrapping, so no text will be hidden.

How do I do this? I tried and haven't been successful.

Is the text already wrapped (with \n's inserted in the text) or do you
do something like calling staticText.Wrap(width)? If so then the best
size calculation should include enough vertical space to show all the
lines. If you are relying on the static text to automatically wrap to
the available width, don't. First, not all platforms do that. Second,
the best size calculation has no idea how it will be auto-wrapped and so
it can't provide an accurate size for the sizer.

In a later message, cool-RR wrote:

But how can I know the width number? I want it to fit inside the
parent dialog, but that might have an unknown size.

And there's the paradox... Something I've done in the past is to catch
the parent's EVT_SIZE, Freeze() the parent, reset the label on the
static text to the unwrapped version of the text, call Wrap again with
the new width, call Thaw(), and call evt.Skip() so the default handler
will still do the layout as normal.

···

On 11/10/10 8:24 AM, cool-RR wrote:

--
Robin Dunn
Software Craftsman

The same trick I mentioned in the other mail will work here too. Just adjust the width you pass to Wrap by the width of the other widget.

···

On 11/10/10 9:43 AM, cool-RR wrote:

In the same dialog I have another piece of static text which is in a
horizontal sizer next to another widget. And I want to give the same
treatment to this static text as well, but I can't know the width in
advance!

What can I do?

--
Robin Dunn
Software Craftsman

Or rather, the best size width of the other widget.

···

On 11/10/10 10:01 AM, Robin Dunn wrote:

On 11/10/10 9:43 AM, cool-RR wrote:

In the same dialog I have another piece of static text which is in a
horizontal sizer next to another widget. And I want to give the same
treatment to this static text as well, but I can't know the width in
advance!

What can I do?

The same trick I mentioned in the other mail will work here too. Just
adjust the width you pass to Wrap by the width of the other widget.

--
Robin Dunn
Software Craftsman

This is getting too abstract, I don’t understand exactly what I should be doing.

With the original, topmost text I do this:

self.general_text.SetSize((self.ClientSize[0] - 20, -1))

self.general_text.Wrap(self.ClientSize[0] - 20)

How will this look for the static text which is in a horizontal sizer? (Its name is self.cruncher_text.)

Ram.

···

On Wed, Nov 10, 2010 at 8:05 PM, Robin Dunn robin@alldunn.com wrote:

On 11/10/10 10:01 AM, Robin Dunn wrote:

On 11/10/10 9:43 AM, cool-RR wrote:

In the same dialog I have another piece of static text which is in a

horizontal sizer next to another widget. And I want to give the same

treatment to this static text as well, but I can’t know the width in

advance!

What can I do?

The same trick I mentioned in the other mail will work here too. Just

adjust the width you pass to Wrap by the width of the other widget.

Or rather, the best size width of the other widget.

Robin Dunn

This is getting too abstract, I don't understand exactly what I should
be doing.

With the original, topmost text I do this:

     self.general_text.SetSize((self.ClientSize[0] - 20, -1))
     self.general_text.Wrap(self.ClientSize[0] - 20)

SetSize is not needed, let the sizer do it based on the best size calculation. After doing the Wrap then the best size calculation should give the correct height because of the \n's that were inserted into the label.

How will this look for the static text which is in a horizontal sizer?
(Its name is `self.cruncher_text`.)

     self.cruncher_text.Wrap(
         self.ClientSize.width - self.otherWidget.BestSize.width - 20)

···

On 11/10/10 10:10 AM, cool-RR wrote:

--
Robin Dunn
Software Craftsman

A couple of things about sizers:

-) Widgets are not "in" sizers; Their size and position are controlled
by sizers. This is why sizers don't have parent container widgets.

-) Once the widget's sizer has sized and positioned its widgets, the
widgets can be queried about their size and position as set by their
sizer. But, don't try to override the sizer's control.

If you don't want the size and/or position that the widget's sizer
assigns to it, then don't put the widget in a sizer. Set the size and
position yourself. However, there are several sizer methods you can
call to help make the widgets in a sizer be controlled the way you
like. See:

    BoxSizerFromTheGroundUp
    BoxSizerFromTheGroundUp - wxPyWiki

A stripped-down -working- sample app from you with just the layout
code would *really* help to figure out what's going on. Trying to
guess is like shooting in the dark. Use Google email to attach your
sample app.

Ray

···

On 11/10/10 9:43 AM, cool-RR wrote:

This is getting too abstract, I don't understand exactly what I should be
doing.

Ram.