A reusable Tkinter component for viewing and editing hierarchical data structures (dicts, lists, primitives) with an in-place tree editor. The project includes both the reusable component and a JSON editor example application.
- tree_edit_frame.py - Reusable TreeEditFrame component (use this in your own applications)
- json_editor_example.py - Example application demonstrating JSON file editing
- example.json - Sample JSON file for testing
The TreeEditFrame is a reusable Tkinter widget that provides a Treeview with in-place editing capabilities for hierarchical data.
-
Edit values directly by double-clicking cells or pressing Enter
-
Type-aware editing with appropriate widgets for each data type:
Data Type Widget Behavior strttk.EntryBasic text entry intttk.EntryValue accepted only if it can be typecast to integer floatttk.EntryValue accepted only if it can be typecast to float boolttk.CheckbuttonToggle between True/False strwith choicesttk.ComboboxDropdown selection from predefined choices -
Headless component (no file I/O - you control data loading/saving)
-
Optional combo choices for dropdown field selection
-
Expand/collapse tree navigation
-
Keyboard navigation support
import tkinter as tk
from tree_edit_frame import TreeEditFrame
# Create your application window
app = tk.Tk()
# Create the TreeEditFrame component
tree_editor = TreeEditFrame(app, combo_choices={'status': ['active', 'inactive']})
tree_editor.pack(fill=tk.BOTH, expand=True)
# Load your data
data = {'name': 'Example', 'value': 42, 'nested': {'key': 'value'}}
tree_editor.set_data(data)
# ... user edits the data ...
# Get the edited data back
edited_data = tree_editor.get_data()
print(edited_data)
# For read-only viewing:
# readonly_tree = TreeEditFrame(app, editable=False)
# readonly_tree.set_data(data)
# Or toggle editing dynamically:
# tree_editor.set_editable(False) # Make read-only
# tree_editor.set_editable(True) # Make editable again
app.mainloop()Constructor:
TreeEditFrame(master, combo_choices=None, editable=True)master: Parent Tkinter widgetcombo_choices(optional): Dict mapping field names to lists of valid choiceseditable(optional): Enable/disable editing mode (default: True)
Methods:
set_data(data, root_name="root")- Load Python object into treeget_data()- Extract Python object from treeset_combo_choices(combo_choices)- Update combo choices dictionaryset_editable(editable)- Enable/disable editing mode dynamicallyexpand_tree(expand=True)- Expand/collapse all tree nodes
The json_editor_example.py demonstrates how to use TreeEditFrame to build a complete JSON file editor with load/save functionality.
python json_editor_example.py- Load JSON files via file dialog
- Save edited JSON with proper indentation
- Demonstrates combo choices for field-specific dropdowns (see "pattern" field in example.json)
- Expand/collapse buttons for tree navigation
The example demonstrates how to configure dropdown choices for specific fields. In json_editor_example.py, combo choices are defined as:
combo_choices = {
"pattern": ["PRBS7", "PRBS31", "fast-clock", "slow-clock"]
}When editing a field named "pattern", a dropdown will appear with these predefined choices instead of a text entry field.
TreeEditFrame is a headless component - it provides the tree editing UI but does NOT handle file I/O. You control how data is loaded and saved, making it reusable for any hierarchical data source.
TreeEditFrame can be used for editing any hierarchical data structure:
- JSON configuration files
- YAML config editors
- XML data viewers
- Application settings interfaces
- Test result viewers
- API response explorers
- Database record editors
- Python 3.x with tkinter (standard library - no additional dependencies)
This project is inspired by:
- PyJSONViewer - well programmed but viewing only
- tkinter-json-editor - includes cell editing
- ttk.Treeview with Entry widget for cell editing