data entry validation with Tkinter
Joseph A. Knapka
jknapka at earthlink.net
Tue Aug 7 19:42:58 EDT 2001
Ken Guest wrote:
>
> hi,
> I'm trying to put together a python + tkinter app with data entry
> validation.
> What I have in mind is that the colour of the text changes depending
> on what the user has typed. (In otherwords the text is displayed in
> red if it is invalid, and in black if it is ok).
> I'd like to use regex (or the more up-to-date re strings) to specify
> what type of text is valid (cf my last query to the help mailing list).
>
> At the moment, basic code that I have gotten to work looks like:
>
> self.__entry = Entry(newframe, validate='key', vcmd='string is int %P',
> invcmd='bell')
>
> attempts at specifying python procedures for vcmd and invcmd only seem
> to be called when the app in initialising and don't exactly work.
> For example, with
>
> self.__entry = Entry(newframe, validate='key', vcmd=self.validate('%P'),
> invcmd=self.invalid())
This is because self.validate('%P') is a method call, not a
bound method object. Similarly self.invalid() - you're calling
the method and sending its result into the Entry() call.
I have a similar problem - I need to send data known at
control-creation time to the callback when the control is
clicked. So I use a callable object and attach the data to
it:
class CallbackCmd:
def __init__(self,boundmethod,data):
self.boundmethod = boundmethod
self.data = data
def __call__(self,*args):
apply(boundmethod,(self.data,)+*args)
Then:
class SomeGUI:
def theCallback(self,data,*args):
doSomethingUseful(data)
def __init__(self,someCallbackData):
btn = Tkinter.Button(command=CallbackCmd(
self.theCallback,
someCallbackData)
)
and then when the btn is clicked, SomeGUI.theCallback() is
invoked with someCallbackData as the first argument, and
the rest of the Tk callback arguments following.
I wonder if there is a more Pythonian way to accomplish this?
HTH,
-- Joe
> the validate and invalid methods are called when the app starts up, but
> neither of those methods are called when any keys are pressed, and I'm
> really not sure that the second definition above is syntatically
> correct, even though the parser didn't catch any problems.
>
> What I'd like to know is if what I am looking to do is in anyway
> possible, and if anybody has any pointers/ideas on how I could
> implement this functionality.
> Am I left to using TCL expressions for data entry validation (and if so,
> how do I do the above)
>
> (I have done a number of searches on Google to see if anybody else
> may have run into these questions before but couldn't find anything
> of interest)
>
> thanks,
>
> Ken
-- Joe Knapka
"You know how many remote castles there are along the gorges? You
can't MOVE for remote castles!" -- Lu Tze re. Uberwald
// Linux MM Documentation in progress:
// http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html
2nd Lbl A + 1 = 2nd Pause 2nd Prt GTO 2 R/S
More information about the Python-list
mailing list