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

Cameron Laird claird at lairds.com
Mon Feb 23 00:28:41 CET 2004


In article <103i0ij3hnd4n76 at corp.supernews.com>, I wondered:
>  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?
			.
			.
			.
I should mention that
  import Tkinter

  root = Tkinter.Tk()

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

  my_variable = Tkinter.StringVar()
  my_variable.trace('w', validate)

  my_entry = Tkinter.Entry(width = 4, textvariable = my_variable)
  my_entry.pack()

  Tkinter.mainloop
*does* model the desired behavior, and is easy enough to "objectify".
I still don't understand getvar() behavior, though.
-- 

Cameron Laird <claird at phaseit.net>
Business:  http://www.Phaseit.net



More information about the Python-list mailing list