开发者

Best way to remove controls from a wx panel

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开发者_如何学Go 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?


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 = []
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜