[Tutor] Validity check for different data types.......................
Python-lover
hemanexp@yahoo.com
Thu May 22 04:06:13 2003
--0-1572837701-1053590637=:47363
Content-Type: text/plain; charset=us-ascii
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.
--0-1572837701-1053590637=:47363
Content-Type: text/html; charset=us-ascii
<DIV>hi,</DIV>
<DIV> </DIV>
<DIV> 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?". </DIV>
<DIV> </DIV>
<DIV> My sample program is given below:</DIV>
<DIV>*********************************************</DIV>
<DIV>class SampleDialog(tkSimpleDialog.Dialog):<BR> def __init__( self, parent, title, name) :<BR> self.name = name[:]<BR> tkSimpleDialog.Dialog.__init__( self, parent, title)<BR> def body(self,master):<BR> self.SampleEntry0=Entry(master)<BR> self.SampleEntry0.insert(0,self.name[0])<BR> self.SampleEntry0.pack()<BR> self.SampleEntry0.bind('<KeyRelease>',self.validate_check)</DIV>
<DIV> self.SampleEntry1=Entry(master)<BR> self.SampleEntry1.insert(0,self.name[1]) <BR> self.SampleEntry1.pack()<BR> <BR> def apply(self):<BR> self.name[0]=self.SampleEntry0.get() <BR> self.name[1]=self.SampleEntry1.get()<BR> <BR> def validate_check(self,event):<BR> try:<BR> chk=self.SampleEntry0.get()<BR> if chk: <BR> chkval = string.atoi(chk)<BR> print "No Error",chk <BR> return 1<BR> except ValueError:<BR> tkMessageBox.showwarning("Bad input", Illegal values")<BR> return 0 </DIV>
<DIV> </DIV>
<DIV> 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. </DIV>
<DIV> </DIV>
<DIV> 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 </DIV>
<DIV>given below.</DIV>
<DIV> </DIV>
<DIV>self.SampleEntry0.bind('<KeyRelease>',<BR> lambda<BR> arg1=self.SampleEntry0.get():<BR> self.validate_check(arg1))</DIV>
<DIV> </DIV>
<DIV>#********* Event handler</DIV>
<DIV> </DIV>
<DIV> def validate_check(self,arg1):<BR> print arg1</DIV>
<DIV> </DIV>
<DIV> So, how can i overcome above problems? Expecting your valuable comments.</DIV>
<DIV>Thanx.<BR></DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://us.rd.yahoo.com/search/mailsig/*http://search.yahoo.com">The New Yahoo! Search</a> - Faster. Easier. Bingo.
--0-1572837701-1053590637=:47363--