Search code examples
pythonpython-3.xwxpythonwxwidgets

Add space between widgets WxPython


This is my code:

class TabTwo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.resetButton = wx.Button(self, label="Reset")
        self.contactButton = wx.Button(self, label="Contact")
        self.copyrightButton = wx.Button(self, label="Copyright")

        v_sizer = wx.BoxSizer(wx.VERTICAL)
        h_sizer = wx.BoxSizer(wx.HORIZONTAL)

        v_sizer.Add(self.resetButton, 0, wx.EXPAND, 30)
        v_sizer.Add(self.contactButton, 0, wx.EXPAND, 30)
        v_sizer.Add(self.copyrightButton, 0, wx.EXPAND, 30)

        self.SetSizer(v_sizer)

How do I add some space between the buttons (Reset, Contact and Copyright) so that it doesn't look to much like it's been pressed together.
Current layout of the buttons


Solution

  • Specify a border when adding to the sizer:

        v_sizer.Add( self.resetButton, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5 )
        v_sizer.Add( self.contactButton, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5 )
        v_sizer.Add( self.copyrightButton, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5 )
    

    Here wx.TOP|wx.BOTTOM specifies to add a border to the top and bottom of the widget. I'm assuming you just want top and bottom; wx.ALL adds on all sides. Also wx.RIGHT and wx.LEFT are available.

    The parameter following wx.EXPAND|wx.TOP|wx.BOTTOM (ie 5) is the size of the border.

    See here for more information: https://wxpython.org/Phoenix/docs/html/wx.Sizer.html#wx-sizer. In particular the Add function and the flag and border parameters.