[wxPython-users] Removing components from a gridbagsizer

Julian Swartz swartzjp at gmail.com
Thu Jun 1 00:49:10 PDT 2006


That doesn't work. Consider the test program below. I have a GridBagSizer
managing a 2x2 grid containing 2 buttons and 2 StaticText labels. I want to
be able to make button 2 disappear by clicking button 1, but when I use the
Detach method, nothing happens. If I add the line self.sizer.Layout() to the
end of the button_clicked() method, button 2 is still visible and the items
merely get realigned in the grid.

Any ideas?

Julian


# gb.py
# clicking button1 should make button 2 disappear

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        '''Creates a frame containing a 2x2 grid with
        2 buttons and 2 statictext labels.'''

        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition)
        button1 =3D wx.Button(self,-1, "Button 1")
        button2 =3D wx.Button(self,-1, "Button 2")
        self.sizer =3D wx.GridBagSizer(2, 2)
        self.sizer.Add(wx.StaticText(self,-1, "Label 1"), (0, 0), flag=3D
wx.ALIGN_CENTER)
        self.sizer.Add(button1, (0, 1), flag=3Dwx.EXPAND)
        self.sizer.Add(button2, (1, 0), flag=3Dwx.EXPAND)
        self.sizer.Add(wx.StaticText(self,-1, "Label 2"), (1, 1), flag=3D
wx.ALIGN_CENTER)

        self.Bind(wx.EVT_BUTTON, self.button_click, button1)
        self.SetSizerAndFit(self.sizer)
        self.Centre()


    def button_click(self, event):
        itm =3D self.sizer.FindItemAtPosition((1, 0))
        if (itm !=3D None) and itm.IsWindow():
            self.sizer.Detach(itm.GetWindow())


class MyApp(wx.App):
    def OnInit(self):
        frame =3D MyFrame(None, -1, "wxgridbagsizer.py")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app =3D MyApp(0)
app.MainLoop()


On 31/05/06, Peter Decker <pydecker at gmail.com> wrote:
>
> On 5/31/06, Julian Swartz <swartzjp at gmail.com> wrote:
> > How can I remove the contents of a cell of a gridbagsizer given only the
> row
> > and column index of the cell?
>
> itm =3D gbsizer.FindItemAtPosition((row, col))
> if itm.IsWindow():
>   gbizer.Detach(itm.GetWindow())
>
>
> --
>
> # p.d.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help at lists.wxwidgets.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200606=
01/d0ac6e99/attachment.htm


More information about the wxpython-users mailing list