limiting text input in Tkinter Entry widget

Discussion in 'Python' started by =?ISO-8859-1?Q?Otto_Kr=FCse?=, Feb 22, 2004.

  1. Hi everyone,

    I'm building a GUI in which I want, amongst other things, for people to
    fill in there postal code. The postal codes of my country (Holland) are
    in this format: 1234 AB

    So for the input I use two entry widgets, one of a length of
    (characters) for the numbers and one of lenght 2 for the letters. What I
    don't like is that although the visible part of the widgets thus are 4
    and 2 characters, users can actually input more characters. They could
    for example input 12345 abcd. I want to make that impossible.

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

    Thanks a lot,
    Otto
     
    =?ISO-8859-1?Q?Otto_Kr=FCse?=, Feb 22, 2004
    #1
    1. Advertisements

  2. =?ISO-8859-1?Q?Otto_Kr=FCse?=

    John Roth Guest

    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.

    John Roth
     
    John Roth, Feb 22, 2004
    #2
    1. Advertisements

  3. .
    .
    .
    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, Feb 22, 2004
    #3
  4. http://effbot.org/zone/tkinter-entry-validate.htm

    </F>
     
    Fredrik Lundh, Feb 22, 2004
    #4
  5. .
    .
    .
    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, Feb 22, 2004
    #5
  6. =?ISO-8859-1?Q?Otto_Kr=FCse?=

    Peter Otten Guest

    Cameron Laird wrote:

    [Doesn't work:]
    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
     
    Peter Otten, Feb 23, 2004
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.