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
@@ -1,4 +1,7 @@
"""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
Expand All @@ -12,18 +15,30 @@
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:
"""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. 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.
"""
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] = []

Expand Down Expand Up @@ -58,9 +73,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)
Expand Down
31 changes: 31 additions & 0 deletions Python_Engine/Python/tests/test_bhom_tkinter_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,34 @@ 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 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"])
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 / "b.aps"]
single.destroy_root()
Loading