settings_window
Dynamic Settings Window for pyRevit Creates a reusable WPF window for managing script configurations.
Usage
from pyrevit import script from pyrevit.forms import settings_window
settings = [ {"name": "scope", "type": "choice", "label": "Scope", "options": ["Visibility", "Active State"], "default": "Visibility"}, {"name": "set_workset", "type": "bool", "label": "Set Workset", "default": True}, {"name": "tolerance", "type": "int", "label": "Tolerance (mm)", "default": 10, "min": 0, "max": 1000}, {"name": "prefix", "type": "string", "label": "Prefix", "default": ""}, {"name": "highlight_color", "type": "color", "label": "Highlight Color", "default": "#ffff0000"}, {"name": "export_folder", "type": "folder", "label": "Export Folder", "default": ""}, {"name": "template_file", "type": "file", "label": "Template File", "default": "", "file_ext": "rvt", "files_filter": "Revit Files (.rvt)|.rvt"}, ]
if settings_window.show_settings(settings, title="My Tool Settings"): print("Settings saved!")
Classes
SettingsWindow(settings_schema, section=None, title='Settings', width=450)
Bases: WPFWindow
Dynamic settings window that generates UI from schema.
Initialize the settings window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings_schema
|
List of setting definitions |
required | |
section
|
Config section name |
None
|
|
title
|
Window title |
'Settings'
|
|
width
|
Window width in pixels |
450
|
Source code in pyrevitlib/pyrevit/forms/settings_window.py
Attributes
config = script.get_config(section)
instance-attribute
settings_schema = settings_schema
instance-attribute
window_title = title
instance-attribute
window_width = width
instance-attribute
config_section = section
instance-attribute
result = False
instance-attribute
controls = {}
instance-attribute
Title = self.window_title
instance-attribute
pyrevit_version
property
Active pyRevit formatted version e.g. '4.9-beta'.
Functions
save_clicked(sender, args)
Handle save button click.
Source code in pyrevitlib/pyrevit/forms/settings_window.py
cancel_clicked(sender, args)
reset_clicked(sender, args)
Handle reset button click.
Source code in pyrevitlib/pyrevit/forms/settings_window.py
load_xaml(xaml_source, literal_string=False, handle_esc=True, set_owner=True)
Load the window XAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xaml_source
|
str
|
The XAML content or file path to load. |
required |
literal_string
|
bool
|
True if |
False
|
handle_esc
|
bool
|
Whether the ESC key should be handled. Defaults to True. |
True
|
set_owner
|
bool
|
Whether to se the window owner. Defaults to True. |
True
|
Source code in pyrevitlib/pyrevit/forms/__init__.py
merge_resource_dict(xaml_source)
Merge a ResourceDictionary xaml file with this window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xaml_source
|
str
|
xaml file with the resource dictionary |
required |
Source code in pyrevitlib/pyrevit/forms/__init__.py
get_locale_string(string_name)
Get localized string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string_name
|
str
|
string name |
required |
Returns:
| Type | Description |
|---|---|
str
|
localized string |
setup_owner()
setup_resources(wpf_ctrl)
staticmethod
Sets the WPF resources.
Source code in pyrevitlib/pyrevit/forms/__init__.py
setup_default_handlers()
handle_input_key(sender, args)
set_icon(icon_path)
setup_icon()
hide()
show(modal=False)
show_dialog()
set_image_source_file(wpf_element, image_file)
staticmethod
Set source file for image element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wpf_element
|
Image
|
xaml image element |
required |
image_file
|
str
|
image file path |
required |
Source code in pyrevitlib/pyrevit/forms/__init__.py
set_image_source(wpf_element, image_file)
Set source file for image element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wpf_element
|
Image
|
xaml image element |
required |
image_file
|
str
|
image file path |
required |
Source code in pyrevitlib/pyrevit/forms/__init__.py
dispatch(func, *args, **kwargs)
Runs the function in a new thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
function to run |
required |
*args
|
Any
|
positional arguments to pass to func |
()
|
**kwargs
|
Any
|
keyword arguments to pass to func |
{}
|
Source code in pyrevitlib/pyrevit/forms/__init__.py
conceal()
hide_element(*wpf_elements)
staticmethod
Collapse elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*wpf_elements
|
list[UIElement]
|
WPF framework elements to be collaped |
()
|
show_element(*wpf_elements)
staticmethod
Show collapsed elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*wpf_elements
|
list[UIElement]
|
WPF framework elements to be set to visible. |
()
|
Source code in pyrevitlib/pyrevit/forms/__init__.py
toggle_element(*wpf_elements)
staticmethod
Toggle visibility of elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*wpf_elements
|
list[UIElement]
|
WPF framework elements to be toggled. |
()
|
Source code in pyrevitlib/pyrevit/forms/__init__.py
disable_element(*wpf_elements)
staticmethod
Enable elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*wpf_elements
|
list[UIElement]
|
WPF framework elements to be enabled |
()
|
enable_element(*wpf_elements)
staticmethod
Enable elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*wpf_elements
|
list[UIElement]
|
WPF framework elements to be enabled |
()
|
handle_url_click(sender, args)
Callback for handling click on package website url.
Functions
show_settings(settings_schema, section=None, title='Settings', width=450)
Show settings window and return True if saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings_schema
|
List of setting definitions. Each setting is a dict with: - name (str): Setting key name for config - type (str): "bool", "choice", "int", "float", "string", "color", "folder", or "file" - label (str): Display label for the setting - default: Default value if not in config - options (list): For "choice" type, list of options - min (int/float): For "int"/"float" type, minimum value - max (int/float): For "int"/"float" type, maximum value - required (bool): For "string" type, whether field is required - file_ext (str): For "file" type, file extension filter (e.g., "rvt") - files_filter (str): For "file" type, files filter (e.g., "Revit Files (.rvt)|.rvt") - init_dir (str): For "file" type, initial directory - multi_file (bool): For "file" type, allow multiple file selection |
required | |
section
|
str
|
Config section name (default: None) |
None
|
title
|
str
|
Window title (default: "Settings") |
'Settings'
|
width
|
int
|
Window width in pixels (default: 450) |
450
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if settings were saved, False if canceled |
Example
settings_schema = [
{"name": "scope", "type": "choice", "label": "Scope",
"options": ["Visibility", "Active State"], "default": "Visibility"},
{"name": "set_workset", "type": "bool", "label": "Set Workset", "default": True},
{"name": "tolerance", "type": "int", "label": "Tolerance (mm)",
"default": 10, "min": 0, "max": 1000},
{"name": "highlight_color", "type": "color", "label": "Highlight Color",
"default": "#ffff0000"},
{"name": "export_folder", "type": "folder", "label": "Export Folder", "default": ""},
{"name": "template_file", "type": "file", "label": "Template File",
"default": "", "file_ext": "rvt", "files_filter": "Revit Files (*.rvt)|*.rvt"},
]
if show_settings(settings_schema, section="MyToolSection", title="My Tool Settings"):
print("Settings saved!")