@@ -188,6 +188,7 @@ def add_entry_or_combobox(
188
188
cb .bind ("<FocusOut>" , lambda event , path = path : self ._validate_combobox (event , path )) # type: ignore[misc]
189
189
cb .bind ("<KeyRelease>" , lambda event , path = path : self ._validate_combobox (event , path )) # type: ignore[misc]
190
190
cb .bind ("<Return>" , lambda event , path = path : self ._validate_combobox (event , path )) # type: ignore[misc]
191
+ cb .bind ("<ButtonRelease>" , lambda event , path = path : self ._validate_combobox (event , path )) # type: ignore[misc]
191
192
192
193
# Prevent mouse wheel from changing value when dropdown is not open
193
194
def handle_mousewheel (_event : tk .Event , widget : tk .Widget = cb ) -> Optional [str ]:
@@ -257,15 +258,27 @@ def _validate_combobox(self, event: tk.Event, path: ComponentPath) -> bool:
257
258
value = combobox .get () # Get the current value of the combobox
258
259
allowed_values = combobox .cget ("values" ) # Get the list of allowed values
259
260
261
+ # Events that should trigger data model update (when value is valid)
262
+ should_update_data_model = event .type in {
263
+ tk .EventType .FocusOut ,
264
+ tk .EventType .KeyPress , # Return key
265
+ tk .EventType .KeyRelease , # Key release events
266
+ tk .EventType .ButtonRelease , # Mouse click on dropdown item
267
+ }
268
+
269
+ if should_update_data_model and value in allowed_values :
270
+ self .data_model .set_component_value (path , value )
271
+
260
272
if value not in allowed_values :
261
- if (
262
- (event .type == "10" and getattr (combobox , "dropdown_is_open" , False )) # FocusOut events
263
- or event .type == "2" # Return KeyPress event
273
+ if ( # this is complicated because we only want to issue error messages in particular cases
274
+ (event .type == tk . EventType . FocusOut and getattr (combobox , "dropdown_is_open" , False )) # FocusOut events
275
+ or event .type == tk . EventType . KeyPress # KeyPress event (Return key)
264
276
):
265
277
paths_str = ">" .join (list (path ))
266
278
allowed_str = ", " .join (allowed_values )
267
279
error_msg = _ ("Invalid value '{value}' for {paths_str}\n Allowed values are: {allowed_str}" )
268
280
show_error_message (_ ("Error" ), error_msg .format (value = value , paths_str = paths_str , allowed_str = allowed_str ))
281
+
269
282
combobox .configure (style = "comb_input_invalid.TCombobox" )
270
283
return False
271
284
0 commit comments