diff --git a/MethodicConfigurator/frontend_tkinter_component_editor.py b/MethodicConfigurator/frontend_tkinter_component_editor.py index f12072d..5dd2207 100644 --- a/MethodicConfigurator/frontend_tkinter_component_editor.py +++ b/MethodicConfigurator/frontend_tkinter_component_editor.py @@ -282,7 +282,7 @@ def set_values_from_fc_parameters(self, fc_parameters: dict, doc: dict): self.set_gnss_type_and_protocol_from_fc_parameters(fc_parameters) self.set_serial_type_and_protocol_from_fc_parameters(fc_parameters, doc) - self.set_battery_from_fc_parameters(fc_parameters) + self.set_battery_type_and_protocol_from_fc_parameters(fc_parameters) self.set_motor_poles_from_fc_parameters(fc_parameters) def set_gnss_type_and_protocol_from_fc_parameters(self, fc_parameters: dict): @@ -372,7 +372,7 @@ def set_serial_type_and_protocol_from_fc_parameters(self, fc_parameters: dict, d self.data['Components']['ESC']['FC Connection']['Protocol'] = \ doc['MOT_PWM_TYPE']['values'][str(mot_pwm_type)] - def set_battery_from_fc_parameters(self, fc_parameters: dict): + def set_battery_type_and_protocol_from_fc_parameters(self, fc_parameters: dict): if "BATT_MONITOR" in fc_parameters: batt_monitor = int(fc_parameters["BATT_MONITOR"]) self.data['Components']['Battery Monitor']['FC Connection']['Type'] = \ @@ -389,11 +389,8 @@ def set_motor_poles_from_fc_parameters(self, fc_parameters: dict): elif "SERVO_FTW_MASK" in fc_parameters and fc_parameters["SERVO_FTW_MASK"] and "SERVO_FTW_POLES" in fc_parameters: self.data['Components']['Motors']['Specifications']['Poles'] = fc_parameters["SERVO_FTW_POLES"] - - def update_esc_protocol_combobox(self, esc_connection_type): - """ - Updates the ESC Protocol combobox based on the selected ESC Type. - """ + def update_esc_protocol_combobox_entries(self, esc_connection_type): + """ Updates the ESC Protocol combobox entries based on the selected ESC Type.""" if len(esc_connection_type) > 3 and esc_connection_type[:3] == 'CAN': protocols = ['DroneCAN'] elif len(esc_connection_type) > 6 and esc_connection_type[:6] == 'SERIAL': @@ -401,23 +398,12 @@ def update_esc_protocol_combobox(self, esc_connection_type): else: protocols = list(self.local_filesystem.doc_dict['MOT_PWM_TYPE']["values"].values()) - # Find the ESC Protocol combobox and update its values protocol_path = ('ESC', 'FC Connection', 'Protocol') if protocol_path in self.entry_widgets: protocol_combobox = self.entry_widgets[protocol_path] - protocol_combobox['values'] = protocols # Update the values - protocol_combobox.set(protocols[0] if protocols else '') # Set the first protocol as the default selection or clear if none - - def validate_and_update_esc_protocols(self, event, path): - """ - Validates the value and updates the ESC Protocol combobox based on the selected ESC Type. - """ - # Perform validation - valid = self.validate_combobox(event, path) - if valid: - # Update ESC Protocol combobox based on the selected type - self.update_esc_protocol_combobox(event.widget.get()) - return valid + protocol_combobox['values'] = protocols # Update the combobox entries + protocol_combobox.set(protocols[0] if protocols else '') + protocol_combobox.update_idletasks() # re-draw the combobox ASAP def add_entry_or_combobox(self, value, entry_frame, path): # Default values for comboboxes in case the apm.pdef.xml metadata is not available @@ -481,12 +467,12 @@ def get_combobox_values(param_name: str) -> list: config = combobox_config.get(path) if config: cb = ttk.Combobox(entry_frame, values=config["values"]) - if path == ('ESC', 'FC Connection', 'Type'): - cb.bind("", lambda event, path=path: self.validate_and_update_esc_protocols(event, path)) - cb.bind("", lambda event, path=path: self.validate_and_update_esc_protocols(event, path)) - else: - cb.bind("", lambda event, path=path: self.validate_combobox(event, path)) - cb.bind("", lambda event, path=path: self.validate_combobox(event, path)) + cb.bind("", lambda event, path=path: self.validate_combobox(event, path)) + cb.bind("", lambda event, path=path: self.validate_combobox(event, path)) + + if path == ('ESC', 'FC Connection', 'Type'): # immediate update of ESC Protocol upon ESC Type selection + cb.bind("<>", lambda event, path=path: self.update_esc_protocol_combobox_entries(cb.get())) + cb.set(value) return cb @@ -544,6 +530,10 @@ def validate_combobox(self, event, path) -> bool: f"Allowed values are: {', '.join(allowed_values)}") combobox.configure(style="comb_input_invalid.TCombobox") return False + + if path == ('ESC', 'FC Connection', 'Type'): + self.update_esc_protocol_combobox_entries(value) + combobox.configure(style="comb_input_valid.TCombobox") return True