tags:

views:

99

answers:

1

What would you recommend in terms of removing controls from a wx.Panel ? I have a list of dicts of controls, something like:

[ 'cb': wx.CheckBox, 'label': wx.StaticText, 'input': wx.TextCtrl ]

and I am trying to remove them when something happens so I can add new ones.

The way I do it is:

    # remove previous controls
    for c in self.controls:
        c['cb'].Destroy()
        c['label'].Destroy()
        c['input'].Destroy()
        self.controls.remove(c)

but it seems that I always end up having len(self.controls) > 0 for an unknown reason

So what's the proper way of removing controls from a panel? Should I do something else on the panel that holds the controls?

+1  A: 

You are trying to remove elements from same list on which you are iterating and that is a sure way of creating bad side effects. e.g. take a simple example

l = range(10)
for a in l:
    l.remove(a)
print l

what do you think output should be? It seems you are removing all items and output should be '[]' but output is

[1, 3, 5, 7, 9]

because by removing items you alter internal index for interation, so instead you should do it on a copy e.g

l = range(10)
for a in l[:]:
    l.remove(a)
print l

but in your case as you know you are removing all items, just set list empty after looping e.g.

for c in self.controls:
    c['cb'].Destroy()
    c['label'].Destroy()
    c['input'].Destroy()

self.controls = []
Anurag Uniyal
I already figure it out and your answer is correct ... setting the list to the empty list fixed it. Thanks!
hyperboreean