colorswxpythontextctrlubuntu

How to change ForegroundColour of a disabled TextCtrl from wxPython


how can I change the foregroundcolor of a disabled TextCtrl from wxPython?
I mean, when I change the color with SetForegroundColour it only changes for enabled status. When I disable the TextCtrl, it remains dark grey even if I set it red, for example.
Thanks in advance!

import wx

class MainFrame(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)
        self.InitUI()
        self.Fit()
        self.Show(True)              

    def InitUI(self):
        text = wx.TextCtrl(self)
        text.SetForegroundColour((255,0,0))
        text.SetValue('Example')
        text.Enable(False)

def main():
    app = wx.App()
    MainFrame(None)
    app.MainLoop()

if __name__ == '__main__':
    main()

Solution

  • The short answer is that you can't.
    The background and foreground colours are overridden by the fact that you have disabled them. Your operating system environment determines how the disabled items will look.

    Of course that doesn't mean that you can't get around the issue.
    If instead of disabling/enabling the item, you set a True/False flag instead, you can then check that flag when the event is triggered and depending on whether the flag is True or False, you process the event or not.
    This allows you to process events and display whatever colours that you like.