[Tkinter]Validation to multiple Entry widgets

Peter Otten __peter__ at web.de
Sat Feb 17 14:20:41 EST 2018


Beppe wrote:

> I would validate values input, on key, in multiple Entry widgets create at
> run time
> 
> I wrote this snip but even if the entry are created and the callback work
> well the relative value is missing
        
>         my_list = (2.14,18.3,76.4,2.38,0.425,2.68,1.09,382,8.59,0.495)
> 
>         for i in my_list:
>             #this work
>             #w = tk.Entry(self,  bg='white')
>             #this doesn't work
>             w = tk.Entry(self,  bg='white',  validate = 'key',
>             validatecommand = self.vcmd) w.insert(tk.END, i)
>             w.pack()
> 
>     def validate(self, action, index, value_if_allowed,
>                  prior_value, text, validation_type,
>                  trigger_type, widget_name):
>         # action=1 -> insert

When you add

          print(text)

here to debug your code the old-fashioned way you'll note that text is, e. 
g. '2.14' and thus will not pass the `text in ...` test below.

>         if(action=='1'):
>             if text in '0123456789.-+':
>                 try:
>                     float(value_if_allowed)
>                     return True
>                 except ValueError:
>                     return False
>             else:
>                 return False
>         else:
>             return True
> 
>    
> root = tk.Tk()
> app = Application(master=root)
> app.mainloop()
> 
> 
> tips?
> 
> regards beppe





More information about the Python-list mailing list