FAQ
hi, I am using python and Tkinter in my application. My problem is as follows. I am using "tkSimpleDiallog" widget to display some values. I have placed 10 entry widgets in the dialog box. Among 10 entry widgets some widgets must have only string values and some others must hold intergers and others hold floats. I expect validity checks for "KeyRelease" event for all entry eidgets. Now the problem is "do i need to have 10 validity check routines one for each entry widget? or having 3 routines (one for string, one for int,one for float) is enough?". My sample program is given below:*******************************************class SampleDialog(tkSimpleDialog.Dialog):
def __init__( self, parent, title, name) :
self.name = name[:]
tkSimpleDialog.Dialog.__init__( self, parent, title)
def body(self,master):
self.SampleEntry0=Entry(master)
self.SampleEntry0.insert(0,self.name[0])
self.SampleEntry0.pack()
self.SampleEntry0.bind('<KeyRelease>',self.validate_check) self.SampleEntry1=Entry(master)
self.SampleEntry1.insert(0,self.name[1])
self.SampleEntry1.pack()

def apply(self):
self.name[0]=self.SampleEntry0.get()
self.name[1]=self.SampleEntry1.get()

def validate_check(self,event):
try:
chk=self.SampleEntry0.get()
if chk:
chkval = string.atoi(chk)
print "No Error",chk
return 1
except ValueError:
tkMessageBox.showwarning("Bad input", Illegal values")
return 0 Here i showed you only 2 entry widgets. First entry widget "SampleEntry0" holds float and second one "SampleEntry1" holds string. The routine "validate_check" checks whether the 1st entry is float or not for evry keyrelease. Suppose if i have another entry widget say "SampleEntry2" which also holds a float, how can i change the above "validate_check" routine so that it will be used for all entry widgets that hold floats? Or do i have to repeat the same routine by only changing the line "chk = self.SampleEntry0.get() ? It is not easy to manage when number of widgets is increased. Moreover is it possible to send the value that a entry widget has, to an event handler using "event binding"? I tried the same with "lambda". But instead of the value i got entry widgets instance number. The code is given below. self.SampleEntry0.bind('<KeyRelease>',
lambda
arg1=self.SampleEntry0.get():
self.validate_check(arg1)) #********* Event handler def validate_check(self,arg1):
print arg1 So, how can i overcome above problems? Expecting your valuable comments. Thanx.


---------------------------------
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20030522/3b52ce00/attachment.html

Search Discussions

  • Python-lover at May 22, 2003 at 8:03 am
    hi, I am using python and Tkinter in my application. My problem is as follows. I am using "tkSimpleDiallog" widget to display some values. I have placed 10 entry widgets in the dialog box. Among 10 entry widgets some widgets must have only string values and some others must hold intergers and others hold floats. I expect validity checks for "KeyRelease" event for all entry eidgets. Now the problem is "do i need to have 10 validity check routines one for each entry widget? or having 3 routines (one for string, one for int,one for float) is enough?". My sample program is given below:*********************************************class SampleDialog(tkSimpleDialog.Dialog):
    def __init__( self, parent, title, name) :
    self.name = name[:]
    tkSimpleDialog.Dialog.__init__( self, parent, title)
    def body(self,master):
    self.SampleEntry0=Entry(master)
    self.SampleEntry0.insert(0,self.name[0])
    self.SampleEntry0.pack()
    self.SampleEntry0.bind('<KeyRelease>',self.validate_check) self.SampleEntry1=Entry(master)
    self.SampleEntry1.insert(0,self.name[1])
    self.SampleEntry1.pack()

    def apply(self):
    self.name[0]=self.SampleEntry0.get()
    self.name[1]=self.SampleEntry1.get()

    def validate_check(self,event):
    try:
    chk=self.SampleEntry0.get()
    if chk:
    chkval = string.atoi(chk)
    print "No Error",chk
    return 1
    except ValueError:
    tkMessageBox.showwarning("Bad input", Illegal values")
    return 0 Here i showed you only 2 entry widgets. First entry widget "SampleEntry0" holds float and second one "SampleEntry1" holds string. The routine "validate_check" checks whether the 1st entry is float or not for evry keyrelease. Suppose if i have another entry widget say "SampleEntry2" which also holds a float, how can i change the above "validate_check" routine so that it will be used for all entry widgets that hold floats? Or do i have to repeat the same routine by only changing the line "chk = self.SampleEntry0.get() ? It is not easy to manage when number of widgets is increased. Moreover is it possible to send the value that a entry widget has, to an event handler using "event binding"? I tried the same with "lambda". But instead of the value i got entry widgets instance number. The code is given below. self.SampleEntry0.bind('<KeyRelease>',
    lambda
    arg1=self.SampleEntry0.get():
    self.validate_check(arg1)) #********* Event handler def validate_check(self,arg1):
    print arg1 So, how can i overcome above problems? Expecting your valuable comments.Thanx.


    ---------------------------------
    Do you Yahoo!?
    The New Yahoo! Search - Faster. Easier. Bingo.
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/python-list/attachments/20030522/f27a6cec/attachment.htm
  • Alan Gauld at May 22, 2003 at 9:13 am

    I expect validity checks for "KeyRelease" event for all entry
    widgets. Now the problem is "do i need to have 10 validity check
    routines one for each entry widget? or having 3 routines
    (one for string, one for int,one for float) is enough?".

    3 should suffice. You can call the routines from your entry
    handlers.

    <PSEUDOCODE ALERT!>

    def isString(s): return type(s) == type('')
    def isInteger(i): return type(i) == type(7)
    def isFloat(f): return type(f) == type(7.0)

    Entries = {}
    for e in range(10):
    Entries[e] = Entry(.....)
    if e % 3 == 0:
    bind(<KeyRelease>, Entries[e], lambda e: isString(e.value))
    elif e % 2 == 0:
    bind(<KeyRelease>, Entries[e], lambda e: isInteger.value())
    else:
    bind(<KeyRelease>, Entries[e], lambda e: isFloat(e.value())

    Creates 10 entries with some checking integers, some strings etc.
    You probably want to control which entry does what rather more
    methodically than that but you get the idea...

    Your type checking might want to be a tad more sophisticated too!

    Alan G
    Author of the Learn to Program web tutor
    http://www.freenetpages.co.uk/hp/alan.gauld

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 22, '03 at 7:29a
activeMay 22, '03 at 9:13a
posts3
users2
websitepython.org

2 users in discussion

Python-lover: 2 posts Alan Gauld: 1 post

People

Translate

site design / logo © 2022 Grokbase