Skip to content

Add widget→View reverse lookup and app widget hierarchy#91

Open
dccote wants to merge 1 commit into
mainfrom
add-widget-view-lookup-and-hierarchy
Open

Add widget→View reverse lookup and app widget hierarchy#91
dccote wants to merge 1 commit into
mainfrom
add-widget-view-lookup-and-hierarchy

Conversation

@dccote

@dccote dccote commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Two small introspection helpers for mytk:

  • Base.view_for(widget) — given any tkinter widget, return the mytk View that owns it (or None). Each placement method (grid_into/pack_into/place_into) now stamps the View onto its widget as a plain .mytk_view instance attribute right after the widget is created.
  • App.widget_hierarchy(widget=None) — return the tkinter widget tree as a nested dict (class/name/path/children) using only tkinter's built-in winfo_* methods. Defaults to the app root, so the common call is just app.widget_hierarchy(); pass a widget to root at a subtree.

widget_hierarchy lives on App rather than _BaseWidget because inspecting the whole app tree is an app-level concern, whereas view_for is genuinely widget-level.

Usage

from mytk import Base

tree = app.widget_hierarchy()                 # nested dict of the whole UI
some_widget = app.window.widget.winfo_children()[0]
Base.view_for(some_widget)                    # the mytk View that owns it

Tests

New mytk/tests/testWidgetHierarchy.py (7 tests) covers view_for after all three placement methods, the unknown-widget None case, and widget_hierarchy shape/default-root/custom-root behavior. Full suite: 511 passed, 8 skipped.

🤖 Generated with Claude Code

Add Base.view_for(widget) to recover the mytk View that owns a tkinter
widget. Each placement method (grid_into/pack_into/place_into) stamps the
View onto its widget via a plain .mytk_view instance attribute.

Add App.widget_hierarchy(widget=None) to dump the tkinter widget tree as a
nested dict using only winfo_* introspection; defaults to the app root.

Covered by mytk/tests/testWidgetHierarchy.py.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant