{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "02ef4c23",
   "metadata": {},
   "source": [
    "# 18-04 · Pozycja myszy i linie\n",
    "\n",
    "Praktyka do sekcji [„Ustalenie pozycji myszy. Rysowanie linii”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e0638c6",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Śledź pozycję myszy i rysuj linię według zdarzeń."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e79104f5",
   "metadata": {},
   "source": [
    "## O mainloop() i wydarzeniach myszy na tym laptopie\n",
    "\n",
    "Zamiast `root.mainloop()` używany tutaj `root.update()` + `root.destroy()`. A zamiast prawdziwych kliknięć myszką wywołujemy funkcje-obsługujące bezpośrednio z małym obiektem `FakeEvent(x, y)` zamiast rzeczywistego zdarzenia, Tkinter w rzeczywistej aplikacji przekazuje obiekt o tych samych polach `.x`/`.y`, więc kod obsługi sprawdzany jest naprawdę."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "78ab85ce",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:51:19.369832Z",
     "iopub.status.busy": "2026-09-03T00:51:19.369594Z",
     "iopub.status.idle": "2026-09-03T00:51:19.386146Z",
     "shell.execute_reply": "2026-09-03T00:51:19.385794Z"
    }
   },
   "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": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0bc2597c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:51:19.387570Z",
     "iopub.status.busy": "2026-09-03T00:51:19.387451Z",
     "iopub.status.idle": "2026-09-03T00:51:19.469725Z",
     "shell.execute_reply": "2026-09-03T00:51:19.469322Z"
    }
   },
   "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": [
    "## Eksperyment 1 – Wyznaczanie granicy przez dwa zdarzenia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bd995264",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:51:19.470950Z",
     "iopub.status.busy": "2026-09-03T00:51:19.470823Z",
     "iopub.status.idle": "2026-09-03T00:51:19.476256Z",
     "shell.execute_reply": "2026-09-03T00:51:19.475752Z"
    }
   },
   "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": [
    "## Sprawdzenie wyniku"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "71e51d8e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:51:19.477268Z",
     "iopub.status.busy": "2026-09-03T00:51:19.477160Z",
     "iopub.status.idle": "2026-09-03T00:51:19.479936Z",
     "shell.execute_reply": "2026-09-03T00:51:19.479422Z"
    }
   },
   "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
}
