|
5 | 5 | import tkinter as tk |
6 | 6 | from pathlib import Path |
7 | 7 | from tkinter import filedialog, messagebox, ttk |
8 | | -from typing import TYPE_CHECKING, Any |
| 8 | +from typing import TYPE_CHECKING, Any, Callable |
9 | 9 |
|
10 | 10 | import numpy as np |
11 | 11 |
|
@@ -237,6 +237,38 @@ def _build_transform_controls( |
237 | 237 | combo.pack(side=tk.LEFT, padx=(0, 4)) |
238 | 238 | combo.bind("<<ComboboxSelected>>", lambda _e: callback()) |
239 | 239 |
|
| 240 | + def _create_styled_listbox( |
| 241 | + self, |
| 242 | + parent: tk.Widget, |
| 243 | + labels: list[str], |
| 244 | + on_select: Callable[[], None], |
| 245 | + *, |
| 246 | + height: int = 4, |
| 247 | + width: int = 12, |
| 248 | + ) -> tk.Listbox: |
| 249 | + """Create a themed multi-select Listbox for derivative/component selection.""" |
| 250 | + bg: str = get_env_from_schema("UI_BUTTON_BG") |
| 251 | + fg: str = get_env_from_schema("UI_FOREGROUND") |
| 252 | + select_bg: str = get_env_from_schema("UI_BUTTON_FG") |
| 253 | + select_fg: str = get_contrast_foreground(select_bg) |
| 254 | + lb = tk.Listbox( |
| 255 | + parent, |
| 256 | + selectmode=tk.EXTENDED, |
| 257 | + height=min(len(labels), height), |
| 258 | + width=width, |
| 259 | + bg=bg, |
| 260 | + fg=fg, |
| 261 | + selectbackground=select_bg, |
| 262 | + selectforeground=select_fg, |
| 263 | + exportselection=False, |
| 264 | + ) |
| 265 | + for lbl in labels: |
| 266 | + lb.insert(tk.END, lbl) |
| 267 | + lb.select_set(0) |
| 268 | + lb.pack(side=tk.LEFT, padx=4) |
| 269 | + lb.bind("<<ListboxSelect>>", lambda _e: on_select()) |
| 270 | + return lb |
| 271 | + |
240 | 272 | # ------------------------------------------------------------------ |
241 | 273 | # Plot tab construction |
242 | 274 | # ------------------------------------------------------------------ |
@@ -275,21 +307,9 @@ def _build_ode_scalar_tabs(self) -> None: |
275 | 307 | self._sol_labels = generate_derivative_labels(notation) |
276 | 308 | ttk.Label(ctrl, text="Show:").pack(side=tk.LEFT, padx=(0, 4)) |
277 | 309 |
|
278 | | - btn_bg: str = get_env_from_schema("UI_BUTTON_BG") |
279 | | - fg: str = get_env_from_schema("UI_FOREGROUND") |
280 | | - select_bg: str = get_env_from_schema("UI_BUTTON_FG") |
281 | | - select_fg: str = get_contrast_foreground(select_bg) |
282 | | - |
283 | | - self._sol_listbox = tk.Listbox( |
284 | | - ctrl, selectmode=tk.EXTENDED, height=min(len(self._sol_labels), 4), |
285 | | - width=12, bg=btn_bg, fg=fg, selectbackground=select_bg, |
286 | | - selectforeground=select_fg, exportselection=False, |
| 310 | + self._sol_listbox = self._create_styled_listbox( |
| 311 | + ctrl, self._sol_labels, self._update_solution_plot, height=4 |
287 | 312 | ) |
288 | | - for lbl in self._sol_labels: |
289 | | - self._sol_listbox.insert(tk.END, lbl) |
290 | | - self._sol_listbox.select_set(0) |
291 | | - self._sol_listbox.pack(side=tk.LEFT, padx=4) |
292 | | - self._sol_listbox.bind("<<ListboxSelect>>", lambda _e: self._update_solution_plot()) |
293 | 313 |
|
294 | 314 | self._build_transform_controls(ctrl, self._update_solution_plot, "sol") |
295 | 315 |
|
@@ -582,23 +602,8 @@ def _build_vector_ode_tabs(self) -> None: |
582 | 602 | self._vec_sol_labels = generate_derivative_labels(notation) |
583 | 603 | ttk.Label(ctrl, text="Show:").pack(side=tk.LEFT, padx=(0, 4)) |
584 | 604 |
|
585 | | - btn_bg: str = get_env_from_schema("UI_BUTTON_BG") |
586 | | - fg: str = get_env_from_schema("UI_FOREGROUND") |
587 | | - select_bg: str = get_env_from_schema("UI_BUTTON_FG") |
588 | | - select_fg: str = get_contrast_foreground(select_bg) |
589 | | - |
590 | | - self._vec_sol_listbox = tk.Listbox( |
591 | | - ctrl, selectmode=tk.EXTENDED, |
592 | | - height=min(len(self._vec_sol_labels), 6), width=12, |
593 | | - bg=btn_bg, fg=fg, selectbackground=select_bg, |
594 | | - selectforeground=select_fg, exportselection=False, |
595 | | - ) |
596 | | - for lbl in self._vec_sol_labels: |
597 | | - self._vec_sol_listbox.insert(tk.END, lbl) |
598 | | - self._vec_sol_listbox.select_set(0) |
599 | | - self._vec_sol_listbox.pack(side=tk.LEFT, padx=4) |
600 | | - self._vec_sol_listbox.bind( |
601 | | - "<<ListboxSelect>>", lambda _e: self._update_vec_solution_plot() |
| 605 | + self._vec_sol_listbox = self._create_styled_listbox( |
| 606 | + ctrl, self._vec_sol_labels, self._update_vec_solution_plot, height=6 |
602 | 607 | ) |
603 | 608 |
|
604 | 609 | self._build_transform_controls(ctrl, self._update_vec_solution_plot, "vec_sol") |
|
0 commit comments