Arindam Roychowdhury
Arindam Roychowdhury

Reputation: 6521

Dynamic Addition & Removal of panel

I am trying to make a Frame to which we add dynamically panels. Also I also want to remove panel dynamically . The dynamic Addition is working perfect.The removal part is not working at all. Please see the code below:

# panels.py
#self.Fit() causes the whole frame to shrink.So we are using self.Layout instead

import wx


class Panels(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        self.vbox = wx.BoxSizer(wx.VERTICAL)
        panel = wx.Panel(self,-1)
        hbox= wx.BoxSizer(wx.HORIZONTAL)
        b1 = wx.Button(panel, -1, 'Add')
        b2 = wx.Button(panel, -1, 'Remove')
        hbox.Add(b1,-1,wx.ALL,10)
        hbox.Add(b2,-1,wx.ALL,10)
        panel.SetSizer(hbox)

        panel2 = wx.Panel(self,-1)
        hbox2= wx.BoxSizer(wx.HORIZONTAL)
        b1_2 = wx.Button(panel2, -1, 'Button 3')
        b2_2 = wx.Button(panel2, -1, 'Button 4')
        hbox2.Add(b1_2,-1,wx.ALL,10)
        hbox2.Add(b2_2,-1,wx.ALL,10)
        panel2.SetSizer(hbox2)
        self.vbox.Add(panel,-1,wx.EXPAND,10)
        self.vbox.Add((-1, 10))
        self.vbox.Add(panel2,-1,wx.EXPAND,10)


        self.SetSizer(self.vbox)
        self.Layout()
        self.Bind(wx.EVT_BUTTON,self.tst, b1)
        self.Bind(wx.EVT_BUTTON,self.remove, b2)

        self.Centre()

        self.Show(True)

    def tst(self,event):
        self.panel3 = wx.Panel(self,-1)
        hbox3= wx.BoxSizer(wx.HORIZONTAL)
        b1_3 = wx.Button(self.panel3, -1, 'Button 5')
        b2_3 = wx.Button(self.panel3, -1, 'Button 6')
        hbox3.Add(b1_3,-1,wx.ALL,10)
        hbox3.Add(b2_3,-1,wx.ALL,10)
        self.panel3.SetSizer(hbox3)
        self.vbox.Add((-1, 10))
        self.vbox.Add(self.panel3,-1,wx.EXPAND,10)
        self.SetSizer(self.vbox)
        self.Layout()
        return

    def remove(self,event):


        self.vbox.Remove(self.panel3)
        self.SetSizer(self.vbox)
        self.Layout()
        return




app = wx.App()
Panels(None, -1, 'Panels')
app.MainLoop()

Anyone has any suggestions how this can work? I am not sure if '.Remove()' is there at all....Or if it works with panels......

Upvotes: 0

Views: 1397

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33111

Depending on what you want to do, you can use the sizer's Remove or Detach method. Remove will remove the panel and then Destroy it. Detach will basically hide the panel and then you can reuse it. You could use Hide() instead of Detach() for that. If you see a flicker when destroying and creating, you'll want to look into the Freeze/Thaw methods.

Finally, I wrote a little article showing how to switch between panels that might help you too: http://www.blog.pythonlibrary.org/2010/06/16/wxpython-how-to-switch-between-panels/

Upvotes: 1

Related Questions