0

Hello so I'm trying to figure out how to make a find function in tkinter. I did figure out how to get tkinter to highlight a specifically searched text there is a few issues I'm having though. Lets say they have the word at line 3 it will highlight all the lines to line 3 so it will highlight line 1, 2, and all of line 3. I'm trying to figure out how to just get it to select the word. I also don't think I'm going about it the right way with highlighting it because they wouldn't be able to right click it and copy it so if anyone can help me I'd really apperciate it!

def find():
    findString = simpledialog.askstring("Find...", "Enter text")
    textData = notepad.get(0.0, END)        
    cannot = 'Can not find:' 
    find = findString
    cannotfind = cannot + " " + find 

    if findString in textData:
        notepad.tag_add(findString, 0.0, "END")
        notepad.tag_config(findString, background="blue", foreground="white")

    else:
        messagebox.showinfo('Not Found!', cannotfind)

*edit I was thinking of instead of using (0.0, END) figuring out how to store the coordinates in a variable of the searched text that would solve all my problems but I don't know if its possible.

Gwendal Grelier
  • 526
  • 1
  • 7
  • 21
Jake
  • 37
  • 1
  • 8
  • I originally thought to bring the courser to the text and figure out how to convert the select all code I created to select the text. But the problem with tkinter is when I use the simpledialog.askstring code and press okay it will not highlight anything anymore since the courser is not on the document. – Jake Apr 12 '18 at 07:11
  • I don't think Tkinter can do that using only one text notepad... You will have to recreate multiple widgets and put different background color for each one... You miht have to slice your text to isolate the desired words to hilight – Gwendal Grelier Apr 12 '18 at 07:11
  • How would you recommend going about doing that I'm still pretty new with Python in general I'm only 3 months into it. – Jake Apr 12 '18 at 07:14
  • Slicing the text I mean because that seems that may help me with future developments – Jake Apr 12 '18 at 07:16
  • I was kinda thinking of instead of using (0.0, END) figuring out how to store the coordinates in a variable of the searched text. – Jake Apr 12 '18 at 07:22
  • @GwendalGrelier: not true. The text widget can show as many colors as you want. – Bryan Oakley Apr 12 '18 at 11:43
  • @BryanOakley My bad, do you have an exemple or a link ? – Gwendal Grelier Apr 12 '18 at 11:48
  • 1
    @GwendalGrelier: read up on how to use tags in the text widget documentation. – Bryan Oakley Apr 12 '18 at 11:53

1 Answers1

0

An idea of how to do it:

First you split you sentence:

my_sentence = "Python is so cool"
my_list = my_sentence.split() # returns ["Python","is","so","cool"]

Then you could iterate over this sentence to find the desired word:

word_index = None
for i, word in enumerate(my_list):
    if word == my_test_string:
        word_index = i

The you can cut your sentence at the desired index and recombine the parts together:

string_start = my_list[0:word_index]
string_start = " ".join(string_start) 
string_end = my_list[word_index+1:]
string_end = " ".join(string_end)
my_word = my_list[word_index]

You then have to recreate labels with the desired backgrounds, colors, ...

Gwendal Grelier
  • 526
  • 1
  • 7
  • 21