(original) (raw)
#!/usr/bin/env python """If Text search is called with an empty search pattern an index error is thrown To see the problem: - Run this python file. - Enter a few characters in the Entry widget and remove them again; - If you remove the last character you will see the exception output. """ from Tkinter import * iVar = None root = Tk() t = Text(root) t.pack() evar = StringVar() e = Entry(root, textvariable = evar) e.pack() l = Label(root, text='enter a few characters and delete them again') l.pack() def doSearch(*args): global iVar print 'dosearch' searchStr = evar.get() print 'searchStr', searchStr begInd = t.search(searchStr, "0.0", count=iVar) #begInd = t.search(None, "0.0", count=iVar, stopindex="1.1") print "begInd = %r; %s count = %r" % (begInd, iVar, iVar.get()) traceFct = evar.trace_variable("w", doSearch) t.insert("end", "Line 1 of sample text\n") t.insert("end", "Line 2 of sample text\n") t.insert("end", "More text\n") t.insert("end", "Yet more text\n") t.insert("end", "One more line of text\n") iVar = IntVar() root.mainloop()