How to do spacing with wx Python BoxSizer

1.1k Views Asked by At

I made a small and simple program using wx.BoxSizer. Here is the source code:'

import wx

# MAIN PROGRAM...
class MyFrame(wx.Frame):

     def __init__(self):

     wx.Frame.__init__(self, None, -1, "My Frame", size = (600, 600))

     mainPanel = wx.Panel(self)
     mainBox = wx.BoxSizer(wx.VERTICAL)

     header1 = wx.StaticText(mainPanel, label = 'Header1:')
     header2 = wx.StaticText(mainPanel, label = 'Header2:')

     panel1 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)
     panel2 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)

     box1 = wx.BoxSizer(wx.HORIZONTAL)
     box1.AddSpacer(50)
     box1.Add(header1, 0, wx.ALL, 5)
     box1.AddSpacer(50)
     box1.Add(header2, 0, wx.ALL, 5)

     box2 = wx.BoxSizer(wx.HORIZONTAL)
     box2.Add(panel1, 0, wx.ALL, 5)
     box2.Add(panel2, 0, wx.ALL, 5)

     mainBox.Add(box1, 0, wx.ALL, 5)
     mainBox.Add(box2, 0, wx.ALL, 5)

     mainPanel.SetSizer(mainBox)
     #self.Center()           


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    frame.Show(True)
    app.MainLoop()

    print 'Exiting...'

The issue is that when, I add horizontal space to the left of each header, it also adds vertical space between the headers and sunken_border header1 and header2. Is there anyway to just add the horizontal space before the headers without adding the vertical space as a side effect? Thanks.

---EDIT---

To answer your comment: here is a picture of the program: enter image description here Simple BoxSizer program...

The 'space' in green is wanted space, but the 'space' in red is an unneeded side effect. I basically only want the green space, but I don't want the red space, I want the headers to be flush with the two panels (like right directly on top...). Right now, I am having to do absolute positioning to get it to work, I just wanted to know if you can make it work with BoxSizer or some other layout manager... Thanks again.

2

There are 2 best solutions below

1
On

When you write

Add( ..., 0, wx.ALL, 5)

you are adding 5 pixels ALL AROUND.

So:

box1.Add(header1, 0, wx.ALL, 5)

adds 5 pizels below header 1

box2.Add(panel1, 0, wx.ALL, 5)

adds 5 pixels above panel 1

mainBox.Add(box1, 0, wx.ALL, 5)

adds 5 pixels below header 1 ( contained in box1 )

 mainBox.Add(box2, 0, wx.ALL, 5)

adds 5 pixels above panel1 ( contained in box2 )

for a total of 20 extra pixels.

If you do not want white space in the vertical direction, do not write

Add( ..., 0, wx.ALL, 5)

Instead, something like this

 mainPanel = wx.Panel(self)
 mainBox = wx.BoxSizer(wx.VERTICAL)

 header1 = wx.StaticText(mainPanel, label = 'Header1:')
 header2 = wx.StaticText(mainPanel, label = 'Header2:')

 panel1 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)
 panel2 = wx.Panel(mainPanel, size = (200, 200), style = wx.SUNKEN_BORDER)

 box1 = wx.BoxSizer(wx.HORIZONTAL)
 box1.AddSpacer(50)
 box1.Add(header1)
 box1.AddSpacer(50)
 box1.Add(header2)

 box2 = wx.BoxSizer(wx.HORIZONTAL)
 box2.AddSpacer(5)
 box2.Add(panel1)
 box2.AddSpacer(10)
 box2.Add(panel2)

 mainBox.AddSpacer(5)
 mainBox.Add(box1)
 mainBox.Add(box2)

 mainPanel.SetSizer(mainBox)
 #self.Center()       
0
On

I found the solution!

instead of this: box1.AddSpacer(50)

do this... box1.AddSpacer((50, 0))

It works, yay! Thanks.