[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>&nbsp;</DIV>
<DIV>&nbsp; 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&nbsp; 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&nbsp; having 3 routines (one for string, one for int,one for float) is enough?". </DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; My sample program is given below:</DIV>
<DIV>*********************************************</DIV>
<DIV>class SampleDialog(tkSimpleDialog.Dialog):<BR>&nbsp;&nbsp;&nbsp; def __init__( self, parent, title, name) :<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name = name[:]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tkSimpleDialog.Dialog.__init__( self, parent, title)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def body(self,master):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.SampleEntry0=Entry(master)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.SampleEntry0.insert(0,self.name[0])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.SampleEntry0.pack()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.SampleEntry0.bind('&lt;KeyRelease&gt;',self.validate_check)</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.SampleEntry1=Entry(master)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.SampleEntry1.insert(0,self.name[1]) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.SampleEntry1.pack()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; def apply(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name[0]=self.SampleEntry0.get()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.name[1]=self.SampleEntry1.get()<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; def validate_check(self,event):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chk=self.SampleEntry0.get()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if chk: <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chkval = string.atoi(chk)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "No Error",chk <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except ValueError:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tkMessageBox.showwarning("Bad input", Illegal values")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0 </DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp; 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&nbsp; 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>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; 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&nbsp; "lambda". But instead of the value i got entry widgets instance number. The code is </DIV>
<DIV>given below.</DIV>
<DIV>&nbsp;</DIV>
<DIV>self.SampleEntry0.bind('&lt;KeyRelease&gt;',<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lambda<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arg1=self.SampleEntry0.get():<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.validate_check(arg1))</DIV>
<DIV>&nbsp;</DIV>
<DIV>#*********&nbsp; Event handler</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; def validate_check(self,arg1):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print arg1</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;So, how can i overcome above problems?&nbsp; 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--