Error in 2.2.1 Tkinter? (was: limiting text input in Tkinter Entry widget)

Peter Otten __peter__ at web.de
Mon Feb 23 08:37:49 CET 2004


Cameron Laird wrote:

[Doesn't work:]

>>  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)

[Works:]

>   def validate(name, index, mode):
>     value = my_variable.get()
>     my_variable.set(value[0:4])

A look into Tkinter.py reveals that Variable.set()/get() is implemented in
terms of tkapp.globalsetvar()/globalgetvar(). Translating it into your
example:

def validate(name, index, mode):
    value = root.tk.globalgetvar(name)
    root.tk.globalsetvar(name, value[0:4])

So the problem seems to relate to different Tcl namespaces. I didn't dig any
deeper.

Peter

PS: I used Python 2.3.3



More information about the Python-list mailing list