entry widget won't validate

Peter Otten __peter__ at web.de
Mon Sep 21 13:47:55 EDT 2009


Mike wrote:

> I'm trying to arrange for an Entry widget to check whether its data
> is all digits and whether the number represented is small enough.
> The validate function seem to be called once at startup and not
> afterwards:
> 
> import sys, Tkinter, tkFileDialog, tkMessageBox
> 
> tk=Tkinter
> tkfd=tkFileDialog
> 
> ...
> class Functor :
>     def __init__(self, func, data) :
>         self.func=func
>         self.data=data
>     def __call__(self) : return self.func(self.data)
> 
> ...
> class Levels(tk.Frame) :
>     def __init__(self, parent) :
>         tk.Frame.__init__(self, parent)
>         self.tv=[]
>         for rgb in range(3) :
>             self.tv.append(tk.StringVar())
>             e=tk.Entry(self, textvariable=self.tv[rgb], width=5,
>                        validate='all',
>                        validatecommand=Functor(self.vc, self.tv
> [rgb]) )
>             self.tv[rgb].set(str(levels_av_max))
>             e.grid(row=2, column=rgb)
>             lab=tk.Label(self, text='RGB'[rgb])
>             lab.grid(row=1, column=rgb)
>         doit=tk.Button(self, text='set levels', command=self.pushed)
>         doit.grid(row=0, column=0, columnspan=3)
> 
>     def vc(self, arg2=None) :
>         print 'vc:', arg2.get()
> 
> The print statement run 3 times at startup.
> Editing an Entry does not cause any printing.
> Any ideas?

Quoting http://www.tcl.tk/man/tcl8.5/TkCmd/entry.htm#M12:

"""
In general, the textVariable and validateCommand can be dangerous to mix. 
Any problems have been overcome so that using the validateCommand will not 
interfere with the traditional behavior of the entry widget. Using the 
textVariable for read-only purposes will never cause problems. The danger 
comes when you try set the textVariable to something that the 
validateCommand would not accept, which causes validate to become none (the 
invalidCommand will not be triggered). The same happens when an error occurs 
evaluating the validateCommand.
"""

You can verify that this is indeed your problem by changing the Levels.vc() 
method to always return True for the moment.

Peter




More information about the Python-list mailing list