limiting text input in Tkinter Entry widget

John Roth newsgroups at jhrothjr.com
Sun Feb 22 12:01:22 EST 2004


"Otto Krüse" <otto_kruse at hotmail.com> wrote in message
news:4038d6c2$0$49821$1b2cd167 at news.wanadoo.nl...
> 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





More information about the Python-list mailing list