WrapLength in wxPython as in Tkinter

9 views
Skip to first unread message

Reckoner

unread,
Dec 8, 2009, 10:33:53 AM12/8/09
to wxPython-users
Hi,

I have a long column of checkboxes in a panel with a sizer where the
associated text for each checkbox is of variable length. I would like
the checkbox text to wrap within the panel. This is possible with
Tkinter using WrapLength.

How would I accomplish the same thing using wxPython?

any help appreciated.

Robin Dunn

unread,
Dec 9, 2009, 3:25:45 PM12/9/09
to wxpytho...@googlegroups.com
As far as I know none of the native checkboxes don't support wrapping
their labels... One hack would be to use a checkbox with no label next
to a wx.StaticText, but that would probably not look/act correctly for
showing the focus or dealing with mouse clicks. Or you could make a
generic checkbox widget that uses the wx.RendererNative to draw the
various states of the checkbox in a native way, and also draws the text
itself wrapping as needed or desired.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Andrea Gavana

unread,
Dec 9, 2009, 4:54:05 PM12/9/09
to wxpytho...@googlegroups.com
2009/12/9 Robin Dunn:
Or you may hack my little CustomCheckBox on the Wiki:

http://wiki.wxpython.org/CreatingCustomControls


Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/
http://thedoomedcity.blogspot.com/

Reckoner

unread,
Dec 10, 2009, 3:51:00 PM12/10/09
to wxPython-users
thanks for your reply.

I looked at the CustomCheckBox per your direction, and it seems that
you can change the text height in the label for the checkbox, but I'm
not sure how you would get the line wrap that way, say, by inserting
the newline '\n' character into the string.

Any more hints?

Thanks in advance

Mike Driscoll

unread,
Dec 10, 2009, 4:06:56 PM12/10/09
to wxPython-users
Hi,

On Dec 10, 2:51 pm, Reckoner <recko...@gmail.com> wrote:
> thanks for your reply.
>
> I looked at the CustomCheckBox per your direction, and it seems that
> you can change the text height in the label for the checkbox, but I'm
> not sure how you would get the line wrap that way, say, by inserting
> the newline '\n' character into the string.
>
> Any more hints?
>
> Thanks in advance
>

Did you try the wordwrap module? See here:

http://www.wxpython.org/docs/api/wx.lib.wordwrap-module.html

I'm not sure if that will work in this case or not.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Andrea Gavana

unread,
Dec 10, 2009, 4:49:59 PM12/10/09
to wxpytho...@googlegroups.com
Hi,

2009/12/10 Mike Driscoll:
> Hi,
>
> On Dec 10, 2:51 pm, Reckoner <recko...@gmail.com> wrote:
>> thanks for your reply.
>>
>> I looked at the CustomCheckBox per your direction, and it seems that
>> you can change the text height in the label for the checkbox, but I'm
>> not sure how you would get the line wrap that way, say, by inserting
>> the newline '\n' character into the string.
>>
>> Any more hints?
>>
>> Thanks in advance
>>
>
> Did you try the wordwrap module? See here:
>
> http://www.wxpython.org/docs/api/wx.lib.wordwrap-module.html
>
> I'm not sure if that will work in this case or not.

It will, although the textwrap module is best suited for this task.
Basically, what you should do is set up a wx.EVT_SIZE handler like
this (highly untested):

self.Bind(wx.EVT_SIZE, self.OnSize)

def OnSize(self, event):

width, height = event.GetSize()
lines = textwrap.wrap(self.GetLabel(), width)

self.SetLabel("\n".join(lines))

event.Skip()

Then, in the DoGetBestSize you need to use GetMultiLineTextExtent
instead of GetTextExtent (and everywhere else too). And, in the
OnPaint method, you should either use dc.DrawLabel or multiple calls
to dc.DrawText (in a number equal to the number of lines).

Reckoner

unread,
Dec 11, 2009, 7:26:47 AM12/11/09
to wxPython-users
thanks for your reply. wordwrap would certainly insert the
appropriate newline characters, but the main issue is getting the
newly updated string to fit into the height for the checkbox so that
it appears wrapped.

thanks again.

On Dec 10, 1:06 pm, Mike Driscoll <kyoso...@gmail.com> wrote:
> Hi,
>
Reply all
Reply to author
Forward
0 new messages