Skip to content

munich-ml/tkinter_json_editor

Repository files navigation

TreeEditFrame - Hierarchical Data Editor for Tkinter

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.

Project Structure

TreeEditFrame Component

The TreeEditFrame is a reusable Tkinter widget that provides a Treeview with in-place editing capabilities for hierarchical data.

Features

  • Edit values directly by double-clicking cells or pressing Enter

  • Type-aware editing with appropriate widgets for each data type:

    Data Type Widget Behavior
    str ttk.Entry Basic text entry
    int ttk.Entry Value accepted only if it can be typecast to integer
    float ttk.Entry Value accepted only if it can be typecast to float
    bool ttk.Checkbutton Toggle between True/False
    str with choices ttk.Combobox Dropdown 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

Basic Usage

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()

API Reference

Constructor:

TreeEditFrame(master, combo_choices=None, editable=True)
  • master: Parent Tkinter widget
  • combo_choices (optional): Dict mapping field names to lists of valid choices
  • editable (optional): Enable/disable editing mode (default: True)

Methods:

  • set_data(data, root_name="root") - Load Python object into tree
  • get_data() - Extract Python object from tree
  • set_combo_choices(combo_choices) - Update combo choices dictionary
  • set_editable(editable) - Enable/disable editing mode dynamically
  • expand_tree(expand=True) - Expand/collapse all tree nodes

JSON Editor Example

The json_editor_example.py demonstrates how to use TreeEditFrame to build a complete JSON file editor with load/save functionality.

Running the Example

python json_editor_example.py

Example Features

  • 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

Combo Choices

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.

Architecture

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.

Use Cases

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

Requirements

  • Python 3.x with tkinter (standard library - no additional dependencies)

References

This project is inspired by:

About

Load JSON content into a tkinter GUI for viewing and editing

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages