tastyminerals
tastyminerals

Reputation: 6538

Tkinter: How to get index for a specific word?

I've resolved my previous issues. Now, when my text is inserted it becomes bold from the word I need to the very END of the whole text. How do I highlight only the word?

self.text.insert('1.0', text)
self.text.grid()
tag_pos = self.text.search(word, '1.0')
self.text.tag_add('bold', tag_pos, END)
self.text.tag_configure('bold', font='TkDefaultFont 9 bold')

"self.text.tag_add('bold', tag_pos, END)" needs END to be the ending index of the word.

How do I retrieve it?

Upvotes: 1

Views: 525

Answers (2)

tastyminerals
tastyminerals

Reputation: 6538

I found the solution:

start = '1.0'
    while 1:                
        tag_start = self.text.search(word, start, stopindex=END, regexp=True)                
        if not tag_start: break
        tag_end = '%s+%dc' % (tag_start, len(word))
        self.text.tag_add('bold', tag_start, tag_end)
        self.text.tag_configure('bold', font='TkDefaultFont 9 bold')
        start = tag_start + "+1c"

Can somebody explain the '%s+%dc' and '+1c' notation?

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385980

"bold" is not the name of a valid font. You need to give it valid font description. tkinter comes with a tkFont module which lets you define fonts.

Upvotes: 0

Related Questions