From 85395fb164b4628b87cd857fdd8c19529ad08273 Mon Sep 17 00:00:00 2001 From: Felix Mallinder Date: Mon, 23 Nov 2026 11:01:35 +0000 Subject: [PATCH 1/3] allow single select on directory file selector --- .../windows/directory_file_selector.py | 8 +++++--- .../Python/tests/test_bhom_tkinter_ui.py | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py index 431a74d..a00bc56 100644 --- a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py +++ b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py @@ -12,18 +12,20 @@ from python_toolkit.bhom_tkinter.theming.scaling import scale_pad class DirectoryFileSelector(BHoMBaseChildWindow): - """Display matching files and return the user's multi-selection.""" + """Display matching files and return the user's selection (one file when multi_select is False).""" def __init__( self, directory: Path, file_types: Iterable[str], selection_label: str = "file(s)", + multi_select: bool = True, **kwargs, ) -> None: self.directory = Path(directory) self.file_types = self._normalise_file_types(file_types) self.selection_label = selection_label + self.multi_select = multi_select self._cancelled = False self.selected_files: List[Path] = [] @@ -58,9 +60,9 @@ def build(self): id="file_selector_listbox", parent=self.content_frame, items=self.display_items, - selectmode=tk.MULTIPLE, + selectmode=tk.MULTIPLE if self.multi_select else tk.BROWSE, height=12, - show_selection_controls=True, + show_selection_controls=self.multi_select, build_options=PackingOptions(fill='both', expand=True), ) self.widgets.append(self.file_selector_listbox) diff --git a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py index 9379fe3..367b5fa 100644 --- a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py +++ b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py @@ -461,3 +461,23 @@ def test_searchable_list_box_fuzzy_matching(): assert only_london.get_visible() == ["London_TRY.epw"] root.destroy_root() + + +def test_directory_file_selector_single_select(tmp_path): + """multi_select=False allows one file only and hides Select All / Deselect All.""" + for name in ("a.aps", "b.aps"): + (tmp_path / name).write_text("") + + multi = DirectoryFileSelector(directory=tmp_path, file_types=[".aps"]) + assert multi.file_selector_listbox.listbox.cget("selectmode") == tk.MULTIPLE + assert hasattr(multi.file_selector_listbox, "select_all_button") + multi.destroy_root() + + single = DirectoryFileSelector(directory=tmp_path, file_types=[".aps"], multi_select=False) + listbox = single.file_selector_listbox + assert listbox.listbox.cget("selectmode") == tk.BROWSE + assert not hasattr(listbox, "select_all_button") + + listbox.listbox.selection_set(0) + single._on_submit() + assert single.selected_files == [tmp_path / "a.aps"] From fcdc1f58faa18a316d07db7ae522d7ac630ad036 Mon Sep 17 00:00:00 2001 From: Felix Mallinder Date: Mon, 23 Nov 2026 14:20:32 +0000 Subject: [PATCH 2/3] Address review: strengthen single-select test, document multi_select Co-Authored-By: Claude Sonnet 5.5 --- .../windows/directory_file_selector.py | 14 +++++++++++++- .../Python/tests/test_bhom_tkinter_ui.py | 19 +++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py index a00bc56..d4ec2fe 100644 --- a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py +++ b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py @@ -1,4 +1,6 @@ -"""Window for selecting multiple files by extension from a directory tree.""" +"""Window for selecting one or more files by extension from a directory tree. + +By default the user can select multiple files; with ``multi_select=False`` only one file can be selected.""" import tkinter as tk from tkinter import ttk @@ -22,6 +24,16 @@ def __init__( multi_select: bool = True, **kwargs, ) -> None: + """Initialise the selector window. + + Args: + directory: Directory to search for matching files. + file_types: File extensions to list. + selection_label: Label describing what is being selected. + multi_select: Defaults to True. When False, the list keeps a single + file and hides Select All / Deselect All. + **kwargs: Passed to BHoMBaseChildWindow. + """ self.directory = Path(directory) self.file_types = self._normalise_file_types(file_types) self.selection_label = selection_label diff --git a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py index 367b5fa..577188f 100644 --- a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py +++ b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py @@ -464,20 +464,31 @@ def test_searchable_list_box_fuzzy_matching(): def test_directory_file_selector_single_select(tmp_path): - """multi_select=False allows one file only and hides Select All / Deselect All.""" + """multi_select=False keeps one file only and hides Select All / Deselect All.""" for name in ("a.aps", "b.aps"): (tmp_path / name).write_text("") multi = DirectoryFileSelector(directory=tmp_path, file_types=[".aps"]) - assert multi.file_selector_listbox.listbox.cget("selectmode") == tk.MULTIPLE - assert hasattr(multi.file_selector_listbox, "select_all_button") + multi_list = multi.file_selector_listbox + assert multi_list.listbox.cget("selectmode") == tk.MULTIPLE + assert hasattr(multi_list, "select_all_button") + assert hasattr(multi_list, "deselect_all_button") + multi_list.listbox.selection_set(0) + multi_list.listbox.selection_set(1) + multi._on_submit() + assert sorted(multi.selected_files) == [tmp_path / "a.aps", tmp_path / "b.aps"] multi.destroy_root() single = DirectoryFileSelector(directory=tmp_path, file_types=[".aps"], multi_select=False) listbox = single.file_selector_listbox assert listbox.listbox.cget("selectmode") == tk.BROWSE assert not hasattr(listbox, "select_all_button") + assert not hasattr(listbox, "deselect_all_button") + # Mimic the browse binding: picking a second row replaces the first. listbox.listbox.selection_set(0) + listbox.listbox.selection_clear(0, tk.END) + listbox.listbox.selection_set(1) single._on_submit() - assert single.selected_files == [tmp_path / "a.aps"] + assert single.selected_files == [tmp_path / "b.aps"] + single.destroy_root() From 8435e1145b92c3e9f64dd70c14d88abb0ba15175 Mon Sep 17 00:00:00 2001 From: Felix Mallinder Date: Mon, 23 Nov 2026 15:54:41 +0000 Subject: [PATCH 3/3] Address review: document selection_label default, black docstring format Co-Authored-By: Claude Sonnet 5.5 --- .../bhom_tkinter/windows/directory_file_selector.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py index d4ec2fe..21ddc89 100644 --- a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py +++ b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/windows/directory_file_selector.py @@ -1,6 +1,7 @@ """Window for selecting one or more files by extension from a directory tree. -By default the user can select multiple files; with ``multi_select=False`` only one file can be selected.""" +By default the user can select multiple files; with ``multi_select=False`` only one file can be selected. +""" import tkinter as tk from tkinter import ttk @@ -29,7 +30,7 @@ def __init__( Args: directory: Directory to search for matching files. file_types: File extensions to list. - selection_label: Label describing what is being selected. + selection_label: Label describing what is being selected. Defaults to "file(s)". multi_select: Defaults to True. When False, the list keeps a single file and hides Select All / Deselect All. **kwargs: Passed to BHoMBaseChildWindow.