Trouble putting padding between text and very edge of panel

I’ve been using wxPython for a project and have had a lot of trouble getting the text to look right. After a lot of effort, I got it 99% of the way there, but I can’t figure out how to put some whitespace padding around the top-level panel. The way it is, the text is all mashed up directly against the outer edges. I pared down my code into a minimal example that shows the problem. Could really use some help.

import wx

class Gui (wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(1920,1080))
        self.parent = parent
        self.Maximize(True)
        tabs = wx.Notebook(self, style=wx.NB_LEFT|wx.NB_FIXEDWIDTH)
        stratPanel = StrategyAnalyzerPanel(tabs)
        tabs.AddPage(stratPanel, "Strategy Analyzer")
        self.Layout()

class StrategyAnalyzerPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.SetBackgroundColour(wx.BLUE)
        
        lvl1_sizer = wx.FlexGridSizer(cols=2, rows=1, vgap=10, hgap=10)
        lvl1_sizer.AddGrowableCol(0)
        lvl1_sizer.AddGrowableCol(1)
        lvl1_sizer.AddGrowableRow(0)
        
        lvl3_stratSelector_sizer = wx.BoxSizer(wx.VERTICAL)
        stratSelector = StrategySelector(self)
        lvl3_stratSelector_sizer.Add(stratSelector, flag=wx.EXPAND|wx.ALL, proportion=1)
        
        lvl1_sizer.Add(lvl3_stratSelector_sizer, proportion=2, flag=wx.EXPAND|wx.ALL)
        
        self.SetSizer(lvl1_sizer)
        self.Layout()

class StrategySelector(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.SetBackgroundColour(wx.RED)
        
        self.level1Sizer = wx.GridSizer(cols=2, hgap=7, vgap=7)
        self.level2_buyStrat_sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=7, vgap=4)
        self.level2_buyStrat_sizer.AddGrowableCol(0)
        
        buyStrat_label = wx.StaticText(self, label="Sample text is up against the very left edge")
        self.level2_buyStrat_sizer.Add(buyStrat_label)
        
        self.level1Sizer.Add(self.level2_buyStrat_sizer, proportion=1, flag=wx.EXPAND|wx.ALL)

if __name__ == '__main__':
    wxApp = wx.App()
    sweet_ui = Gui(None, "Border Test")
    sweet_ui.Show()
    wxApp.MainLoop()
    
  


In your calls to the various sizers’ Add() method you are passing wx.ALL in the flag parameter, but you are not passing a value in the border parameter, so it defaults to zero - i.e. no padding.

Try adding border parameters set to the required size in pixels.

1 Like

Ohhh! I thought the border option was just for adding visible black borders around everything! This works great! Thanks!