{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "721ba94b",
   "metadata": {},
   "source": [
    "# 16-11 · Дерево виджетов\n",
    "\n",
    "Практика к разделу [«Виджет и дерево интерфейса»](../../site/chapters/glava-16/16-11-derevo-widgetov.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3106976",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Моделировать дерево виджетов как вложенные данные и рассуждать о нём."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c7df66a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T21:45:09.489353Z",
     "iopub.status.busy": "2026-08-18T21:45:09.489244Z",
     "iopub.status.idle": "2026-08-18T21:45:09.524606Z",
     "shell.execute_reply": "2026-08-18T21:45:09.524126Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 main_frame\n"
     ]
    }
   ],
   "source": [
    "widget_tree = (\"root\", [\n",
    "    (\"main_frame\", [\n",
    "        (\"title_label\", []),\n",
    "        (\"name_entry\", []),\n",
    "        (\"save_button\", []),\n",
    "    ]),\n",
    "])\n",
    "\n",
    "def count_widgets(node):\n",
    "    label, children = node\n",
    "    total = 1\n",
    "    for child in children:\n",
    "        total += count_widgets(child)\n",
    "    return total\n",
    "\n",
    "def find_parent(node, target_label, parent_label=None):\n",
    "    label, children = node\n",
    "    if label == target_label:\n",
    "        return parent_label\n",
    "    for child in children:\n",
    "        result = find_parent(child, target_label, label)\n",
    "        if result is not None:\n",
    "            return result\n",
    "    return None\n",
    "\n",
    "total_widgets = count_widgets(widget_tree)\n",
    "save_button_parent = find_parent(widget_tree, \"save_button\")\n",
    "print(total_widgets, save_button_parent)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00da74d0",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика\n",
    "\n",
    "Найдите родителя title_label."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f40fb239",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T21:45:09.525735Z",
     "iopub.status.busy": "2026-08-18T21:45:09.525570Z",
     "iopub.status.idle": "2026-08-18T21:45:09.527740Z",
     "shell.execute_reply": "2026-08-18T21:45:09.527329Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "main_frame\n"
     ]
    }
   ],
   "source": [
    "title_label_parent = find_parent(widget_tree, \"title_label\")\n",
    "print(title_label_parent)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
