From 0c4c1e27eac7ed705fcb3373997bce42fea870ab Mon Sep 17 00:00:00 2001 From: VALLONGOL Date: Mon, 22 Dec 2025 12:26:11 +0100 Subject: [PATCH] sistemata visualizzazione dei campi ed invio comandi al server. --- pybusmonitor1553/gui/monitor.py | 64 +++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/pybusmonitor1553/gui/monitor.py b/pybusmonitor1553/gui/monitor.py index 3556d99..afde5e5 100644 --- a/pybusmonitor1553/gui/monitor.py +++ b/pybusmonitor1553/gui/monitor.py @@ -381,16 +381,13 @@ class MonitorApp(tk.Frame): self.current_form_label = label self.current_msg_wrapper = msg_wrapper # Store wrapper for updates - # header with Apply button at top + # header with label (Apply button removed - changes are applied instantly) hdr = tk.Frame(self.detail_form_container) hdr.pack(fill=tk.X, padx=6, pady=(6,0)) lbl_hdr = tk.Label(hdr, text=f"Edit message {label}", anchor=tk.W) lbl_hdr.pack(side=tk.LEFT) - # For editable forms show Apply button, otherwise present a label - if editable: - apply_btn = tk.Button(hdr, text='Apply', command=lambda: self._apply_form_values(msg_wrapper)) - apply_btn.pack(side=tk.RIGHT, padx=6) - else: + # For read-only forms (B messages) show label + if not editable: tk.Label(hdr, text='Read-only', anchor=tk.E).pack(side=tk.RIGHT, padx=6) # recursive builder for fields @@ -503,6 +500,9 @@ class MonitorApp(tk.Frame): var.set(sel) except Exception: pass + # Add instant callback for editable comboboxes + if editable: + cb.bind('<>', lambda e, path=full: self._on_field_changed(path)) cb.pack(side=tk.RIGHT, fill=tk.X, expand=True) widget = ('combobox', cb, enum_items) else: @@ -523,6 +523,10 @@ class MonitorApp(tk.Frame): ent.config(state='disabled') except Exception: pass + else: + # Add instant callback for editable entries (on Return key or focus loss) + ent.bind('', lambda e, path=full: self._on_field_changed(path)) + ent.bind('', lambda e, path=full: self._on_field_changed(path)) ent.pack(side=tk.RIGHT, fill=tk.X, expand=True) widget = ('entry', ent) @@ -533,6 +537,54 @@ class MonitorApp(tk.Frame): except Exception: pass + def _on_field_changed(self, field_path): + """Callback when user changes a field value - apply change immediately to message.""" + try: + # Get current wrapper + msg_wrapper = getattr(self, 'current_msg_wrapper', None) + if not msg_wrapper: + return + + # Get widget and extract new value + widget = self.form_widgets.get(field_path) + if not widget: + return + + val = None + if widget[0] == 'combobox': + cb = widget[1] + sel = cb.get() + if not sel: + return + # Extract numeric value from 'NAME (value)' format + try: + start = sel.rfind('(') + end = sel.rfind(')') + if start != -1 and end != -1 and end > start: + num = sel[start+1:end].strip() + val = int(num) + else: + num = sel.split()[0] + val = int(num) + except Exception: + val = sel + else: + ent = widget[1] + txt = ent.get() + val = self._coerce_text_to_type(txt) + + # Apply change to message + try: + self._apply_edit_to_msg(field_path, str(val), msg_wrapper) + self.logger.info(f"Applied instant change: {field_path} = {val}") + except Exception as e: + self.logger.error(f"Failed to apply {field_path}: {e}") + except Exception as e: + try: + self.logger.error(f"_on_field_changed error: {e}") + except Exception: + pass + def _apply_form_values(self, msg_wrapper): """Read widgets from `self.form_widgets` and apply values to message.""" for full, widget in list(self.form_widgets.items()):