1

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?

minerals
  • 6,090
  • 17
  • 62
  • 107

2 Answers2

1

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?

minerals
  • 6,090
  • 17
  • 62
  • 107
  • Probably late, but I can explain the notation for you: Those percentage symbols are essentially things that you can place in strings, and the values will be substituted. if `tag_start = 5 and len(word) = 3`, then `tag_end` will equal to `"5+3c"`. If you've heard of the python `str.format()`, then it's similar to this, except try to always use the `str.format()` as it's cleaner, and easier to use :) – Zizouz212 Aug 02 '15 at 23:04
0

"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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Bryan, in [this](http://stackoverflow.com/questions/3781670/tkinter-text-highlighting-in-python) solution you're saying that .search should provide all the necessary information about the searched pattern, but it only returns the starting index of the word. How do I retrieve the ending index? – minerals Oct 10 '13 at 06:20