[Tkinter]Validation to multiple Entry widgets

Beppe giuseppecostanzi at gmail.com
Sat Feb 17 13:32:12 EST 2018


hi all,

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

#!/usr/bin/python3
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)


        self.vcmd = (self.register(self.validate),
                '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
        
        self.pack()
        self.init_ui()
        
    def init_ui(self):
       
        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
        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