wxPython – Colour with wxColour

In this tutorial we will discuss how to create a wxColour object, which we can use to add Colour for our wxPython window and it’s widgets.


wxColour Methods

A list of useful methods for the wxColour class.

MethodDescription
Alpha()Returns the Alpha Value of the Colour.
Blue()Returns the Blue Value of the Colour.
Get(bool)Returns a tuple with RGBA values. Pass False into parameters for only RGB.
Green()Returns the Green Value of the Colour.
MakeDisabled(int)Takes as parameter a “brightness” value, (ranging from 0 – 255), which creates a dimmed version of the colour.
Red()Returns the Red Value of the Colour.
Set(string)Sets the current colour using a String.

wxPython Colour with StaticText

Here’s a simple example, where we use a wxColour on a StaticText Widget. (Pretty much any wxPython function/method related to Colour will accept or return wxColour objects)

In this case, we are passing in a wxColour object that we created, into the SetForegroundColour() of the StaticText widget. This changes the color of the Text to match the color specified by the RGB values in the wxColour object.

import wx

class Window(wx.Frame):
    def __init__(self, title):
        super().__init__(parent = None, title = title, size = (420, 300))
        self.panel = wx.Panel(self)

        content = '''Quisque pretium venenatis interdum'''

        self.text1 = wx.StaticText(self.panel, label = content, 
                     pos = (40,100), size = (340, 40))

        RedColour = wx.Colour(255, 0, 0)
        self.text1.SetForegroundColour(RedColour)

                
        self.Centre()
        self.Show()

 
app = wx.App()
window = Window("WxPython Tutorial")
app.MainLoop()

The output of the above code:

wxPython colour

This marks the end of the wxPython Colour – wxColour Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments