limiting text input in Tkinter Entry widget

  • Thread starter =?ISO-8859-1?Q?Otto_Kr=FCse?=
  • Start date
?

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

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
 
J

John Roth

Otto Krüse said:
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
 
C

Cameron Laird

.
.
.

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

Fredrik Lundh

Otto said:
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>
 
C

Cameron Laird

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

Peter Otten

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top