0

recently I have been working on a GUI python plain text editor. The code calls this function:

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

However, I cannot get it to work. The code is supposed to highlight all of the words that are supposed to be found, however, the user must clicks on the button "Find Me!!!" as many times as there are words to be highlighted in order to highlight all of them. have searched the internet looking for anything that might help me fix this find feature, but I have not succeeded in finding any explanations as to how I might do so. Any help in fixing this find feature would be very much appreciated.

EDIT

This is the new code that still does not solve the problem:

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()
Pseudonym Enigma
  • 129
  • 1
  • 2
  • 14
  • Why are you using a function inside another function? What's the advantage? – nbro Feb 13 '15 at 19:29
  • @Rinzler: The inner function is a callback. I don't think that's the problem. – Blckknght Feb 13 '15 at 19:30
  • The search method returns just the index of the first occurrence in the Text widget, therefore you have to click as many times as you said to have them highlighted. Try first to find all the occurrences in the Text widget of a certain target, and then apply your `tag` with `tag_add` method to all of these occurrences. – nbro Feb 13 '15 at 19:35
  • I can not seem to manage. I added a for loop above the code that defines pastit, but the only difference is that only the first letter of the word is highlighted. The modified portion of the code is `for targetfind in where: pastit = where + ('+%dc' % len(targetfind))` – Pseudonym Enigma Feb 13 '15 at 19:45
  • 1
    @PseudonymEnigma See [this example](http://stackoverflow.com/questions/3781670/how-to-highlight-text-in-a-tkinter-text-widget) by Bryan. – nbro Feb 13 '15 at 19:52
  • Using the example, I still can not get the function to work. I have appended my updated (but still not functional) code to the end of my original question. – Pseudonym Enigma Feb 13 '15 at 20:24
  • @Rinzler I have updated the code in my question to reflect the changes I have made (thanks to the link you provided). It still does not work. What am I doing wrong? – Pseudonym Enigma Feb 14 '15 at 00:13
  • I have once again updated the code. Now, it gives me the coordinates to all of the instances of the target. However, it only highlights the last one. How should I fix this? Also, I apologize for accidentally moving this discussion to chat. I did not mean to. – Pseudonym Enigma Feb 14 '15 at 02:19
  • 1
    @PseudonymEnigma: it only shows the last one because each time through your loop you're deleting all of the SEL tags. – Bryan Oakley Feb 14 '15 at 13:11
  • Thank you. Now it works. As there is no answer I will post the fixed code as my own answer. – Pseudonym Enigma Feb 14 '15 at 18:16

1 Answers1

0

As my question was answered through numerous helpful comments, I have decided to post my fixed code as an answer.

This is the code:

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()
Pseudonym Enigma
  • 129
  • 1
  • 2
  • 14