无法获得突出显示目标所有实例的查找功能(文本编辑器)

Cannot get find function to highlight all instances of the target (text editor)

最近我一直在开发 GUI python 纯文本编辑器。代码调用此函数:

def Find(event=None):
    def find_button_pressed():
        targetfind = e1.get()
        print targetfind
        if targetfind:
            where = textPad.search(targetfind, INSERT, END)
            if where:
                print where
                pastit = where + ('+%dc' % len(targetfind))
                #self.text.tag_remove(SEL, '1.0', END)
                textPad.tag_add(SEL, where, pastit)
                for targetfind in where:
                    textPad.mark_set(INSERT, pastit)
                    textPad.see(INSERT)
                textPad.focus()
    win = Toplevel() 
    Label(win, text="Find:").pack()
    e1 = Entry(win)
    e1.pack()
    Button(win, text="Find Me!!!!", command=find_button_pressed).pack()
    textPad.focus()

但是,我无法让它工作。该代码应该突出显示所有应该找到的单词,但是,用户必须单击按钮 "Find Me!!!" 与要突出显示的单词一样多的次数才能突出显示所有单词。已经在互联网上搜索任何可以帮助我修复此查找功能的东西,但我没有成功找到任何关于如何这样做的解释。如果您能帮助修复此查找功能,我们将不胜感激。

编辑

这是新的代码还是没有解决问题:

def Find(event=None):
    def find_button_pressed():
        start = "1.0"
        end = "end"
        start = textPad.index(start)
        end = textPad.index(end)
        count= Tkinter.IntVar() 
        count=count
        textPad.mark_set("matchStart", start)
        textPad.mark_set("matchEnd", start)
        textPad.mark_set("searchLimit", end)
        targetfind = e1.get()
        print targetfind
        if targetfind:
            while True:
                where = textPad.search(targetfind, "matchEnd", "searchLimit",
                                       count=count)
                if where == "": break
                elif where:
                    print where
                    pastit = where + ('+%dc' % len(targetfind))
                    textPad.tag_remove(SEL, '1.0', END) 
                    textPad.mark_set("matchStart", where)
                    textPad.mark_set("matchEnd", "%s+%sc" % (where, count.get()))
                    textPad.tag_add(SEL, where, pastit)
                    textPad.see(INSERT)
                    textPad.focus()
    win = Toplevel() 
    Label(win, text="Find:").pack()
    e1 = Entry(win)
    e1.pack()
    Button(win, text="Find Me!!!!", command=find_button_pressed).pack()
    textPad.focus()

由于许多有用的评论回答了我的问题,我决定post我的固定代码作为答案。

这是代码:

def Find(event=None):
    def find_button_pressed():
        start = "1.0"
        end = "end"
        start = textPad.index(start)
        end = textPad.index(end)
        count= Tkinter.IntVar() 
        count=count
        textPad.mark_set("matchStart", start)
        textPad.mark_set("matchEnd", start)
        textPad.mark_set("searchLimit", end)
        targetfind = e1.get()
        if targetfind:
            while True:
                where = textPad.search(targetfind, "matchEnd", "searchLimit",
                                       count=count)
                if where == "": break
                elif where:
                    pastit = where + ('+%dc' % len(targetfind)) 
                    textPad.mark_set("matchStart", where)
                    textPad.mark_set("matchEnd", "%s+%sc" % (where, count.get()))
                    textPad.tag_add(SEL, where, pastit)
                    textPad.see(INSERT)
                    textPad.focus()
        win.destroy()
    textPad.tag_remove(SEL, '1.0', END)
    win = Toplevel() 
    Label(win, text="Find:").pack()
    e1 = Entry(win)
    e1.pack()
    Button(win, text="Find Me!!!!", command=find_button_pressed).pack()
    textPad.focus()