[wxPython-users] Wrapping StaticText - again
Robin Dunn
robin at alldunn.com
Fri Mar 14 16:31:40 PDT 2008
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 Stat=
icText
> 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 wr=
aps
> 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!
-------------- next part --------------
import wx
print wx.version()
class AutoWrapStaticText(wx.PyControl):
def __init__(self, parent, id=3D-1, label=3D"",
pos=3Dwx.DefaultPosition, size=3Dwx.DefaultSize,
style=3D0, name=3D"wrapStatText"):
wx.PyControl.__init__(self, parent, id, pos, size, wx.NO_BORDER,
wx.DefaultValidator, name)
self.st =3D wx.StaticText(self, -1, label, style=3Dstyle)
self._label =3D label # save the unwrapped text
self._Rewrap()
self.Bind(wx.EVT_SIZE, self.OnSize)
=
def SetLabel(self, label):
self._label =3D 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 =3D """\
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 w=
hen they began to grow ripe it was found that every night one of them was g=
one. The king became very angry at this, and ordered the gardener to keep w=
atch all night under the tree. The gardener set his eldest son to watch; bu=
t about twelve o'clock he fell asleep, and in the morning another of the ap=
ples 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 th=
ird 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 twe=
lve he heard a rustling noise in the air, and a bird came flying that was o=
f 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 bi=
rd no harm; only it dropped a golden feather from its tail, and then flew a=
way. 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 a=
ll 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 =3D wx.Panel(self)
box =3D wx.StaticBox(p, -1, "AutoWrapStaticText")
sbs =3D wx.StaticBoxSizer(box, wx.VERTICAL)
awst =3D AutoWrapStaticText(p, label=3Dtext)
sbs.Add(awst, 1, wx.EXPAND)
sizer =3D wx.BoxSizer()
sizer.Add(sbs, 1, wx.EXPAND|wx.ALL, 25)
p.SetSizer(sizer)
=
app =3D wx.App(redirect=3DFalse)
frm =3D TestFrame(None, title=3D"test wrapping static text")
frm.Show()
#import wx.lib.inspection
#wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
More information about the wxpython-users
mailing list