Error in 2.2.1 Tkinter? (was: limiting text input in Tkinter Entry widget)
Cameron Laird
claird at lairds.com
Sun Feb 22 14:26:11 EST 2004
In article <103ho4p6m98gs97 at news.supernews.com>,
John Roth <newsgroups at jhrothjr.com> wrote:
>
>"Otto Krüse" <otto_kruse at hotmail.com> wrote in message
>news:4038d6c2$0$49821$1b2cd167 at news.wanadoo.nl...
.
.
.
>> Does anyone know a way to limit the amount of characters an entry widget
>> can take? Is there an easy option to set for this or does this problem
>> require some python code? Can't seem to find answers in any documentation.
>>
>> The code:
>> self.e1 = Entry(frame, width="4")
>> self.e2 = Entry(frame, width="2")
>
>The MegaWidgits package has an EntryField widgit that has some
>built-in validation, and has a hook for you to insert a validation function
>or method.
>
>Otherwise, you need to do the validation as you collect text characters
>and pass them to the widgit.
>
>Validation code is inherently ugly. Not complex, just ugly. In your case
>I'd probably do some form of pattern driven validation.
.
.
.
Without disputing the general proposition about the aesthetics
of validation of user data, there's supposed to be a simpler
answer. Tkinter includes textvariable and trace capabilities
which should make this as compact as
import Tkinter
root = Tkinter.Tk()
def validate(name, index, mode):
value = root.getvar(name)
# Truncate the entry text to its first four characters.
root.setvar(name, value[0:4])
my_variable = Tkinter.StringVar()
my_variable.trace_variable('w', validate)
my_entry = Tkinter.Entry(width = 4, textvariable = my_variable)
my_entry.pack()
Tkinter.mainloop
In practice, one would likely encapsulate the "bookkeeping" in
an object.
The problem is that, when I run this example, Tkinter tosses the
exception
TclError: can't read "PY_VAR0": no such variable
This surprises me. I'm *sure* I've done this before. Before I
dive into the distribution source code, is it obvious to anyone
else what I'm missing?
--
Cameron Laird <claird at phaseit.net>
Business: http://www.Phaseit.net
More information about the Python-list
mailing list