Explain Tkinter text search method

前端 未结 2 1356
借酒劲吻你
借酒劲吻你 2020-11-29 12:26

I don\'t quite understand how text.search method works. For example there is a sentence: Today a red car appeared in the park. I need to find a red car

2条回答
  •  青春惊慌失措
    2020-11-29 12:59

    It may be a problem of the indexes.

    In a program of mine, i have to search the start index and calculate the end index

    my method for example, it works fine:

    def highlight(self):
        start = 1.0
        pos = self.area_example.search(self.item.name, start, stopindex=END)
        while pos:
            length = len(self.item.name)
            row, col = pos.split('.')
            end = int(col) + length
            end = row + '.' + str(end)
            self.area_example.tag_add('highlight', pos, end)
            start = end
            pos = self.area_example.search(self.item.name, start, stopindex=END)
        self.area_example.tag_config('highlight', background='white', foreground='red')
    

提交回复
热议问题