First Tkinter script: requesting comments

Peter Otten __peter__ at web.de
Sun May 23 05:18:03 EDT 2010


Francesco Bochicchio wrote:

> One thing I don't understand, is why you need to 'poll' continuously
> for changes, except for demo purpose.

You can give the user immediate feedback on changes he makes. I'd argue that 
e. g. an "auto-apply" dialog with a revert button is much more usable and am 
surprised that this style hasn't caught on.

> If this is supposed to be a window for user to enter data into the
> program, the standard practice is to have a
> separate window (not the main one), with  apply and cancel buttons,
> where both buttons close the window but only
> the apply button register the change. So you need to do the stuff you
> do in the poll_exams function only in the
> callback of the apply button.

If you want live updates you can also use StringVar.trace() instead of 
polling:

from functools import partial

def grade_changed(name, index, var, *args):
    new_grade = to_int(var.get())
    if new_grade != exam_grades[index]:
        exam_grades[index] = new_grade
        print name, "-->", new_grade

pairs = zip(names, exam_grades_string)
for index, (name, var) in enumerate(pairs):
    var.trace("w", partial(grade_changed, name, index, var))

See also http://effbot.org/tkinterbook/variable.htm

Peter



More information about the Python-list mailing list