wxPython search word in TextCrl

327 views Asked by At

I have a simple mini notepad program written in wxPython. the text is written in TextCtrl:

  self.rtb = wx.TextCtrl(self, ID_RTB, size=wx.Size(400,200),
                          style=wx.TE_MULTILINE | wx.TE_RICH2)

I wanted to implement a search feature where user gives a word and I'm highlighting it in the TextCtrl.

my method is:

    def SearchIt(self,e):
    for line in self.rtb: 
        if self.text in line:
            print line   #will be changed to highlight when it will work

However i get an error:

TypeError: 'TextCtrl' object is not iterable

any suggestion how to resolve it? It's too complex to change the TextCtrl now. is there another way I can search a word in the TextCtrl or maybe a way to convert the text in TextCtrl to another data structure just for the search?

1

There are 1 answers

0
Ed Smith On BEST ANSWER

You are trying to interate textCtrl, instead of the lines in it. You could do something like this,

def SearchIt(self,e):
    for i in range(self.rtb.GetNumberOfLines()):
        line = self.rtb.GetLineText(i)
        if self.text in line:
            print(line)