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. Advertising

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

    John Roth Guest

    "Otto Krüse" <> wrote in message
    news:4038d6c2$0$49821$...
    > 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")


    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

    >
    > Thanks a lot,
    > Otto
     
    John Roth, Feb 22, 2004
    #2
    1. Advertising

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

    In article <>,
    John Roth <> wrote:
    >
    >"Otto Krüse" <> wrote in message
    >news:4038d6c2$0$49821$...

    .
    .
    .
    >> 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 <>
    Business: http://www.Phaseit.net
     
    Cameron Laird, Feb 22, 2004
    #3
  4. Otto Krüse wrote:

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


    http://effbot.org/zone/tkinter-entry-validate.htm

    </F>
     
    Fredrik Lundh, Feb 22, 2004
    #4
  5. Re: Error in 2.2.1 Tkinter? (was: limiting text input in Tkinter Entry widget)

    In article <>, 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 <>
    Business: http://www.Phaseit.net
     
    Cameron Laird, Feb 22, 2004
    #5
  6. =?ISO-8859-1?Q?Otto_Kr=FCse?=

    Peter Otten Guest

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

    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
     
    Peter Otten, Feb 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Schmidt
    Replies:
    3
    Views:
    567
    Michael Peuser
    Sep 6, 2003
  2. Rod Stephenson
    Replies:
    2
    Views:
    391
    Eric Brunel
    Mar 5, 2004
  3. Frederic Rentsch
    Replies:
    0
    Views:
    251
    Frederic Rentsch
    Jul 8, 2012
  4. Terry Reedy
    Replies:
    15
    Views:
    640
    Frederic Rentsch
    Jul 16, 2012
  5. Frederic Rentsch
    Replies:
    0
    Views:
    433
    Frederic Rentsch
    Jul 9, 2012
Loading...

Share This Page