I'm liking wxPython, just need a little help. This is a newbie question so
thank you for your patience. FYI I'm using version 2.3.
I want to layout controls on a page in rows - unequally sized. Each row
contains one or more widgets horizontally, but the entries do not form
columns. It's not a grid, just a collection of distinct input areas
(StaticBox) laid out in a row. There are multiple unrelated rows.
I want some of the input areas to take their sizes based on the size of the
containing panel. Others take their size based on the widgets that they
contain. I'm doing this by creating BoxSizers in a hierarchy. The rows are
horizontal BoxSizers containing one or more vertical BoxSizers. The rows are
brought together by a single vertical BoxSizer for the panel.
I have this working through trial and error and can get the whole thing to
expand by forcing one of the contained widgets to an arbitrary size, but I
want the whole thing to take on its size based on the size of the containing
panel. I can get this to work, but I'm having trouble ...
From the documentation on BoxSizer:
"The stretch factor ... is used for the main orientation, i.e. when using a
horizontal box sizer, the stretch factor determines how much the child can be
stretched horizontally."
... I'm having trouble getting the control placed in a horizontal BoxSizer to
limit its expansion to horizontal. The only way that I can get it to expand
at all is if when I add its row to the vertical BoxSizer (the one that brings
the rows together) I tell it that it can expand. That seems to tell it to
expand vertically as well. I don't want this.
First question: How can I get my row entry to expand horizontally only and
fill out to the size of the panel containing it?
Second question: In the following sample code, why does setting an explicit
size in the Frame produce something close to the desired result, but setting
the size on the Panel does not - seems to have no effect.
Please note: This is just sample that I put together to ask this question.
My real-world example is much more complex.
import wx
class TopFrame(wx.Frame):
def __init__(self, parent, id, title):
···
#
# Two cases:
# - use the explict Frame size and the default size in the Panel (OK)
# - use the default Frame size and the explicit size in the Panel (NOT OK)
# wx.Frame.__init__(self, parent, id, title, (-1,-1), (-1, -1), \
wx.Frame.__init__(self, parent, id, title, (-1,-1), (300, 150), \
wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER |
wx.RESIZE_BOX | wx.MAXIMIZE_BOX))
mypanel = wx.Panel(self, -1, size = (-1,-1))
# mypanel = wx.Panel(self, -1, size = (300,150))
mylabel1 = wx.StaticText(mypanel, label = "Label1", style =
wx.ALIGN_RIGHT)
mytext1 = wx.TextCtrl(mypanel, size = (-1,-1))
mylabel2 = wx.StaticText(mypanel, label = "Label2", style =
wx.ALIGN_RIGHT)
mytext2 = wx.TextCtrl(mypanel, size = (-1,-1))
mysizer1 = wx.BoxSizer(wx.HORIZONTAL)
mysizer1.Add(mylabel1, 0, wx.CENTER)
mysizer1.Add((5,5))
# If I don't expand here, I don't get expansion
# mysizer1.Add(mytext1, 0, wx.CENTER)
mysizer1.Add(mytext1, 1, wx.CENTER|wx.EXPAND)
mysizer2 = wx.BoxSizer(wx.HORIZONTAL)
mysizer2.Add(mylabel2, 0, wx.CENTER)
mysizer2.Add((5,5))
mysizer2.Add(mytext2, 0, wx.CENTER)
mysizer3 = wx.BoxSizer(wx.VERTICAL)
mysizer3.AddSpacer((2,2), 0)
# If I don't expand here, I don't get expansion in either dimension
mysizer3.Add(mysizer1, 1, wx.EXPAND)
# mysizer3.Add(mysizer1, 0)
mysizer3.Add(mysizer2, 1)
mysizer3.AddSpacer((1,1), 2, wx.EXPAND)
mypanel.SetSizerAndFit(mysizer3)
class SPSApp(wx.App):
def OnInit(self):
self.topframe = TopFrame(None, -1, "TEST")
self.SetTopWindow(self.topframe)
self.topframe.Show(True)
return True
app = SPSApp(redirect=True, filename = "testerror.log")
app.MainLoop()
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005