{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "02ef4c23",
   "metadata": {},
   "source": [
    "# 18-04 · Позиция мыши и линии\n",
    "\n",
    "Практика к разделу [«Получаем позицию мыши. Рисуем линии»](../../site/chapters/glava-18/18-04-mysh-linii.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e0638c6",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Отследить позицию мыши и нарисовать линию по событиям."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e79104f5",
   "metadata": {},
   "source": [
    "## Про mainloop() и события мыши в этом ноутбуке\n",
    "\n",
    "Вместо `root.mainloop()` здесь используется `root.update()` + `root.destroy()`. А вместо настоящих кликов мышью мы вызываем функции-обработчики напрямую с маленьким объектом `FakeEvent(x, y)` вместо настоящего события — Tkinter в реальном приложении передаёт объект с такими же полями `.x`/`.y`, так что код обработчиков проверяется по-настоящему."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "78ab85ce",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T21:02:33.766352Z",
     "iopub.status.busy": "2026-08-14T21:02:33.766240Z",
     "iopub.status.idle": "2026-08-14T21:02:33.782006Z",
     "shell.execute_reply": "2026-08-14T21:02:33.781217Z"
    }
   },
   "outputs": [],
   "source": [
    "class FakeEvent:\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5afb551",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0bc2597c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T21:02:33.783896Z",
     "iopub.status.busy": "2026-08-14T21:02:33.783626Z",
     "iopub.status.idle": "2026-08-14T21:02:33.887486Z",
     "shell.execute_reply": "2026-08-14T21:02:33.887036Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x=150, y=75\n"
     ]
    }
   ],
   "source": [
    "import tkinter as tk\n",
    "\n",
    "root = tk.Tk()\n",
    "canvas = tk.Canvas(root, width=600, height=400, bg=\"white\")\n",
    "canvas.pack()\n",
    "\n",
    "pozicia_label = tk.Label(root, text=\"x=0, y=0\")\n",
    "pozicia_label.pack()\n",
    "\n",
    "def pokazat_poziciyu(event):\n",
    "    pozicia_label.config(text=f\"x={event.x}, y={event.y}\")\n",
    "\n",
    "pokazat_poziciyu(FakeEvent(150, 75))\n",
    "print(pozicia_label.cget(\"text\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "048d6695",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — рисуем линию по двум событиям"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bd995264",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T21:02:33.888577Z",
     "iopub.status.busy": "2026-08-14T21:02:33.888459Z",
     "iopub.status.idle": "2026-08-14T21:02:33.894509Z",
     "shell.execute_reply": "2026-08-14T21:02:33.894073Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Фигур до/после: 0 1\n"
     ]
    }
   ],
   "source": [
    "tekuschij_cvet = \"black\"\n",
    "tolschina = 3\n",
    "start_x, start_y = None, None\n",
    "\n",
    "def nachalo_risovaniya(event):\n",
    "    global start_x, start_y\n",
    "    start_x, start_y = event.x, event.y\n",
    "\n",
    "def vo_vremya_risovaniya(event):\n",
    "    canvas.create_line(start_x, start_y, event.x, event.y, fill=tekuschij_cvet, width=tolschina)\n",
    "\n",
    "before = len(canvas.find_all())\n",
    "nachalo_risovaniya(FakeEvent(10, 10))\n",
    "vo_vremya_risovaniya(FakeEvent(200, 150))\n",
    "after = len(canvas.find_all())\n",
    "print(\"Фигур до/после:\", before, after)\n",
    "root.update()\n",
    "root.destroy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b41d5ae",
   "metadata": {},
   "source": [
    "## Проверка результата"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "71e51d8e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T21:02:33.895655Z",
     "iopub.status.busy": "2026-08-14T21:02:33.895542Z",
     "iopub.status.idle": "2026-08-14T21:02:33.897682Z",
     "shell.execute_reply": "2026-08-14T21:02:33.897260Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Верно: ровно одна новая линия появилась на холсте.\n"
     ]
    }
   ],
   "source": [
    "assert after == before + 1\n",
    "print(\"Верно: ровно одна новая линия появилась на холсте.\")"
   ]
  }
 ],
 "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
}
