Wrapping StaticText - again

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrapping StaticText - again

NorbertK
Hello list,
since some months I use wxPython and I like it alot. Unfortunately there are
some little problems that confuse me and one of them is the problem of
automatically wrapping static text.

I want a Panel (page in a notebook for that matter) which contains a StaticText
which can be several lines long.

The frame and the text within shall be resizable. The text shall take all
available space but not more.

A long StaticText *without a Wrap* goes way out of its surrounding frame.

A StaticText with a call to Wrap wraps nicely but ist width is fixed by
the call to wrap.

If I enlarge the frame-width the width of the StaticText remains the
same. If I reduce the width below the original Wrap Parameter the Text wraps
again but the end is invisible, it should show a scrollbar in this case.

Consider the folowing example and comment the Wrap statement in and out.

* Watch out that the content of the text1 should be on one or two lines
 to show the problem. GMANE forces me to break the lines.

Watch also for the effect of using wx.EXPAND or not in the 'sizer.Add'.

We have 4 different ways to sho this text - but none of them good from my POV.



Thanks for listening

Norbert
________
import wx

class MyFrame(wx.Frame):
    def __init__(self, *args,**kwargs):
        wx.Frame.__init__(self,None,1,"Resize this")
        pan = wx.Panel(self)
        text1 = wx.StaticText(pan, -1, """
his release is mostly about fixing a number of bugs and inconsistencies in

wxWidgets and wxPython.  In other words, there have been a whole lot more

changes than what is listed here, but they are not new features or API visible

changes, which is what are usually listed in this file.  Some Menu APIs
 added to
make things more consistent.  Added wx.MenuBar.SetMenuLabel,
wx.MenuBar.GetMenuLabel, wx.MenuBar.GetMenuLabelText, wx.Menu.GetLabelText,

wx.MenuItem.SetItemLabel, wx.MenuItem.GetItemLabel,
wx.MenuItem.GetItemLabelText, wx.MenuItem.GetLabelText.  The Get...Label

functions get the raw label with mnemonics and accelerators, and the

Get...LabelText functions get the text only, without mnemonics/accelerators.


Added wx.BORDER_THEME style.  Thi
        """)
        text1.Wrap(400) ### Comment in and out and resize the frame
        sizer = wx.BoxSizer(wx.HORIZONTAL) ###Try this and the next one
       # sizer.Add(text1,wx.EXPAND) ###Try this and the previous one
        sizer.Add(text1)
        pan.SetSizer(sizer)
        self.CreateStatusBar()

app = wx.App(False)
frame = MyFrame(None)
frame.Show()
app.MainLoop()


________




Reply | Threaded
Open this post in threaded view
|

Re: Wrapping StaticText - again

Robin Dunn
Norbert Klamann wrote:

> Hello list,
> since some months I use wxPython and I like it alot. Unfortunately there are
> some little problems that confuse me and one of them is the problem of
> automatically wrapping static text.
>
> I want a Panel (page in a notebook for that matter) which contains a StaticText
> which can be several lines long.
>
> The frame and the text within shall be resizable. The text shall take all
> available space but not more.
>
> A long StaticText *without a Wrap* goes way out of its surrounding frame.
>
> A StaticText with a call to Wrap wraps nicely but ist width is fixed by
> the call to wrap.
>
> If I enlarge the frame-width the width of the StaticText remains the
> same. If I reduce the width below the original Wrap Parameter the Text wraps
> again but the end is invisible, it should show a scrollbar in this case.
Perhaps the attached would be a good starting point for you.


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



import wx
print wx.version()

class AutoWrapStaticText(wx.PyControl):
    def __init__(self, parent, id=-1, label="",
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=0, name="wrapStatText"):
        wx.PyControl.__init__(self, parent, id, pos, size, wx.NO_BORDER,
                              wx.DefaultValidator, name)
        self.st = wx.StaticText(self, -1, label, style=style)
        self._label = label # save the unwrapped text
        self._Rewrap()
        self.Bind(wx.EVT_SIZE, self.OnSize)
       

    def SetLabel(self, label):
        self._label = label
        self._Rewrap()
    def GetLabel(self):
        return self._label

    def SetFont(self, font):
        self.st.SetFont(font)
        self._Rewrap()
    def GetFont(self):
        return self.st.GetFont()


    def OnSize(self, evt):
        self.st.SetSize(self.GetSize())
        self._Rewrap()

    def _Rewrap(self):
        self.st.Freeze()
        self.st.SetLabel(self._label)
        self.st.Wrap(self.GetSize().width)
        self.st.Thaw()

    def DoGetBestSize(self):
        # this should return something meaningful for what the best
        # size of the widget is, but what that size should be while we
        # still don't know the actual width is still an open
        # question...  Just return a dummy value for now.
        return wx.Size(100,100)


text = """\
A certain king had a beautiful garden, and in the garden stood a tree which bore golden apples. These apples were always counted, and about the time when they began to grow ripe it was found that every night one of them was gone. The king became very angry at this, and ordered the gardener to keep watch all night under the tree. The gardener set his eldest son to watch; but about twelve o'clock he fell asleep, and in the morning another of the apples was missing. Then the second son was ordered to watch; and at midnight he too fell asleep, and in the morning another apple was gone. Then the third son offered to keep watch; but the gardener at first would not let him, for fear some harm should come to him: however, at last he consented, and the young man laid himself under the tree to watch. As the clock struck twelve he heard a rustling noise in the air, and a bird came flying that was of pure gold; and as it was snapping at one of the apples with its beak, the gardener's son jumped up and shot an arrow at it. But the arrow did the bird no harm; only it dropped a golden feather from its tail, and then flew away. The golden feather was brought to the king in the morning, and all the council was called together. Everyone agreed that it was worth more than all the wealth of the kingdom: but the king said, 'One feather is of no use to me, I must have the whole bird.'
"""

class TestFrame(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)

        p = wx.Panel(self)
        box = wx.StaticBox(p, -1, "AutoWrapStaticText")
        sbs = wx.StaticBoxSizer(box, wx.VERTICAL)
        awst = AutoWrapStaticText(p, label=text)
        sbs.Add(awst, 1, wx.EXPAND)

        sizer = wx.BoxSizer()
        sizer.Add(sbs, 1, wx.EXPAND|wx.ALL, 25)
        p.SetSizer(sizer)
       

app = wx.App(redirect=False)
frm = TestFrame(None, title="test wrapping static text")
frm.Show()
#import wx.lib.inspection
#wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
Reply | Threaded
Open this post in threaded view
|

Re: Wrapping StaticText - again

NorbertK
Robin Dunn <robin <at> alldunn.com> writes:
>
> Perhaps the attached would be a good starting point for you.
>

[Program snipped]

Yes, it is. Thanks a Lot, Robin !

As an aside : the inspection tool rocks, I looked for something like this for
weeks. Has wxpython more of these hidden gems lurking in the sourcecode ?


Thanks again

Norbert