472,986 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Limitting the Contents of an Entry Widget in Tkinter

How do I limit what the user can enter in an Entry Widget? I know I can
set it to display '*' to hide a password, but what I want to do is
limit the contents to numeric characters. What is the easiest way of
doing this?

Jan 14 '06 #1
2 2818
Dustan wrote:
How do I limit what the user can enter in an Entry Widget? I know I can
set it to display '*' to hide a password, but what I want to do is
limit the contents to numeric characters. What is the easiest way of
doing this?


You can check the source of tkSimpleDialog.askfloat, which, in my
opinion, is pretty lazy. It checks after everything is entered.

Cooler is to intercept events and check them real-time. It gets
complicated but I find the resulting gui to be much more "user-friendly"
and intuitive.

The key events to catch for an entry are <Key> and <ButtonRelease-2>
(pasting). Other types of pasting (e.g. ctrl-v) may also be used and you
can code these to your preference. When you intercept these events, a
good way is to check and see what would happen to the entry by testing
the result of the key press without modifying the entry. Return "break"
if the result does not meet your criteria (float, int, even, odd, etc).
If it does meet your criteria, update the Entry and return "break".
Remember to process special keys, like "Delete" and "Tab". You can
modify their behavior to your taste. For "Tab" and "<Shift-Tab>", look at

awidget.tk_focusNext().focus_set()
awidget.tk_focusPrev().focus_set()

A list of keysyms is at
http://infohost.nmt.edu/tcc/help/pub...key-names.html

These can be accesed by the Event.keysym attribute. Keysyms seem to be
somewhat platform independent and seem to correspond to events pretty
well. For pasting, you will want to access the pasteboard(s) via

Event.widget.selection_get(selection='PRIMARY')
Event.widget.selection_get(selection='CLIPBOARD')

in that order, using try/except.

James
Jan 14 '06 #2
Am Sat, 14 Jan 2006 13:24:01 -0800 schrieb Dustan:
How do I limit what the user can enter in an Entry Widget? I know I can
set it to display '*' to hide a password, but what I want to do is
limit the contents to numeric characters. What is the easiest way of
doing this?


Hi!

Fredrik Lundh's ValidateEntry might be what you want.
http://tkinter.unpythonic.net/wiki/ValidateEntry

Regards, Markus

Jan 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mickel Grönroos | last post by:
Hi, I'm trying to put an Tkinter.Entry of fixed size onto a specific location on a canvas using the place manager. The idea is that one can double-click a rectangle object on a canvas to get an...
3
by: Phil Schmidt | last post by:
I'm trying to make a custom entry widget, as in the code that follows. There are two problems I'm trying to fix: 1) I would like the widget to behave as myEntry.Escape() does now, except that it...
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 ...
7
by: Michael Onfrek | last post by:
Hi! I'm playing with entry again and trying to restrict length of entry widget to certain number of character, so users cannot enter more character into it. Any ideas? Reg. Michael Onfrek
3
Elias Alhanatis
by: Elias Alhanatis | last post by:
Hello to everybody!! I am running Python 2.5.1 on Windows Vista and i have a problem with the "Entry" widget of Tkinter. Take a look at this code: from Tkinter import * def fetch(): ...
3
by: asit | last post by:
from Tkinter import * def callback(): print e.get() master=Tk() e=Entry(master) e.pack(anchor=CENTER)
2
Strider1066
by: Strider1066 | last post by:
Is it a bug or a feature? I'm trying to sub-class an Entry widget that displays a template (i.e. '____-__-__') for users and does keystroke validation. It is starting to work but with one problem....
2
by: lee.walczak | last post by:
Hi, I am using Tkinter & the Kevin Walzer's TableList Wrapper for python implemented GUI: http://tkinter.unpythonic.net/wiki/TableListWrapper The TableList has been extremely useful in...
0
by: Leonhard Vogt | last post by:
Hello I have the following problem in Python 2.5 on Windows XP. On Ubuntu I do not see the problem. I have a Tkinter application as in the following example The entry-widget is somehow...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.