954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic Addition & Removal of panel

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....

arindam31
Light Poster
48 posts since Mar 2011
Reputation Points: 2
Solved Threads: 0
 

Simply using rhe python del command does not work?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 
askandstudy
Newbie Poster
7 posts since Nov 2011
Reputation Points: 20
Solved Threads: 4
 
I just do the exercises use your code. http://my-study-code.googlecode.com/svn/trunk/python2/wx_dynamic_add_remove_panel.py

Wow, that worked perfectly wel. A big thanks............

arindam31
Light Poster
48 posts since Mar 2011
Reputation Points: 2
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: