I have coded an app with python and tkinter. I have an entry box and I want that when the user enters a data it matches with the regex. it must match interactively. That is to say when the user enters a value which does not correspond the value must be not blocked. That's why I use focusin in the code. What I don't understand is that my entry is invalid even when the value matches the not regex In addition, the entry allows you to enter any value and not block it
Here is a working example
import tkinter as tk
import re
class FocusOutValidationDemo() :
def __init__(self):
self.master = tk.Tk()
self.errormsg = tk.Label( text = '', fg = 'red' )
self.errormsg.pack()
tk.Label( text = 'Enter Email Address' ).pack()
vcmd = (self.master.register( self.validate_email ), '%P')
invcmd = (self.master.register( self.invalid_email ), '%P')
self.emailentry = tk.Entry( self.master, validate = "focusin",validatecommand = vcmd,invalidcommand = invcmd)
self.emailentry.pack()
tk.mainloop()
def validate_email(self, P):
self.errormsg.config( text = '' )
x = re.match( r"#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*(?:;#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*)*", P )
return (x != None)
def invalid_email(self, P):
self.errormsg.config( text = 'Invalid' )
self.emailentry.focus_set()
app = FocusOutValidationDemo()