3

Suppose this is the program that I need to use and I want the find() function to select the word "Hello" from Text widget when it is called-

from Tkinter import *
def find():
    tx.select_word("Hello")
root = Tk()
tx = Text(root)
bu = Button(root, text = "Find Hello", command = find)
tx.pack()
bu.pack()
root.mainloop()

When the Button "Find Hello" is pressed the widget will look like this.Text Widget

Suparno
  • 111
  • 1
  • 6

1 Answers1

4

I made a simple program to select all the Hellos in the Text:

from tkinter import *


def find_nth(haystack, needle, n):                                   #Function to find the index of nth substring in a string
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start+len(needle))
        n -= 1
    return start

def find():
    word = "H"                                                       #Targetted Word  
    text, line = tx.get("1.0",END), 0                                #text getting text of the widget
    text = text.split("\n")                                          #splitting and getting list on the newlines
    for x, i in enumerate(text):                                     #Looping through that list
        if word in i:                                                #if targetted word is in the xth string i of the list
            for e in range(0, i.count(word)):
                index = find_nth(i, word, e+1)                       #Getting the index of the word
                start = float(str(x+1)+"."+str(index))               #Making the indices for tkinter
                end = float(str(x+1)+"."+str(index+len(word)))       #Making the indices for tkinter
                tx.focus()                                           #Focusing on the Text widget to make the selection visible
                tx.tag_add("sel", start, end)                        #selecting from index start till index end
root = Tk()
tx = Text(root)
tx.insert(END, "World Hello World\nHello World Hello Hello\nHello Hello")
bu = Button(root, text = "Find Hello", command = find)
tx.pack()
bu.pack()
root.mainloop()

Output:

enter image description here

I used this answer for help.

Nouman
  • 6,947
  • 7
  • 32
  • 60
  • If the tag_add value is something like 2.10, float is automatically removing the "0". Do you have any solution? – Suparno Sep 04 '19 at 18:57
  • @Suparno You can use it as string and convert to float when needed for an input. You can ask a separate question about this. – Nouman Sep 04 '19 at 20:32
  • Actually I tried but nothing changed. Then I am asking a separate question, thank you. – Suparno Sep 05 '19 at 18:45
  • @BlackThunder Please How can we get tooltip for each selected word? – bib Aug 10 '22 at 17:04
  • @bib For tooltips, you'd have to track the mouse position to see if it is above any of the selected words, and then use top-level window to create the tooltip. Personally, I would recommend using some HTML-based UI such as using eel framework. – Nouman Aug 11 '22 at 10:37