[wxPython-users] plese suggest good tutorial for sizers.
Christopher Barker
Chris.Barker at noaa.gov
Thu Jan 4 13:35:22 PST 2007
oops,
I forgot to enclose it in the last email. Here it is.
Christopher Barker wrote:
> By the way, As it is often the case that you want to use a label and a =
> text box as essentially one control, I wrote a little module that helps =
> with that. I've enclosed it.
-- =
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
#!/usr/bin/env python2.4
"""
A module to provide a labeled window: one with a regular old wx.Window
and a wx.StaticText next to each other.
What you get is a Sizer with the widgets layed out for you.
"""
import wx
def LabeledWindowSizer(window, label, pos =3D wx.TOP, space=3D2, stretch=3D=
0, SizerFlags=3D0):
"""
LabeledWindow(window,
label,
pos =3D wx.TOP,
space=3D2,
stretch=3D0,
SizerFlags=3D0):
Creates a Sizer that contains the wx.Window passed in, with a
wx.StaticText as a label to one side of the Window. The resulting
Sizer can then be Added, as a unit, to another sizer.
window: Any wx.Window -- created in the usual way.
label: A string with the text you want in the label.
pos: position: one of: wx.TOP, wx.LEFT, wx.RIGHT, wx.BOTTOM
space: number of pixels of space between the label and the Window
stretch: the Sizer "option" parameter, indicating how much you want
the Window to stretch
SizerFlags: any Sizer Flags for the window you want passed into the siz=
er.
=
"""
## use the window's parent for the StaticText
parent =3D window.GetParent()
Text =3D wx.StaticText(parent, label=3Dlabel)
if pos =3D=3D wx.TOP:
Sizer =3D wx.BoxSizer(wx.VERTICAL)
Sizer.Add(Text, 0, wx.ALIGN_LEFT)
Sizer.Add((1,space),0)
Sizer.Add(window, stretch, SizerFlags)
elif pos =3D=3D wx.LEFT:
Sizer =3D wx.BoxSizer(wx.HORIZONTAL)
Sizer.Add(Text, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
Sizer.Add((space,1), 0)
Sizer.Add(window, stretch, wx.ALIGN_CENTER_VERTICAL|SizerFlags)
elif pos =3D=3D wx.RIGHT:
Sizer =3D wx.BoxSizer(wx.HORIZONTAL)
Sizer.Add(window, stretch, wx.ALIGN_CENTER_VERTICAL|SizerFlags)
Sizer.Add((space,1), 0)
Sizer.Add(Text, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
elif pos =3D=3D wx.BOTTOM:
Sizer =3D wx.BoxSizer(wx.VERTICAL)
Sizer.Add(window, stretch, SizerFlags)
Sizer.Add((1,space),0)
Sizer.Add(Text, 0, wx.ALIGN_LEFT)
else:
raise ValueError("%s is not a supported Position"%pos)
return Sizer
if __name__ =3D=3D "__main__":
Choices =3D "these are some choices A_really_Long_One".split()
class TestPanel(wx.Panel):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
Sizer =3D wx.FlexGridSizer(cols=3D2, vgap=3D20, hgap=3D20)
# basic:
win =3D wx.Choice(self, choices=3DChoices)
win =3D LabeledWindowSizer(win, label=3D"A top label:")
Sizer.Add(win, 0, wx.ALIGN_CENTER_VERTICAL)
win =3D wx.Choice(self, choices=3DChoices)
win =3D LabeledWindowSizer(win, label=3D"A left label:", pos=3D=
wx.LEFT)
Sizer.Add(win, 0, wx.ALIGN_CENTER_VERTICAL)
# one with a small Window and large label
win =3D wx.Choice(self, choices=3DChoices[:-1])
win =3D LabeledWindowSizer(win, label=3D"A long top label:")
Sizer.Add(win, 0, wx.ALIGN_CENTER_VERTICAL)
# A Textcontrol:
win =3D wx.TextCtrl(self, value=3D"Some sample text")
win =3D LabeledWindowSizer(win,
label=3D"A left Label:",
pos=3Dwx.LEFT,
stretch=3D1,
)
Sizer.Add(win, 1, wx.ALIGN_CENTER_VERTICAL|wx.GROW)
# one with a small Window and small label
win =3D wx.Choice(self, choices=3DChoices[:-1])
win =3D LabeledWindowSizer(win, label=3D"A label:")
Sizer.Add(win, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT)
win =3D wx.TextCtrl(self, value=3D"Some sample text", style=3Dw=
x.TE_MULTILINE)
win =3D LabeledWindowSizer(win,
label=3D"A left Label:",
pos=3Dwx.LEFT,
stretch=3D1,
)
Sizer.Add(win, 1, wx.ALIGN_CENTER_VERTICAL|wx.GROW)
win =3D wx.Choice(self, choices=3DChoices)
win =3D LabeledWindowSizer(win,
label=3D"A bottom label:",
pos=3Dwx.BOTTOM)
Sizer.Add(win, 0, wx.ALIGN_CENTER_VERTICAL)
win =3D wx.Choice(self, choices=3DChoices)
win =3D LabeledWindowSizer(win, label=3D":A right label", pos=
=3Dwx.RIGHT)
Sizer.Add(win, 0, wx.ALIGN_CENTER_VERTICAL)
=
self.SetSizerAndFit(Sizer)
=
App =3D wx.App()
f =3D wx.Frame(None)
p =3D TestPanel(f)
f.Fit()
f.Show()
App.MainLoop()
More information about the wxpython-users
mailing list