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()