0

So, I am trying to work on implementing a find_and_replace option for my tkinter text editor. I've set up how my program finds the text within the program and that works perfectly, my only problem is that I am using a variable to locate where the exact word is.

    text.delete(1.other_other, 1.(other_other+other))

The 1 represents the line number, but whenever I run the program it always tells me I have a syntax error just before the comma. Any ideas are helpful.

Thanks :)

  • What is the (line) number there supposed to do? What is `other_other`? What is `other`? – poke May 07 '15 at 11:53
  • `1.thing` is not valid python syntax, and neither is `1.(thing)`. Can you explain in more detail what you would like these expressions to do? Then we can help you find working equivalent techniques. – Kevin May 07 '15 at 11:53
  • Sure, the normal thing you would see is something like 1.0 What this represents is the first line (the 1) at what is technically the first character (the 0). The variables are named other because I wrote this quickly as a test and therefore placeholders. Using a for loop a small bit back I used a for loop to count all the characters in the line, therefore deleting at the start of the word through to the end (other is a character count of the whole word). What I want these variables to do is act as the character count, but I just don't know how to do something like that. – user3458038 May 07 '15 at 11:56
  • Ok, that makes sense :-) So basically you were doing `text.delete(1.3, 1.9)` or something before, and now want to know how to use variables instead of literal 3 and 9. – Kevin May 07 '15 at 12:06
  • Are you aware that the text widget has a search function that will give you the exact index of a string matching a pattern? For example: http://stackoverflow.com/a/3781773/7432 – Bryan Oakley May 07 '15 at 12:30

1 Answers1

2

The index must be a string. 1.other_other isn't a string. You can use python's string formatting to give you a proper index:

start_index = "1.%d" % other_other
end_index = "1.%d" % other_other+other
text.delete(start_index, end_index)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685