[Tkinter]Validation to multiple Entry widgets
Beppe
giuseppecostanzi at gmail.com
Sat Feb 17 15:14:38 EST 2018
Il giorno sabato 17 febbraio 2018 20:21:53 UTC+1, Peter Otten ha scritto:
> 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.
yeah, thank you very much, I change
if text in '0123456789.-+':
with
if text:
and it works now
thank you, Peter
>
> > 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