Hi everyone,

I have a problem to setup a custom lexer using in wx.stc.StyledTextCtrl (wxPython). I have a text file which consit of certain keywords which I would like to highlight. The text file is not related to any of the programing languages and so I cannot use the buildin lexers. The method I tried is slow and I was just wondering if someone knows a better way. Here is my code:
First, I create a custom lexer and then bind wx.stc.EVT_STC_STYLENEEDED to the appropriate function for highlighting

self.text.SetLexer(wx.stc.STC_LEX_CONTAINER)
self.text.SetStyleBits(5)
self.text.Bind(wx.stc.EVT_STC_STYLENEEDED, self.Color)

than I create the actual function to handle the event:

def onColor1(self, event):
            styled_text=['BFG', 'FYI', 'DPS', 'DSD']
            fulltext = self.text.GetText()
            self.text.StyleSetSpec(2, 'fore:#0000FF, back:#FFFFFF,face:Courier New,size:12') 
            for item in styled_text:
                pos=fulltext.find(item)
                while pos!=-1: 
                        self.text.StartStyling(pos, 0xff)
                        self.text.SetStyling(len(item), 2)
                        pos = fulltext.find(item, pos+1)

this seems to work fine but it's very slow.

I appreciate any help

thanks in advance

check with the bind method name( event handler method). its different to the method name it calls.
self.Color is not the same as onColor.

thanks, it's a typo. I saw this then it was too late to correct it. However, it's not my main problem.

I guess I solve my problem in a way by using:

self.Bind(wx.EVT_KEY_UP, self.onColour) #instead of: 
self.text.Bind(wx.stc.EVT_STC_STYLENEEDED, self.onColour) 
# and the following handler function:
def onColour(self,event):           
            styled_text=['BFG', 'FYI', 'DPS', 'DSD']
            fulltext = self.text.GetText()
            self.text.StyleSetSpec(2, 'fore:#221dff, back:#FFFFFF,face:Courier New,size:12') 
            for item in styled_text:
                pos = fulltext.find(item)
                while pos in range(0, len(fulltext)):
                    self.text.StartStyling(pos, 2)
                    self.text.SetStyling(len(item), 2)
                    pos=fulltext.find(item, pos+1)  
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.