StyledTextCtrl() change bg and fg colors

I’m using the line below to change the StyledTextCtrl() bg/fg colors – but – when I start to enter text, the text displays in what I guess is it’s default colors.

self.editor.StyleSetBackground(wx.stc.STC_STYLE_DEFAULT, (192, 192, 192))

So, obviously, this line just sets the editor colors but not the text that is entered.

What method can I use to set the text colors?

Thank you…

wx.stc.STC_STYLE_DEFAULT is style number 32. It defines the attributes that all styles receive when wx.stc.StyleClearAll() is called.

As far as I can tell, when new text is typed into an STC, it is set to style 0, not style 32.

When I change style 0’s foreground colour using:

    self.editor.StyleSetForeground(0, (192, 192, 192))

text typed into the STC is shown in the desired light grey colour.

Note that that colour is very close to the default background colour used for selected text, making it impossible to read. You would probably need to call self.editor.SetSelForeground() and/or self.editor.SetSelBackground() to make selected text more readable.

[This was tested using Python 3.8.5 + wxPython 4.1.1 gtk3 (phoenix) wxWidgets 3.1.5 + Linux Mint 20.1].

Yes, that worked fine. How in the world did you figure that out? I would have been here until Xmas trying to figure that one out!!! Thank you Richard.

I wrote a little experimental script in order to investigate how to apply different styles when typing in new text. That script binds stc.EVT_STC_MODIFIED events to a handler which applies a current style, which can be changed by the buttons at the top of the frame.

I temporarily added a print() call to the handler, before the handler changes the style. The print() call outputs the position of the caret and the style that would have been used for the text. The print() call always outputs zero for the style and not 32.

import wx
import wx.stc as stc


class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.SetTitle("STC Styles")

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        default_button = wx.Button(self, wx.ID_ANY, "Default")
        sizer.Add(default_button, 0, wx.RIGHT, 10)
        red_button = wx.Button(self, wx.ID_ANY, "Red|White")
        sizer.Add(red_button, 0, wx.RIGHT, 10)
        blue_button = wx.Button(self, wx.ID_ANY, "Blue|Yellow")
        sizer.Add(blue_button, 0, wx.RIGHT, 10)

        self.Bind(wx.EVT_BUTTON, self.OnDefault, default_button)
        self.Bind(wx.EVT_BUTTON, self.OnRed,     red_button)
        self.Bind(wx.EVT_BUTTON, self.OnBlue,    blue_button)


        self.editor = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE)

        # Only trigger EVT_STC_MODIFIED when text inserted
        self.editor.SetModEventMask(stc.STC_MOD_INSERTTEXT)
        self.editor.Bind(stc.EVT_STC_MODIFIED, self.OnTextModified)


        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(sizer, 0, wx.TOP|wx.BOTTOM, 8)
        main_sizer.Add(self.editor, 1, wx.EXPAND)
        self.SetSizer(main_sizer)
        self.SetSize((600, 400))

        self.editor.StyleSetSpec(0, "fore:#000000,face:Times,size:10")
        self.editor.StyleSetSpec(1, "fore:#ff0000,face:Helvetica,size:10")
        self.editor.StyleSetSpec(2, "fore:#0000ff,face:Courier,bold,back:#ffff00,size:12")

        self.current_style = 0


    def OnDefault(self, _event):
        self.current_style = 0
        self.editor.SetFocus()

    def OnRed(self, _event):
        self.current_style = 1
        self.editor.SetFocus()

    def OnBlue(self, _event):
        self.current_style = 2
        self.editor.SetFocus()

    def OnTextModified(self, event):
        pos = event.GetPosition()
        print(pos, self.editor.GetStyleAt(pos))
        length = event.GetLength()
        self.editor.StartStyling(pos)
        self.editor.SetStyling(length, self.current_style)


if __name__ == '__main__':

    app = wx.App()
    frame = TestFrame(None)
    frame.Show()
    app.MainLoop()


Lordy…very awsome…!!!