[wxPython] Problems with wxBoxsizer

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] Problems with wxBoxsizer

Gordon Williams
Im having trouble making the boxsizer work.

I want to put a panel with two subpanels in the top left corner of the
frame.  Each sub panel contains some statictext and either a button or
choice box.
All should be packed tightly into the corner.

Here are the problems:
1)  The subpanels are stretched to the bottom of the frame, but the text
and button/choice box are positioned OK.  The subpanel border should fit
tightly around the child widgets.

2)  The main panel (coloured blue) stretches to fill the frame.  I only
want it to surround the two subpanels, therefore no blue should be showing.

3)  Specifying the size of the main panel or providing SetSizeHints has NO
effect on this panel.  I dont know why.

Needless to say, I have tried all sorts of thing to make this work and come
up blank.  Im am begining to think that it is a bug.


Any ideas would be apprieciated.

Regards,

Gordon Williams



from wxPython.wx import *


class choicePanel(wxPanel):
    '''For sample rate and number of samples'''
   
    def __init__(self, parent, sampleList, label):
        wxPanel.__init__(self, parent, -1,(-1,-1), (-1,-1),
style=wxSUNKEN_BORDER)

        ID= wxNewId()
        txt= wxStaticText(self, -1, label, (-1,-1), (-1,-1), style=
wxALIGN_CENTRE)
        txt.SetBackgroundColour(wxRED)

        choice= wxChoice(self, ID, (-1,-1), (-1,-1),  sampleList)
        choice.SetStringSelection(sampleList[0])
        EVT_CHOICE(self, ID, self.Onchoice)

        box= wxBoxSizer(wxVERTICAL)
        box.Add(txt,0, wxEXPAND)
        box.Add(choice,0, wxEXPAND)
       
        self.SetAutoLayout(TRUE)
        self.SetSizer(box)
        self.Fit()
       
    def Onchoice(self, event):
        print 'This is the event sample rate: %s\n' % event.GetString()


class buttonPanel(wxPanel):
    '''For sample rate and number of samples'''
   
    def __init__(self, parent, label):
        wxPanel.__init__(self, parent, -1,(-1,-1), (-1,-1),
style=wxSUNKEN_BORDER)

        ID= wxNewId()
        txt= wxStaticText(self, -1, label, (-1,-1), (-1,-1), style=
wxALIGN_CENTRE)
        txt.SetBackgroundColour(wxRED)
       
        choice= wxButton(self, ID, "Button")

        box= wxBoxSizer(wxVERTICAL)
        box.Add(txt,0, wxEXPAND)
        box.Add(choice,0, wxEXPAND)
       
        self.SetAutoLayout(TRUE)
        self.SetSizer(box)
        self.Fit()
       
class sampleRatePanel(wxPanel):
    ''' Main Panel '''    
    def __init__(self, parent):
        wxPanel.__init__(self, parent, -1, wxPoint(-1,-1), wxSize(-1, -1),
style=wxSUNKEN_BORDER)
        self.SetBackgroundColour(wxBLUE)
       
        sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
                      'six', 'seven', 'eight']

        sr1= buttonPanel(self, "Sample\nRate\n(Hz)")              
        sr2= choicePanel(self, sampleList, "Sample\nRate\n(Hz)")          
 

        box= wxBoxSizer(wxHORIZONTAL)
        box.Add(sr1,0, wxEXPAND)
        box.Add(sr2,0, wxEXPAND)        

        self.SetSizer(box)
        self.SetAutoLayout(TRUE)
        self.SetSizeHints(200,200,300,300)
        self.Fit()
       
class myFrame(wxFrame):
    def __init__(self, parent=NULL):
        wxFrame.__init__(self, parent, -1, "wx_plot_lines",
            wxDefaultPosition,wxSize(500,300))
       
    def OnCloseWindow(self, event):
        self.Destroy()
   
    def OnExit(self, event):
        self.Close(true)

#---------------------------------------------------------------------------

class myApp(wxApp):
    def OnInit(self):
        global pcan
        topframe=myFrame()
        topframe.Show(TRUE)
        self.SetTopWindow(topframe)
         
        srPanel= sampleRatePanel(topframe)
               
        return TRUE

#---------------------------------------------------------------------------

def test():

    app = myApp(0)
    app.MainLoop()

 
if __name__ == '__main__':
    test()
   



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Problems with wxBoxsizer

Robin Dunn
>
> Here are the problems:
> 1)  The subpanels are stretched to the bottom of the frame, but the text
> and button/choice box are positioned OK.  The subpanel border should fit
> tightly around the child widgets.
>

Actually, they are stretched to the bottom of the blue panel, which is what
you told the blue panel's box sizer to do with them.


> 2)  The main panel (coloured blue) stretches to fill the frame.  I only
> want it to surround the two subpanels, therefore no blue should be showing.

The default behavior of frames with only one child is to size that child to
fill the client area.  If you want to change this you need to catch the
frame's size event and set the child's size explicitly, or use constraints or
something.


>
> 3)  Specifying the size of the main panel or providing SetSizeHints has NO
> effect on this panel.  I dont know why.

Because the frame is resizing it.

Since you are using sizers what you needed to do is ask the sizer to fit the
window to the sizer's minimum, rather than just telling the window to fit
around the controls.

Try this:


from wxPython.wx import *


class choicePanel(wxPanel):
    '''For sample rate and number of samples'''

    def __init__(self, parent, sampleList, label):
        wxPanel.__init__(self, parent, -1,(-1,-1), (-1,-1),
style=wxSUNKEN_BORDER)

        ID= wxNewId()
        txt= wxStaticText(self, -1, label, (-1,-1), (-1,-1),
style=wxALIGN_CENTRE)
        txt.SetBackgroundColour(wxRED)

        choice= wxChoice(self, ID, (-1,-1), (-1,-1),  sampleList)
        choice.SetStringSelection(sampleList[0])
        EVT_CHOICE(self, ID, self.Onchoice)

        box= wxBoxSizer(wxVERTICAL)
        box.Add(txt,0, wxEXPAND)
        box.Add(choice,0, wxEXPAND)

        self.SetAutoLayout(TRUE)
        self.SetSizer(box)
        box.Fit(self)

    def Onchoice(self, event):
        print 'This is the event sample rate: %s\n' % event.GetString()


class buttonPanel(wxPanel):
    '''For sample rate and number of samples'''

    def __init__(self, parent, label):
        wxPanel.__init__(self, parent, -1,(-1,-1), (-1,-1),
style=wxSUNKEN_BORDER)

        ID= wxNewId()
        txt= wxStaticText(self, -1, label, (-1,-1), (-1,-1), style=
wxALIGN_CENTRE)
        txt.SetBackgroundColour(wxRED)

        choice= wxButton(self, ID, "Button")

        box= wxBoxSizer(wxVERTICAL)
        box.Add(txt,0, wxEXPAND)
        box.Add(choice,0, wxEXPAND)

        self.SetAutoLayout(TRUE)
        self.SetSizer(box)
        box.Fit(self)

class sampleRatePanel(wxPanel):
    ''' Main Panel '''
    def __init__(self, parent):
        wxPanel.__init__(self, parent, -1, wxPoint(-1,-1), wxSize(-1, -1),
style=wxSUNKEN_BORDER)
        self.SetBackgroundColour(wxBLUE)

        sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
                      'six', 'seven', 'eight']

        sr1= buttonPanel(self, "Sample\nRate\n(Hz)")
        sr2= choicePanel(self, sampleList, "Sample\nRate\n(Hz)")


        box= wxBoxSizer(wxHORIZONTAL)
        box.Add(sr1,0, wxEXPAND)
        box.Add(sr2,0, wxEXPAND)

        self.SetSizer(box)
        self.SetAutoLayout(TRUE)
        box.Fit(self)


class myFrame(wxFrame):
    def __init__(self, parent=NULL):
        wxFrame.__init__(self, parent, -1, "wx_plot_lines",
            wxDefaultPosition,wxSize(500,300))

    def OnCloseWindow(self, event):
        self.Destroy()

    def OnSize(self, event):
        pass


#---------------------------------------------------------------------------

class myApp(wxApp):
    def OnInit(self):
        global pcan
        topframe=myFrame()
        topframe.Show(TRUE)
        self.SetTopWindow(topframe)

        srPanel= sampleRatePanel(topframe)

        return TRUE

#---------------------------------------------------------------------------

def test():

    app = myApp(0)
    app.MainLoop()


if __name__ == '__main__':
    test()





_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users