Tkinter text entry questions: set_text, onchange.

Dirk Gerrits dirk at gerrits.homeip.net
Wed Apr 23 16:59:26 EDT 2003


I've been creating a little GUI application with Python 2.3a2 and 
Tkinter and I've been running into some issues with Entry widgets.

I have some of them with state='readonly', and I want to update their 
text values from Python functions. For reading their text values there 
is the simple method .get(), but there doesn't seem to be a 
corresponding .set() method? So I've tried using delete followed by 
insert, but this doesn't seem to work for state='readonly'. Finally, I 
made the following helper function:

def set_text(entry, text):
     old_state = entry["state"]
     entry["state"] = "normal"
     entry.delete(0, END)
     entry.insert(0, str(text))
     entry["state"] = old_state

This seems to do the trick, but it seems to me that it's a lot more 
trouble then necessary. Isn't there a more obvious, more 'pythonic' 
solution?

Also, I have some state='normal' Entry widgets for which I want to call 
a function whenever their value changes. Currently, I use the following 
helper function, but just as with set_text, I feel it is a non-obvious hack:

def bind_onchange(entry, command):
     for sequence in ["<Key>", "<KeyPress>", "<KeyRelease>"]:
         entry.bind(sequence, command)

Is there a better solution for this (too)?

Thanks in advance,

Dirk Gerrits







More information about the Python-list mailing list