Search code examples
wxpython

wxPython get colour of panel


I am trying to retrieve the background colour of a panel in wxPython

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Panel colour')
        panel = MainPanel(self)
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(panel)
        self.SetSizerAndFit(main_sizer)

        self.Centre()
        self.Show()
        print(self.GetBackgroundColour())
        print(panel.GetBackgroundColour())

class MainPanel(wx.Panel):
    def __init__(self, frame):
        wx.Panel.__init__(self, frame)
        lbl_description = wx.StaticText(self, label='Hello world')
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(lbl_description, flag=wx.ALL, border=10)
        self.SetSizer(main_sizer)


if __name__ == "__main__":
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

enter image description here

but the output I get is

(225, 225, 225, 255)
(225, 225, 225, 255)

Clearly, I am doing something wrong. How should I get it?


Solution

  • These numbers gives you the colour as RGB values.

    enter image description here

    RGB colours are defined by three values for Red, Green and Blue going from 0 to 255. The fourth values is the alpha value (transparency).