tags:

views:

717

answers:

1

Hi I am newbie to wxpython I am trying to have a Frame and within that a small panel area which I am coloring blue. However no matter what I do the wx.Panel using the size attribute , the single panel snaps to the size of its parent frame. If I add another panel (pane2 in code below) both panes are drawn in the correct size.

I know I can control these panels using sizers . But I was trying to understand why the wx.Panel object behaves the way it does when its all alone.

Here is the code:

import wx

class PlateGui(wx.Frame):

    def __init__(self, *args , **kwds):
        self.frame = wx.Frame.__init__(self,*args, **kwds)
        print "Made frame"


if __name__ == "__main__":
    an_app = wx.PySimpleApp()
    aframe = PlateGui(parent=None,id=-1,title="Test Frame",size=(300, 300))
    pane = wx.Panel(parent=aframe,size=(100,100),style=wx.RAISED_BORDER)
    pane.SetBackgroundColour(wx.Colour(0,0,255))
 #  pane2 = wx.Panel(parent=aframe,size=(200,100),style=wx.RAISED_BORDER)
 # Commenting out the second pane makes the first pane fit 
 # entire frame regardless of size specified  
    aframe.Show()
    an_app.MainLoop()
+2  A: 

By default, wx.Frame has a sizer that expands its child to fill the frame. Create your own sizer, add the panel to it (without specifying expand flags) and set that as the frame's sizer.

import wx
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, 'Test')
sizer = wx.BoxSizer(wx.VERTICAL)
panel = wx.Panel(frame, -1, size=(100,100), style=wx.BORDER_RAISED)
sizer.Add(panel)
frame.SetSizer(sizer)
frame.Show()
app.MainLoop()
FogleBird
Awesome FogleBird- thanks a tonne - This "feature" of the wx.Frame imposing its sizer on its only child is not documented very well , or is it?. I struggled for hours and then when I finally added the second pane the wx.Frame released the first pane from its sizer. Is there some place in the documentation that I could have figured this out from?I would vote you up , but I am new to Stackoverflow and I guess newbie thank yous dont help you any. Thanks for answering this question anyway
harijay
From here: http://docs.wxwidgets.org/stable/wx_wxframe.html#wxframe"If the frame has exactly one child window, not counting the status and toolbar, this child is resized to take the entire frame client area."You should vote me up if you like my answer. You should mark my answer as correct if you believe it is. Doesn't matter how new you are. :)
FogleBird