1

I am creating a search box for a text editor using the text.search("1.0", entry.get(), stopindex="end") command. The problem is I get the error TclError: bad text index "text here". I followed the question here for guidance on the search() function but I don't know what is wrong. How do I fix this to make the search box work? Here is my code:

from Tkinter import *
import tkMessageBox
import tkFileDialog

class Main(object):
    def __init__(self, root):
        root.title("PyText")

        self.f1=Frame(root)
        self.f1.grid(row=1)

        #Main text widget
        self.t1=Text(root)
        self.t1.config(width=90, height=40, undo=True, bg="#41494B", highlightbackground="#41494B", foreground="white")
        self.t1.grid(row=2, padx=10, pady=10)

        self.search=Entry(self.f1, highlightbackground="#2b373a")
        self.search.grid(column=2, row=0)

        self.search_button=Button(self.f1, highlightbackground="#2b373a", command=lambda: self.t1.search("1.0", self.search.get(), stopindex="end"), text="Search")
        self.search_button.grid(row=3, column=0)

root = Tk()
root.config(bg="#2b373a")
app = Main(root)
root.mainloop()
Community
  • 1
  • 1
Jonah Fleming
  • 1,187
  • 4
  • 19
  • 31
  • 1
    You seem to have the arguments in the wrong order. It should be the search term first, then the start index, then the stop index, while you have the start index, then the search term, then the stop index. – TigerhawkT3 Nov 07 '15 at 03:15
  • Thank you! If you put that in an answer ill accept it! – Jonah Fleming Nov 07 '15 at 03:31
  • I recommend you _not_ use `lambda`. lambda servers no purpose in this example, and it makes your code harder to debug. Create a method to do your search, and have your binding tied directly to that method. – Bryan Oakley Nov 07 '15 at 12:08

1 Answers1

2

You have transposed the arguments to search(). The first argument needs to be the search query, and the second argument needs to be the starting index.

self.t1.search("1.0", self.search.get(), stopindex="end")

should be:

self.t1.search(self.search.get(), "1.0", stopindex="end")
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97