Search code examples
wxpython

Disabling/enabling elements of sizer in wxpython


In wxpython you cannot disable/enable sizer and its items, so I decided to loop through sizer and disable them one by one.

My panel looks like this:

vertical_sizer
├── horizontal_sizer_1
│   └── dynamic_created_elements
└── horizontal_sizer_2
    └── dynamic_created_elements

Because my items are created dynamically, i don't know how many or what kind elements will be inside, so can't do it by their id. So I have wrtitten this code:

for horizontal_sizer in self.my_sizer.GetChildren():
    for element in horizontal_sizer.GetChildren():
        element.Enable()

But i keep on getting error AttributeError: 'SizerItem' object has no attribute 'GetChildren' on my horizontal_sizer.


Solution

  • You should try calling GetWindow() on your horizontal_sizer item and print it out to determine if it is None or a widget you weren't expecting. My suspicion is that you have another widget inside of your vertical sizer that is not a sizer.

    Alternatively you can use Python's hasattr to determine if the child item has GetChildren() as a method and if not, just skip it:

    for horizontal_sizer in self.my_sizer.GetChildren():
        if hasattr(horizontal_sizer, 'GetChildren'):
            for element in horizontal_sizer.GetChildren():
                element.Enable()