{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "db078243",
   "metadata": {},
   "source": [
    "# 16-30 · Redaktor Notatek\n",
    "\n",
    "Praktyka do sekcji [„Mini-projekt: edytor notatek”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57d25793",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Złóż edytor notatek z menu, filedialog i pathlib."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81e1cf1a",
   "metadata": {},
   "source": [
    "## Pro mainloop() w tym laptopie\n",
    "\n",
    "W normalnym życiu `.py`- pliku aplikacja jest uruchamiana przez `root.mainloop()` - To polecenie „zamraża” `root.mainloop()` zadzwonimy `root.update()` (obsługuje zdarzenia raz, bez oczekiwania) a następnie `root.destroy()` — interfejs buduje się dokładnie tak samo, po prostu bez nieskończonego oczekiwania na końcu."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8170bdb",
   "metadata": {},
   "source": [
    "## Pro filedialog w tym laptopie\n",
    "\n",
    "Okno wyboru pliku czeka na użytkownika na żywo. W automatycznie uruchamianym laptopie niezbędne funkcje `filedialog` tymczasowo zastępowane przez przygotowane odpowiedzi."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e2c08990",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T21:45:25.704947Z",
     "iopub.status.busy": "2026-08-18T21:45:25.704842Z",
     "iopub.status.idle": "2026-08-18T21:45:25.757911Z",
     "shell.execute_reply": "2026-08-18T21:45:25.757368Z"
    }
   },
   "outputs": [],
   "source": [
    "from tkinter import filedialog\n",
    "\n",
    "filedialog.asksaveasfilename = lambda *args, **kwargs: 'moi_zametki_test.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f1839c36",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T21:45:25.759023Z",
     "iopub.status.busy": "2026-08-18T21:45:25.758906Z",
     "iopub.status.idle": "2026-08-18T21:45:25.856875Z",
     "shell.execute_reply": "2026-08-18T21:45:25.856346Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Заметка о главе 16\n"
     ]
    }
   ],
   "source": [
    "import tkinter as tk\n",
    "from tkinter import ttk\n",
    "from tkinter.scrolledtext import ScrolledText\n",
    "from pathlib import Path\n",
    "\n",
    "root = tk.Tk()\n",
    "root.title(\"Редактор заметок\")\n",
    "\n",
    "text_widget = ScrolledText(root, wrap=\"word\")\n",
    "text_widget.pack(fill=\"both\", expand=True)\n",
    "text_widget.insert(\"1.0\", \"Заметка о главе 16\")\n",
    "\n",
    "current_path = None\n",
    "\n",
    "def sohranit_kak():\n",
    "    global current_path\n",
    "    filename = filedialog.asksaveasfilename(defaultextension=\".txt\")\n",
    "    if not filename:\n",
    "        return\n",
    "    current_path = Path(filename)\n",
    "    current_path.write_text(text_widget.get(\"1.0\", \"end-1c\"), encoding=\"utf-8\")\n",
    "\n",
    "sohranit_kak()\n",
    "root.update()\n",
    "print(current_path.read_text(encoding=\"utf-8\"))\n",
    "root.destroy()"
   ]
  }
 ],
 "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
}
