the right way to remove widgets in sizers

I have a “row” of widgets that correspond to 2 rows of a flexGridSizer, and within that there is another flexGridSizer. I collect all of these widget objects, plus some non-widget objects (such as datetime objects), in a list. The user has the option to remove all that from the screen, and so I want to go through the list and clear everything from the screen and destroy all these objects and sizers. I’ll then layout the panel again without them and change the size accordingly.

I just want a canonical answer as to what the “rightest” (most wxPythonic) way to do this is. Is there a code sample that you could recommend?

Hi Che,

···

On Friday, September 6, 2013 3:55:09 PM UTC-5, Che M wrote:

I have a “row” of widgets that correspond to 2 rows of a flexGridSizer, and within that there is another flexGridSizer. I collect all of these widget objects, plus some non-widget objects (such as datetime objects), in a list. The user has the option to remove all that from the screen, and so I want to go through the list and clear everything from the screen and destroy all these objects and sizers. I’ll then layout the panel again without them and change the size accordingly.

I just want a canonical answer as to what the “rightest” (most wxPythonic) way to do this is. Is there a code sample that you could recommend?

If you want to delete the row, I would just use parent sizer’s Remove method to remove the child flexGridSizer. If you want to just hide the row, but NOT destroy it, then use Detach. If you don’t want to destroy the child sizer itself, then I think what you’re doing is correct (i.e. grab the widgets themselves and destroy them).

  • Mike

Hi Mike,

Thanks. Yeah, what I wound up doing–for better or worse–was this:

dont_remove_list = [self.flexGridSizer1,None,flexGrids_to_be_removed_list]
flexGrids_to_be_removed_list = []

for obj in list_of_objects:
if type(obj) is not datetime.datetime:
containing_sizer = obj.GetContainingSizer()
if containing_sizer not in dont_remove_list:
flexGrids_to_be_removed_list.append(containing_sizer)

    obj.Destroy()
    for sizer in flexGrids_to_be_removed_list:
    try:
        self.flexGridSizer1.Remove(sizer)
    except:
        pass

This basically chugs through the list and if it is not a datetime, it destroys the object and saves for later the sizer it was in (IF it wasn’t the overall sizer). At the end it removes the subsizers, so that they are not taking up space in the main flexGridSizer. The various conditionals and try/excepts are because I was getting problems removing things twice, or removing the overall sizer, etc.

Not elegant, but working for now.

Btw, Is there a way to say: “If obj is a widget” in Python?

Che

(there’re some indenting mistakes up there, but you get the idea)

···

On Mon, Sep 9, 2013 at 3:25 PM, C M cmpython@gmail.com wrote:

Hi Mike,

Thanks. Yeah, what I wound up doing–for better or worse–was this:

dont_remove_list = [self.flexGridSizer1,None,flexGrids_to_be_removed_list]
flexGrids_to_be_removed_list =

for obj in list_of_objects:
if type(obj) is not datetime.datetime:
containing_sizer = obj.GetContainingSizer()
if containing_sizer not in dont_remove_list:
flexGrids_to_be_removed_list.append(containing_sizer)

    obj.Destroy()
    for sizer in flexGrids_to_be_removed_list:
    try:
        self.flexGridSizer1.Remove(sizer)
    except:
        pass

This basically chugs through the list and if it is not a datetime, it destroys the object and saves for later the sizer it was in (IF it wasn’t the overall sizer). At the end it removes the subsizers, so that they are not taking up space in the main flexGridSizer. The various conditionals and try/excepts are because I was getting problems removing things twice, or removing the overall sizer, etc.

Not elegant, but working for now.

Btw, Is there a way to say: “If obj is a widget” in Python?

Che

C M wrote:

Btw, Is there a way to say: "If obj is a widget" in Python?

     if isinstance(obj, wx.Window): ...

···

--
Robin Dunn
Software Craftsman