Hi All . I am trying to make a Frame to which we add dynamically Add panels.
Also I want to remove panel dynamically . The dynamic Addition is working perfect.
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, 'Button 1')
        b2 = wx.Button(panel, -1, 'Button 2')
        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_2)

        self.Centre()

        self.Show(True)

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

Now similarly, i want to remove panels one by one .
I tried this function.

def remove(self,event):
    
    
        self.vbox.Remove(self.panel3)
        self.SetSizer(self.vbox)
        self.Layout()
        return

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

Simply using rhe python del command does not work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.