I don’t know much about sized controls, apart from reading the documentation.
It seems to me that you should only call a control’s SetSizerProps()
method if that control is being laid out by a sized control.
In your example, sizedPanel
is a SizedPanel
object, but it is not being laid out by another sized control, so you shouldn’t call its SetSizerProps()
method. Instead, it should be added to boxSizer
.
However, if you needed to set the sizer properties of any of its children, then you should call the children’s SetSizerProps()
methods.
I modified your example along these lines and it appears to run OK using wxPython 4.2.2 gtk3 (phoenix) wxWidgets 3.2.6 + Python 3.12.3 + Linux Mint 22
from typing import cast
import wx
from wx import App
from wx import DEFAULT_FRAME_STYLE
from wx import FRAME_FLOAT_ON_PARENT
from wx import Frame
from wx import ID_ANY
from wx import Panel
from wx import StaticText
from wx.lib.agw.buttonpanel import BoxSizer
from wx.lib.sized_controls import SizedFrame
from wx.lib.sized_controls import SizedPanel
WINDOW_WIDTH: int = 400
WINDOW_HEIGHT: int = 200
class SizedPanelApp(App):
def __init__(self):
super().__init__()
self._outerFrame: SizedFrame = cast(SizedFrame, None)
def OnInit(self) -> bool:
title: str = 'Demo Sized Panel in Panel'
frameStyle: int = DEFAULT_FRAME_STYLE
self._outerFrame = Frame(parent=None, id=ID_ANY, size=(WINDOW_WIDTH, WINDOW_HEIGHT), style=frameStyle, title=title)
# This simulates a wizard page (which is a panel)
mainPanel: Panel = Panel(parent=self._outerFrame)
boxSizer: BoxSizer = BoxSizer()
mainPanel.SetSizer(boxSizer)
# This simulate a component that is a sized panel
sizedPanel: SizedPanel = SizedPanel(parent=mainPanel) # This is problematic
sizedPanel.SetSizerType('vertical')
boxSizer.Add(sizedPanel, 1, wx.EXPAND, 0)
# noinspection PyUnusedLocal
topLabel: StaticText = StaticText(sizedPanel, ID_ANY, 'Top Label')
# noinspection PyUnusedLocal
bottomLabel: StaticText = StaticText(sizedPanel, ID_ANY, 'Bottom Label')
self._outerFrame.Show(True)
return True
testApp = SizedPanelApp()
testApp.MainLoop()
Having said that, I still prefer to use wxGlade to layout sizers directly, rather than use sized controls.