By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

limiting text input in Tkinter Entry widget

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.