obj.SetBackgroundColour(obj.GetBackgroundColour()) is not a no-op

I was surprised to find that I couldn’t reset the background colour of a control back to the default by setting the colour back to what I’d read earlier using .GetBackgroundColour.

It happened with a radio button on a Panel on a Frame, where the panel background had not been explicitly set, but the frame had a white background set. Further experiments showed that
panel.SetBackgroundColour(panel.GetBackgroundColour())
is not a no-op: It changed the panel from the default white to grey.

Anyone have an explanation for this? And maybe a suggestion for how to read the true background colour, or how to otherwise restore it to the original after a change.

(MSW, wx 4.2.3.)

I found an old post by @driscollis which may be related.

He suggested setting the background colour to wx.NullColour.

Below is my example which appears to work on wxPython 4.2.3 gtk3 (phoenix) wxWidgets 3.2.7 + Python 3.12.3 + Linux Mint 22.2

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.SetSize((400, 300))
        self.SetTitle("Get & Set Panel Colour")
        self.main_panel = wx.Panel(self, wx.ID_ANY)
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        self.inner_panel = wx.Panel(self.main_panel, wx.ID_ANY)
        main_sizer.Add(self.inner_panel, 1, wx.ALL | wx.EXPAND, 4)
        bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.Add(bottom_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 4)
        self.set_button = wx.Button(self.main_panel, wx.ID_ANY, "Set")
        bottom_sizer.Add(self.set_button, 0, wx.RIGHT, 24)
        self.reset_button = wx.Button(self.main_panel, wx.ID_ANY, "Reset")
        bottom_sizer.Add(self.reset_button, 0, 0, 0)
        self.main_panel.SetSizer(main_sizer)
        self.Layout()

        self.reset_button.Bind(wx.EVT_BUTTON, self.OnReset)
        self.set_button.Bind(wx.EVT_BUTTON, self.OnSet)

        sys_background = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND)
        print(sys_background)

        self.main_panel.SetBackgroundColour(wx.WHITE)
        self.orig_colour = self.inner_panel.GetBackgroundColour()
        print(self.orig_colour)


    def OnReset(self, _event):
        # self.inner_panel.SetBackgroundColour(self.orig_colour)
        self.inner_panel.SetBackgroundColour(wx.NullColour)

    def OnSet(self, _event):
        self.inner_panel.SetBackgroundColour(wx.GREEN)


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
2 Likes

Thanks, I tried it and wx.NullColour does work. Well, not for my use case, since I also needed to know the actual colour so that I could choose other colours to match, but I got around that by hardwiring ‘WHITE’.

Missing the next line, after that SetBackgroundColour, and the other at OnSet:
self.inner_panel.Refresh()

1 Like