-2

How can I find the position of a specific text string inside my text?

For example, I have the text "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", and when I search for "ipsum" I want to find out its position.

if "ipsum" in text:
    print position


else:
    pass

The position I'm looking for is something like "1.6" If I want to change the color of the text it looks like this:

text.tag_add("foo", "1.28", "1.39")

Then in order to change the color of the text that I''ve searched for I need to get its postion somehow, right? I could be wrong but I'm looking for some example or guidance. Thanks! :)

Aleksandr Kovalev
  • 3,508
  • 4
  • 34
  • 36
David Olsson
  • 21
  • 1
  • 4
  • 1
    what do you mean by 'The position I'm looking for is something like "1.6"'? what does 1.6 represent? First row, 6th column? – Julien Spronck May 06 '15 at 13:02
  • If I want to change the color of the text it looks like this: text.tag_add("foo", "1.28", "1.39") Then in order to change the color of the text that I''ve searched for I need to get its postion somehow, right? I could be wrong but I'm looking for some example or guidance. Thanks! :) – David Olsson May 06 '15 at 13:04
  • I removed the tkinter tag, since this question seems to have nothing to do with tkinter. You're simply asking how to search a string for a substring. – Bryan Oakley May 06 '15 at 13:08
  • 1
    possible duplicate http://stackoverflow.com/questions/19464813/explain-tkinter-text-search-method? – Julien Spronck May 06 '15 at 13:10
  • 3
    Removing the tkinter tag was a mistake -- that "x.y" position notation is how the tkinter text widget identifies positions within the widget. – bgporter May 06 '15 at 13:10
  • 1
    You are asking about searching a text string for a substring, but is your real question how to search for, and highlight, text in a text widget? What is the _real_ problem you are trying to solve? – Bryan Oakley May 06 '15 at 13:26
  • I want to highlight surten string in my text. – David Olsson May 06 '15 at 13:42
  • David, you need to be more precise in your language. When you say "in my text", do you mean "in my text _widget_" or "in my text _string_"? The solution to highlighting something in a string is different than highlighting something in a text widget, and the solution to the former doesn't necessarily lead to a solution in the latter. If your ultimate goal is to highlight text in a text widget, that is answered here: http://stackoverflow.com/questions/19464813/explain-tkinter-text-search-method – Bryan Oakley May 06 '15 at 14:04

1 Answers1

5

Use the Text.search() function, e.g

text = Text(root)
text.insert(INSERT, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
pos = text.search('ipsum', '1.0', stopindex=END)
print pos

This should output 1.6.

To highlight the target string you can use tag_add() and tag_config()...

root = Tk()
text = Text(root)
text.pack()
text.insert(INSERT, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
target = 'ipsum'
start_pos = text.search(target, '1.0', stopindex=END)
print '{!r}'.format(start_pos)
if start_pos:
    end_pos = '{}+{}c'.format(start_pos, len(target))
    print '{!r}'.format(end_pos)
    text.tag_add('highlight', start_pos, end_pos)
    text.tag_config('highlight', foreground='red')

root.mainloop()

If you would like to search for all instances of a word within the text widget, or more than one word, take a look at this answer: https://stackoverflow.com/a/29316232/21945

Community
  • 1
  • 1
mhawke
  • 84,695
  • 9
  • 117
  • 138