statictext

Greetings,
I just realized that statictext controls do not linewrap (boo, hiss)...
And I do need to wrap the text, can someone give me some suggestions on where to look for a solution (methinks I will have to write my own class or function)?

Thanks,
Scott

NOTE: the text object's width cannot be changed. I have it set for 240,
height is -1.

···

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.9.5 - Release Date: 4/7/2005

Scott Mallory wrote:

Greetings,
I just realized that statictext controls do not linewrap (boo, hiss)...
And I do need to wrap the text, can someone give me some suggestions on where to look for a solution (methinks I will have to write my own class or function)?

Wrapping might make it in to 2.6. There has been a bit of discussion about it in the last couple days.

In the meantime the way to do it is to measure the words in the text with GetTExtExtent and insert \n's to break the lines wherever you calculate the best spots would be.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks Robin.
Here is my solution for the next newbie...

text = "This is a test of a very long string that may have no end in sight, no end at all, it just keeps going on and on. Old Woman 'I don't like spam!'"
...
ftext = self._formattext(text)
label = wx.StaticText( self.panel2, -1, ftext, size=(240, -1))
...

def _formattext(self, text, max=240):
         # dc.GetTextExtent("This is a test")
         #(84, 16) width in pixels, height in pixels
         dc = wx.ScreenDC()

         ftext, j = '', ''
         w = 0
         li = text.split()
         for i in li:
             j = ' '.join([j, i])
             w, h = dc.GetTextExtent(j)
             while w <= max:
                 break
             else:
    j.strip()
                 ftext = '\n'.join([ftext, j])
                 j = ' '
  j.strip()
         ftext = '\n'.join([ftext, j])

         return ftext

Scott

···

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.9.5 - Release Date: 4/7/2005

class StaticWrapText(wx.StaticText):
    """
    StaticWrapText wxPython widget; implements StaticText
    widget with basic word wrapping algorithm to support
    intelligent resizing.
    """
    __id__ = "$Id: stext.py,v 1.1 2004/09/15 16:45:55 nyergler Exp $"
    __version__ = "$Revision: 1.1 $"
    __copyright__ = '(c) 2004, Nathan R. Yergler'
    __license__ = 'licensed under the GNU GPL2'
    
    def __init__(self, *args, **kwargs):
        kwargs['style'] = kwargs.get('style',0) | wx.ST_NO_AUTORESIZE
        wx.StaticText.__init__(self, *args, **kwargs)
        self.__label = super(StaticWrapText, self).GetLabel()
        self.Bind(wx.EVT_SIZE, self.OnSize)

    def SetLabel(self, newLabel):
        self.__label = newLabel
        self.__wrap()

    def GetLabel(self):
        return self.__label
    
    def __wrap(self):
        words = self.__label.split()
        lines =
        max_width = self.GetVirtualSizeTuple()[0]
        index = 0
        current =
        for word in words:
            current.append(word)
            if self.GetTextExtent(" ".join(current))[0] > max_width:
                del current[-1]
                lines.append(" ".join(current))
                current = [word]
        lines.append(" ".join(current))
        s = "\n".join(lines)
        super(StaticWrapText, self).SetLabel(s)
        self.Refresh()
        
    def OnSize(self, event):
        self.__wrap()

···

On 2005-04-08, Scott Mallory <sm@ebbyfish.com> wrote:

I just realized that statictext controls do not linewrap (boo, hiss)...
And I do need to wrap the text, can someone give me some suggestions on
where to look for a solution (methinks I will have to write my own class
or function)?

--
Grant Edwards grante Yow! Yow! And then we
                                  at could sit on the hoods of
                               visi.com cars at stop lights!

Hmm. I seem to recall making a couple minor tweaks to this
file, but I can't find the original anywhere. I should have
added a note that's it's been modified from the above version.

···

On 2005-04-09, Grant Edwards <grante@visi.com> wrote:

I just realized that statictext controls do not linewrap (boo, hiss)...
And I do need to wrap the text, can someone give me some suggestions on
where to look for a solution (methinks I will have to write my own class
or function)?

class StaticWrapText(wx.StaticText):
    """
    StaticWrapText wxPython widget; implements StaticText
    widget with basic word wrapping algorithm to support
    intelligent resizing.
    """
    __id__ = "$Id: stext.py,v 1.1 2004/09/15 16:45:55 nyergler Exp $"
    __version__ = "$Revision: 1.1 $"
    __copyright__ = '(c) 2004, Nathan R. Yergler'
    __license__ = 'licensed under the GNU GPL2'

--
Grant Edwards grante Yow! -- I love KATRINKA
                                  at because she drives a
                               visi.com PONTIAC. We're going
                                                   awaynow. I fed the cat.