@@ -35,6 +35,7 @@ def __init__(
3535 required : bool = True ,
3636 custom_validator : Optional [Callable [[Any ], tuple [bool , str ]]] = None ,
3737 on_validate : Optional [Callable [[bool ], None ]] = None ,
38+ validate_on_change : bool = True ,
3839 ** kwargs
3940 ) -> None :
4041 """
@@ -56,6 +57,10 @@ def __init__(
5657 required: Whether the field is required
5758 custom_validator: Custom validation function that returns (is_valid, error_message)
5859 on_validate: Callback function called after validation with validation result
60+ validate_on_change: Validate as the user types, so a stale success mark is removed straight away
61+ and `on_validate` follows the value. Errors are not shown while typing, only on focus out
62+ and Return. If False, validation runs only on focus out and Return.
63+ An empty optional entry shows neither mark.
5964 """
6065 super ().__init__ (parent , ** kwargs )
6166
@@ -76,6 +81,7 @@ def __init__(
7681 self .required = required
7782 self .custom_validator = custom_validator
7883 self .on_validate = on_validate
84+ self .validate_on_change = validate_on_change
7985 self .default_value = default_value
8086
8187 # Store the external variable (any tkinter var type)
@@ -139,7 +145,12 @@ def __init__(
139145 self .variable .trace_add ("write" , self ._on_value_change )
140146
141147 def _on_value_change (self , * _ ) -> None :
142- """Fire the on_change callback when the entry value changes."""
148+ """Validate (if enabled), then fire the on_change callback when the entry value changes."""
149+ if self .validate_on_change :
150+ is_valid = self .validate ()[0 ]
151+ if not is_valid :
152+ # Keep the error for focus out / Return: the value may just be partly typed.
153+ self .clear_error ()
143154 self ._fire_on_change (self .get ())
144155
145156 def _sync_to_external (self , * _ ) -> None :
@@ -221,7 +232,7 @@ def validate(self) -> tuple[bool, Optional[str], Optional[Literal['info', 'warni
221232
222233 # If not required and empty, it's valid
223234 if not self .required and not value_str :
224- self ._show_success ()
235+ self .clear_error ()
225236 self ._call_validate_callback (True )
226237 base_result = (True , None , None )
227238 return self .apply_validation (base_result )
0 commit comments