#!/usr/bin/env python """If Text search is called from a variable trace then the count variable is not be updated. I see this with Python 2.4.3 and Aqua Tcl/Tk 8.4.11 on MacOS X 10.4.7. I have not tested it elsewhere. Note that this works fine in tcl/tk (see tcl procedure at end of doc string) so this appears to be a Tkinter issue. To see the problem: - Run this python file. - Enter text in the Entry widget; - If the text exists in the Text widget you'll see a valid begInd, but count is always zero. It works correctly if the trace uses "after". To see this: - Modify the trace to call okSearch instead of doSearch/ - Repeat the test just listed. Also, here is the equivalent tcl/tk procedure (which *does* work) wish text .t pack .t .t tag configure found -background "light green" .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" entry .e -textvariable evar pack .e trace variable evar w dosearch proc dosearch {var dum1 dum2} { global evar global .t puts "evar = $evar" set begind [.t search -count cnt $evar 1.0 end] puts "found $evar begind = $begind cnt = $cnt" } """ from Tkinter import * root = Tk() t = Text() t.pack() evar = StringVar() e = Entry(textvariable = evar) e.pack() def doSearch(*args): var = IntVar() var.set(0) searchStr = evar.get() begInd = t.search(searchStr, "0.0", count=var) print "begInd = %r; count = %r" % (begInd, var.get()) def okSearch(*args): t.after(1, doSearch) 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") root.mainloop()