472,354 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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
5 10166

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: snx7s | last post by:
Hi, using Tkinter, on a Text widget i would like to place the cursor to a given position like the icursor() method of the Entry widget. Is it possible do that, if it is how please ? Thank You.
3
by: bigbinc | last post by:
I have used the 'entry' tk widget to get text values, I am now using 'Text' but I cant seem to use 'get' method. The TK docs say use get(index1, index2), I tried numbers and get an error ...
2
by: Tonino | last post by:
Hi, I have a small Tkinter app that gets data from a socket connection to a "server". The app has a Text() widget to display the info that it gets from the socket connection. I have the...
7
by: William Gill | last post by:
Is there a simple way to cut and paste from a tkinter text widget to an entry widget? I know I could create a mouse button event that triggers a popup (message widget) prompting for cut/paste in...
2
by: rahulnag22 | last post by:
Hi, I guess this is a very trivial question -- I am using a label widget to display text (black font in a white background color). I am trying to use my mouse to scroll over the displayed text to...
4
by: BartlebyScrivener | last post by:
Using Python on Debian Etch. What is the best way to paste a block of text in at the command prompt. I'm trying something like: Quote = raw_input("Paste quote here: ") Which works great...
8
by: Lie | last post by:
Inspect the following code: --- start of code --- import Tkinter as Tk from Tkconstants import * root = Tk.Tk() e1 = Tk.Entry(root, text = 'Hello World') e2 = Tk.Entry(root, text = 'Hello...
1
by: C Martin | last post by:
What am I doing wrong in this code? The callback doesn't work from the Entry widget. ##start code import Tkinter tk = Tkinter.Tk() var = Tkinter.StringVar() print var._name def cb(name,...
0
by: John McMoangle | last post by:
Hey guys. I am having trouble understanding the get() method from The arguments to the get method of the Text widget are strings in the form of "line.position". So, text.get('1.5', '2.7')...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.