467,920 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

limiting text input in Tkinter Entry widget

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
Jul 18 '05 #1
  • viewed: 8099
Share:
5 Replies

"Otto Krüse" <ot********@hotmail.com> wrote in message
news:40***********************@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

Jul 18 '05 #2
In article <10*************@news.supernews.com>,
John Roth <ne********@jhrothjr.com> wrote:

"Otto Krüse" <ot********@hotmail.com> wrote in message
news:40***********************@news.wanadoo.nl. ..

Jul 18 '05 #3
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>


Jul 18 '05 #4
In article <10*************@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?

Jul 18 '05 #5
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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by snx7s | last post: by
3 posts views Thread by bigbinc | last post: by
2 posts views Thread by Tonino | last post: by
7 posts views Thread by William Gill | last post: by
2 posts views Thread by rahulnag22 | last post: by
4 posts views Thread by BartlebyScrivener | last post: by
8 posts views Thread by Lie | last post: by
1 post views Thread by C Martin | last post: by
reply views Thread by John McMoangle | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.