wxPanel and Sizers question

Cliff Wells LogiplexSoftware at earthlink.net
Thu Jan 23 12:10:15 EST 2003


On Thu, 2003-01-23 at 07:54, Markus von Ehr wrote:
> OK, I discovered the error.
> I missed:
> 
>      self.SetSizer(sizer)

This is more how it would be done (untested):

class MyWindow(wxPanel):
    def __init__(self, parent, id):
        wxPanel.__init__(self, parent, id)
        self.SetAutoLayout(true)

        box = wxBoxSizer(wxVERTICAL)
	self.SetSizer(box)

        b1 = wxButton(self, -1, "one")
        b2 = wxButton(self, -1, "two")
        b3 = wxButton(self, -1, "three")
        b4 = wxButton(self, -1, "four")
        box.AddMany([
            (b1, 0, wxEXPAND),
            (b2, 0, wxEXPAND),
            (b3, 0, wxEXPAND),
            (b4, 0, wxEXPAND),
        ])

	EVT_BUTTON(self.b1, b1.GetId(), someEventHandler1)
	EVT_BUTTON(self.b2, b2.GetId(), someEventHandler2)
        ...

I also find it useful, when creating a a window with a repeated series
of controls (as you are apparently doing here), to do it with a loop
(also untested, but you should get the idea):


class MyWindow(wxPanel):
    def __init__(self, parent, id):
        wxPanel.__init__(self, parent, id)
        self.SetAutoLayout(true)

        box = wxBoxSizer(wxVERTICAL)
	self.SetSizer(box)

	buttons = {
	    'One':   self.oneHandler,
            'Two':   self.twoHandler,
            'Three': self.threeHandler,
            'Four':  self.fourHandler,
        }

	for b in buttons:
            button = wxButton(self, -1, label)
            EVT_BUTTON(self, button.GetId(), buttons[b])
            box.Add(b, 0, wxEXPAND)


This approach allows you to add a new button by just updating the
'button' dictionary and adding an appropriate event handler.  It also
avoids polluting your namespace with a bunch of references to buttons
you only need once.

Regards,


-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308






More information about the Python-list mailing list