I have a situation where I have to create a panel with content in the panel, and then after creating the panel, I need to resize it. The size of the panel depends on the child content, and when you create the child content, you need to specify the parent panel; like a chicken and the egg problem. So, I have a need to create the panel and its content, then based on the content, resize the panel...
So, I made a mock up code example, where the panel starts out at 50 x 50, and after constructing it at that size, I want it to instead be 400 x 400. But, when you run this on your computer, you just see it as 50 x 50. The SetSize((400, 400)) has no effect!
How can I fix this and make SetSize take effect after applying the sizer? Any help is appreiciated...
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, id = -1, title = 'Hello, World!', size = (500, 500))
self.__myPanel = wx.Panel(self, size = (50, 50), style = wx.SIMPLE_BORDER)
def test(self):
self.__myPanel.SetSize((400, 400))
vertBoxSizer = wx.BoxSizer(wx.VERTICAL)
vertBoxSizer.AddSpacer((0, 10))
vertBoxSizer.Add(self.__myPanel, 0, wx.ALL, 0)
self.SetSizer(vertBoxSizer)
self.Layout()
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
frame.test()
frame.Show(True)
app.MainLoop()
print 'Exiting...'
There's no chicken and egg because windows and sizers are not parent/children related. Sizers are a very good tool (they are not windows!) used to size windows.
If your panel is the only child of a frame then no sizer is needed: The panel will be sized to fit the client area of its parent frame.
If you want that the window who set the sizer changes its size when the sizer needs it (perhaps due to yo did use SetSize() in a panel whose size is managed by that sizer) just call Fit(),
self.Fit()in your test() function.I recomend reading official docs for window sizing