Search code examples
pythonwxpythonwxwidgets

Python WxWidgets - how to have static text labels appear on top of gradient painted panel


I have a WxWidgets app. I'm working on with Python. The app has a main frame, a menu bar, a status bar at the bottom, and for the middle main content I want a gradient painted panel to give a nice background shading effect. I have this working, but I now want to start adding my GUI widgets to the panel (e.g. a set of static text labels and edit boxes). How do I add these to the panel so they appear on top of the panel? In my example code below adding a text field to the panel causes it to collapse to a small size.

Thanks for any help.

def createGui(self, parent, title):
    wx.Frame.__init__(self, parent, title=title, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
    self.SetBackgroundColour("white")
    self.SetSizeHints(800, 640, 800, 600)
    self.sizer = wx.BoxSizer(wx.VERTICAL)
    self.panel = wx.Panel(self, size=(800, 600))
    self.sizer.Add(self.panel)
    self.panel.Bind(wx.EVT_PAINT, self.on_paint)

    self.label1 = wx.StaticText(self, -1, "PV Power" , wx.Point(1, 1))
    # This next part is not working, I want to add static text at certain geometric locations to the panel, so I get to see
    # the panel with it's background gradient and the text labels on top
    #self.panel.AddChild(self.label1)

    menubar = wx.MenuBar()
    fileMenu = wx.Menu()
    fitem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')
    menubar.Append(fileMenu, '&File')
    self.SetMenuBar(menubar)

    self.Bind(wx.EVT_MENU, self.onQuit, fitem)

    # Create a box at the bottom for buttons
    self.statusbar = self.CreateStatusBar()
    self.statusbar.SetStatusText('Waiting to run')
    self.sizer.Add(self.statusbar)

    self.SetSizer(self.sizer)
    self.SetAutoLayout(1)
    self.sizer.Fit(self)
    self.Show()

def on_paint(self, event):
    dc = wx.PaintDC(self.panel)
    x = 0
    y = 0
    w, h = self.GetSize()
    dc.GradientFillLinear((x, y, w, h), 'white', 'light grey')

Solution

  • from your question it sounds like all you need to do is

    self.panel.SetMinSize((800,600))
    

    right after you create your panel, unfortunately limitations on wxStaticText will cause un alot of headaches (it will have a white background(or at least colored)) there are some hacks you can do to set the background of the text to an area of an image that basically mimics background transparency ... but they are a pain