Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ def build(self):

self.refresh_sizing()

def rebuild(self) -> None:
"""Re-place all widgets so changes to `self.widgets` are reflected.

Every BHoM widget currently managed by the content frame is removed from
its geometry manager (without being destroyed), then `build()` places the
widgets that are still in `self.widgets`.
"""
for child in self.content_frame.winfo_children():
if not isinstance(child, BHoMBaseWidget):
continue
manager = child.winfo_manager()
if manager == "pack":
child.pack_forget()
elif manager == "grid":
child.grid_forget()
elif manager == "place":
child.place_forget()

self.build()

def _set_window_icon(self) -> None:
"""Set a custom window icon, replacing Tk's default icon."""
icon_path = self.theme.icon_path
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tkinter import ttk
from typing import Optional, Callable, List, Literal

from python_toolkit.bhom.logging import CONSOLE_LOGGER
from python_toolkit.bhom_tkinter.widgets._widgets_base import BHoMBaseWidget

class DropDownSelection(BHoMBaseWidget):
Expand All @@ -18,6 +19,7 @@ def __init__(
width: int = 20,
state: str = "normal",
value_var: Optional[tk.StringVar] = None,
display: Optional[Callable[[str], str]] = None,
**kwargs):
"""
Args:
Expand All @@ -27,6 +29,8 @@ def __init__(
default (str, optional): Default selected value.
width (int): Width of the dropdown widget in characters.
state (str): Combobox state ("normal", "readonly", or "disabled").
display (callable, optional): Maps an option to the text the dropdown shows for it. Display only:
`get()`, `set()` and `command` always use the plain option.
item_title (str, optional): Optional header text shown at the top of the widget frame.
helper_text (str, optional): Optional helper text shown above the dropdown.
**kwargs: Additional Frame options.
Expand All @@ -35,13 +39,19 @@ def __init__(

self.options = [str(option) for option in (options or [])]
self.command = command
self.value_var = value_var or tk.StringVar()
self.value_var = value_var or tk.StringVar(master=self)
self._display = display
self._display_to_option = {}
# With a display function the combobox shows its own text variable, kept in step with `value_var`
self._shown_var = tk.StringVar(master=self) if display else self.value_var
if display:
self.value_var.trace_add("write", lambda *_: self._shown_var.set(self._shown(self.value_var.get())))

# Create the combobox in the content frame
self.combobox = ttk.Combobox(
self.content_frame,
textvariable=self.value_var,
values=self.options,
textvariable=self._shown_var,
values=self._shown_options(),
width=width,
state=state,
justify=self._text_justify,
Expand All @@ -57,12 +67,24 @@ def __init__(
elif self.options:
self.value_var.set(self.options[0])

def _shown(self, option: str) -> str:
"""The text the dropdown shows for an option."""
return self._display(option) if self._display and option in self.options else option

def _shown_options(self) -> List[str]:
"""The dropdown entries: the options as shown, remembering which option each one is."""
self._display_to_option = {self._shown(option): option for option in self.options}
return list(self._display_to_option) if self._display else list(self.options)

def _on_select(self, event=None):
"""Handle selection change event.

Args:
event: Tkinter event (optional).
"""
if self._display:
shown = self._shown_var.get()
self.value_var.set(self._display_to_option.get(shown, shown))
if self.command:
self.command(self.get())
self._fire_on_change(self.get())
Expand Down Expand Up @@ -94,7 +116,7 @@ def set_options(self, options: List[str], default: Optional[str] = None):
default: Optional value to select after updating options.
"""
self.options = [str(option) for option in (options or [])]
self.combobox['values'] = self.options
self.combobox['values'] = self._shown_options()

if default is not None and default in self.options:
self.set(default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def __init__(
super().__init__(parent, **kwargs)

self.command = command
self._value_var = tk.StringVar()
# Bind to this widget's own interpreter: an unbound variable uses tkinter's default
# root, which can be a different (or stale) Tk instance once several roots exist.
self._value_var = tk.StringVar(master=self)

# Store range/list constraints for validation
self._from = from_
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from python_toolkit.bhom_tkinter.bhom_base_child_window import BHoMBaseChildWindow
from .colour_scale_builder import ColourScaleBuilder
from .directory_file_selector import DirectoryFileSelector
from .landing_page import LandingPage
from .modal_window import BHoMModalWindow
Expand All @@ -8,6 +9,7 @@
__all__ = [
"BHoMBaseChildWindow",
"BHoMModalWindow",
"ColourScaleBuilder",
"DirectoryFileSelector",
"LandingPage",
"ProcessingWindow",
Expand Down
Loading
Loading