Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10235 > unrolled thread

Validating Entry in tkinter

Started bySaul Spatz <saul.spatz@gmail.com>
First post2011-07-24 17:11 -0700
Last post2011-07-27 11:24 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Validating Entry in tkinter Saul Spatz <saul.spatz@gmail.com> - 2011-07-24 17:11 -0700
    Re: Validating Entry in tkinter rantingrick <rantingrick@gmail.com> - 2011-07-24 18:03 -0700
    Re: Validating Entry in tkinter Peter Otten <__peter__@web.de> - 2011-07-25 09:32 +0200
    Re: Validating Entry in tkinter Wolfgang Meiners <WolfgangMeiners01@web.de> - 2011-07-25 10:48 +0200
    Re: Validating Entry in tkinter Giacomo Boffi <giacomo.boffi@polimi.it> - 2011-07-27 11:24 +0200

#10235 — Validating Entry in tkinter

FromSaul Spatz <saul.spatz@gmail.com>
Date2011-07-24 17:11 -0700
SubjectValidating Entry in tkinter
Message-ID<0417c151-9cfa-42b7-8476-17c732f91396@glegroupsg2000goo.googlegroups.com>
In tcl/tk an Entry widget can be set to validate its contents with the validate option.  You have to give it a validatecommand (vcmd), which is a tcl script that runs when some action triggers validation.  Usually, the script would use "percent substitutions" so the script would be something like {ValidInt %P} where %P is the value of the widget should the proposed change occur.  

Can one do something like this in tkinter?  I've verified that with

e = Entry(master, validate = 'all', vcmd = validInt) 

the validInt function is called, but it isn't passed any parameters. I can't find that e has any attribute corresponding to the %P value above.

Is it not possible to do this in tkinter, or have I overlooked something? 

[toc] | [next] | [standalone]


#10236

Fromrantingrick <rantingrick@gmail.com>
Date2011-07-24 18:03 -0700
Message-ID<17425de2-9b5e-4b1d-a4a4-b20ce726c91e@a11g2000yqm.googlegroups.com>
In reply to#10235
On Jul 24, 7:11 pm, Saul Spatz <saul.sp...@gmail.com> wrote:
>
> Can one do something like this in tkinter?
‡

(1) First of all what exactly do you wish return?
 * an integer
 * a float
 * something else?

(2) Is this input part of a modal or non-modal interface?

For me, input validation should happen in *real* time and NOT after
the fact. For instance; if you have two entrys forms and a button in
your GUI like below...

 +--------------------------+
 | Tk                 | X | |
 +--------------------------+
 |                          |
 |  Value1: [          ]    |
 |  Value2: [          ]    |
 |                          |
 |       [Calculate]        |
 '--------------------------'

... and your "validator" only checks the contents of "Value1" and
"Value2" AFTER the user presses the "Calculate" button then
congratulations because you have just created an unintuitive GUI
design. Instead, your entry widgets should have been filtering the key-
presses so that it would be impossible to enter anything but an
integer or float or whatever you want. Running a validater after the
fact is an anti pattern.

It is not very difficult to subclass a tk.Entry widget and create some
validation/filtering logic. However it would be helpful if you could
be a wee bit more specific about your validation needs.

[toc] | [prev] | [next] | [standalone]


#10245

FromPeter Otten <__peter__@web.de>
Date2011-07-25 09:32 +0200
Message-ID<mailman.1446.1311579180.1164.python-list@python.org>
In reply to#10235
Saul Spatz wrote:

> In tcl/tk an Entry widget can be set to validate its contents with the
> validate option.  You have to give it a validatecommand (vcmd), which is a
> tcl script that runs when some action triggers validation.  Usually, the
> script would use "percent substitutions" so the script would be something
> like {ValidInt %P} where %P is the value of the widget should the proposed
> change occur.
> 
> Can one do something like this in tkinter?  I've verified that with
> 
> e = Entry(master, validate = 'all', vcmd = validInt)
> 
> the validInt function is called, but it isn't passed any parameters. I
> can't find that e has any attribute corresponding to the %P value above.
> 
> Is it not possible to do this in tkinter, or have I overlooked something?

Some time ago I came up with 

http://mail.python.org/pipermail/python-list/2009-September/1220447.html

import Tkinter as tk

def validate(before, after):
    print before, "-->", after
    return after.isdigit()

if __name__ == "__main__":
    root = tk.Tk()
    name = root.register(validate)
    cmd = 'expr {[%(name)s %(parms)s]}' % dict(name=name, parms="%s %P")
    var = tk.StringVar()
    entry = tk.Entry(root, textvariable=var,
                     validate="all", validatecommand=cmd)
    entry.pack()
    entry.focus_set()
    root.mainloop()

[toc] | [prev] | [next] | [standalone]


#10250

FromWolfgang Meiners <WolfgangMeiners01@web.de>
Date2011-07-25 10:48 +0200
Message-ID<4e2d2dcd$0$7620$9b4e6d93@newsspool1.arcor-online.net>
In reply to#10235
Am 25.07.11 02:11, schrieb Saul Spatz:
> In tcl/tk an Entry widget can be set to validate its contents with the validate option.  You have to give it a validatecommand (vcmd), which is a tcl script that runs when some action triggers validation.  Usually, the script would use "percent substitutions" so the script would be something like {ValidInt %P} where %P is the value of the widget should the proposed change occur.  
> 
> Can one do something like this in tkinter?  I've verified that with
> 
> e = Entry(master, validate = 'all', vcmd = validInt) 
> 
> the validInt function is called, but it isn't passed any parameters. I can't find that e has any attribute corresponding to the %P value above.
> 
> Is it not possible to do this in tkinter, or have I overlooked something? 

Hi,
i think you can find the answer using the following link:
http://stackoverflow.com/questions/4140437/python-tkinter-interactively-validating-entry-widget-content

Wolfgang

[toc] | [prev] | [next] | [standalone]


#10378

FromGiacomo Boffi <giacomo.boffi@polimi.it>
Date2011-07-27 11:24 +0200
Message-ID<86wrf4oz40.fsf@aiuole.stru.polimi.it>
In reply to#10235
Saul Spatz <saul.spatz@gmail.com> writes:

> In tcl/tk an Entry widget can be set to validate its contents with
> the validate option. [...]  Can one do something like this in
> tkinter?

i read the thread and nobody mentioned the python mega widget (Pmw)
toolkit, from whose docs i quote the following

Pmw.EntryField() - entry widget with validation

    [...]

    Validation is performed early, at each keystroke or other event
    which modifies the text. However, if partially valid text is
    permitted, the validity of the entered text can be checked just
    before it is to be used, which is a form of late validation.

    [...]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web