中級者向け No.087

デスクトップ付箋アプリ

常時最前面表示・カラー選択・自動保存機能付きのデスクトップ付箋。複数枚同時表示対応。

🎯 難易度: ★★☆ 📦 ライブラリ: tkinter(標準ライブラリ)のみ ⏱️ 制作時間: 30〜90分

1. アプリ概要

常時最前面表示・カラー選択・自動保存機能付きのデスクトップ付箋。複数枚同時表示対応。

このアプリはutilカテゴリの実践的なPythonアプリです。使用ライブラリは tkinter(標準ライブラリ)、難易度は ★★☆ です。

Pythonの豊富なライブラリを活用することで、実用的なアプリを短いコードで実装できます。ソースコードをコピーして実行し、仕組みを理解したうえでカスタマイズに挑戦してみてください。

GUIアプリ開発はプログラミングの楽しさを実感できる最も効果的な学習方法のひとつです。変数・関数・クラス・イベント処理などの重要な概念が自然と身につきます。

2. 機能一覧

  • デスクトップ付箋アプリのメイン機能
  • 直感的なGUIインターフェース
  • 入力値のバリデーション
  • エラーハンドリング
  • 結果の見やすい表示
  • クリア機能付き

3. 事前準備・環境

ℹ️
動作確認環境

Python 3.10 以上 / Windows・Mac・Linux すべて対応

以下の環境で動作確認しています。

  • Python 3.10 以上
  • OS: Windows 10/11・macOS 12+・Ubuntu 20.04+

4. 完全なソースコード

💡
コードのコピー方法

右上の「コピー」ボタンをクリックするとコードをクリップボードにコピーできます。

app087.py
import tkinter as tk
from tkinter import ttk, messagebox, colorchooser
import json
import os
from datetime import datetime

SAVE_PATH = os.path.join(os.path.dirname(__file__), "sticky_notes.json")

NOTE_COLORS = {
    "黄":  "#fffde7",
    "青":  "#e3f2fd",
    "緑":  "#e8f5e9",
    "ピンク": "#fce4ec",
    "紫":  "#f3e5f5",
    "白":  "#ffffff",
}


class StickyNote:
    """個別の付箋ウィンドウ"""

    def __init__(self, master, note_data, on_save, on_delete):
        self._data = note_data
        self._on_save   = on_save
        self._on_delete = on_delete

        color = note_data.get("color", "#fffde7")
        x = note_data.get("x", 100)
        y = note_data.get("y", 100)
        w = note_data.get("w", 220)
        h = note_data.get("h", 180)

        self.win = tk.Toplevel(master)
        self.win.overrideredirect(True)   # タイトルバー非表示
        self.win.geometry(f"{w}x{h}+{x}+{y}")
        self.win.configure(bg=color)
        self.win.attributes("-topmost", note_data.get("topmost", False))

        # タイトルバー代替
        title_bar = tk.Frame(self.win, bg=self._darken(color), height=22)
        title_bar.pack(fill=tk.X)
        title_bar.pack_propagate(False)

        tk.Label(title_bar, text="📌", bg=self._darken(color),
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=4)

        tk.Button(title_bar, text="✕", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._hide).pack(side=tk.RIGHT, padx=2)
        tk.Button(title_bar, text="⋮", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._menu).pack(side=tk.RIGHT, padx=2)

        # ドラッグ移動
        self._drag_x = self._drag_y = 0
        title_bar.bind("<ButtonPress-1>",   self._start_move)
        title_bar.bind("<B1-Motion>",       self._do_move)
        title_bar.bind("<ButtonRelease-1>", self._end_move)

        # テキストエリア
        self.text = tk.Text(self.win, bg=color, fg="#333",
                             font=("Noto Sans JP", 10), relief=tk.FLAT,
                             insertbackground="#333", wrap=tk.WORD,
                             padx=4, pady=2, undo=True)
        self.text.pack(fill=tk.BOTH, expand=True)
        self.text.insert("1.0", note_data.get("content", ""))
        self.text.bind("<<Modified>>", self._on_modified)

        # リサイズグリップ
        grip = tk.Label(self.win, text="◢", bg=self._darken(color),
                         fg="#999", font=("Arial", 7), cursor="size_nw_se")
        grip.place(relx=1.0, rely=1.0, anchor="se")
        grip.bind("<ButtonPress-1>",   self._start_resize)
        grip.bind("<B1-Motion>",       self._do_resize)

    def _darken(self, hex_color):
        """16進カラーを少し暗くする"""
        try:
            r = int(hex_color[1:3], 16)
            g = int(hex_color[3:5], 16)
            b = int(hex_color[5:7], 16)
            return f"#{max(0,r-30):02x}{max(0,g-30):02x}{max(0,b-30):02x}"
        except Exception:
            return hex_color

    def _start_move(self, e):
        self._drag_x = e.x
        self._drag_y = e.y

    def _do_move(self, e):
        dx = e.x - self._drag_x
        dy = e.y - self._drag_y
        x = self.win.winfo_x() + dx
        y = self.win.winfo_y() + dy
        self.win.geometry(f"+{x}+{y}")

    def _end_move(self, _e):
        self._data["x"] = self.win.winfo_x()
        self._data["y"] = self.win.winfo_y()
        self._on_save()

    def _start_resize(self, e):
        self._rx = e.x_root
        self._ry = e.y_root
        self._rw = self.win.winfo_width()
        self._rh = self.win.winfo_height()

    def _do_resize(self, e):
        dw = e.x_root - self._rx
        dh = e.y_root - self._ry
        nw = max(150, self._rw + dw)
        nh = max(100, self._rh + dh)
        self.win.geometry(f"{nw}x{nh}")
        self._data["w"] = nw
        self._data["h"] = nh

    def _on_modified(self, _=None):
        self._data["content"] = self.text.get("1.0", tk.END).rstrip()
        self.text.edit_modified(False)
        self._on_save()

    def _hide(self):
        self._data["visible"] = False
        self.win.withdraw()
        self._on_save()

    def _menu(self):
        m = tk.Menu(self.win, tearoff=False, bg="#252526", fg="#c9d1d9")
        m.add_command(label="🗑 削除", command=self._delete)
        m.add_separator()
        for name, hex_c in NOTE_COLORS.items():
            m.add_command(label=f"🎨 {name}",
                           command=lambda c=hex_c: self._change_color(c))
        pin_label = "📌 ピン留め解除" if self._data.get("topmost") else "📌 ピン留め"
        m.add_command(label=pin_label, command=self._toggle_topmost)
        m.post(self.win.winfo_rootx() + self.win.winfo_width() - 60,
               self.win.winfo_rooty() + 22)

    def _delete(self):
        self._data["deleted"] = True
        self.win.destroy()
        self._on_delete(self._data["id"])

    def _change_color(self, color):
        self._data["color"] = color
        self.win.configure(bg=color)
        self.text.configure(bg=color)
        self._on_save()

    def _toggle_topmost(self):
        val = not self._data.get("topmost", False)
        self._data["topmost"] = val
        self.win.attributes("-topmost", val)
        self._on_save()


class App087:
    """デスクトップ付箋アプリ"""

    def __init__(self, root):
        self.root = root
        self.root.title("デスクトップ付箋アプリ")
        self.root.geometry("320x440")
        self.root.configure(bg="#1e1e1e")
        self._notes   = []   # note data dicts
        self._windows = {}   # {note_id: StickyNote}
        self._next_id = 1
        self._build_ui()
        self._load()

    def _build_ui(self):
        header = tk.Frame(self.root, bg="#252526", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="📌 デスクトップ付箋アプリ",
                 font=("Noto Sans JP", 11, "bold"),
                 bg="#252526", fg="#4fc3f7").pack(side=tk.LEFT, padx=12)

        tb = tk.Frame(self.root, bg="#2d2d2d", pady=4)
        tb.pack(fill=tk.X)
        for color_name, hex_c in NOTE_COLORS.items():
            btn = tk.Button(tb, text=color_name, bg=hex_c, fg="#333",
                             relief=tk.FLAT, font=("Arial", 8), padx=4, pady=3,
                             activebackground=hex_c, bd=1,
                             command=lambda c=hex_c: self._new_note(c))
            btn.pack(side=tk.LEFT, padx=1)

        # 付箋一覧
        tk.Label(self.root, text="付箋一覧", bg="#1e1e1e", fg="#888",
                 font=("Arial", 9)).pack(anchor="w", padx=8, pady=(4, 0))
        cols = ("color", "preview", "updated")
        self.tree = ttk.Treeview(self.root, columns=cols, show="headings",
                                  selectmode="browse")
        self.tree.heading("color",   text="色")
        self.tree.heading("preview", text="内容")
        self.tree.heading("updated", text="更新")
        self.tree.column("color",   width=40,  anchor="center")
        self.tree.column("preview", width=160, anchor="w")
        self.tree.column("updated", width=80,  anchor="center")
        tsb = ttk.Scrollbar(self.root, command=self.tree.yview)
        self.tree.configure(yscrollcommand=tsb.set)
        tsb.pack(side=tk.RIGHT, fill=tk.Y)
        self.tree.pack(fill=tk.BOTH, expand=True, padx=(8, 0))
        self.tree.bind("<<TreeviewSelect>>", self._on_select)
        self.tree.bind("<Double-1>", self._show_selected)

        btn_row = tk.Frame(self.root, bg="#1e1e1e")
        btn_row.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_row, text="表示", command=self._show_selected).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全表示", command=self._show_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全非表示", command=self._hide_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="削除", command=self._delete_selected).pack(side=tk.RIGHT, padx=2)

        self.status_var = tk.StringVar(value="付箋を追加してください")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#252526", fg="#858585", font=("Arial", 9),
                 anchor="w", padx=8).pack(fill=tk.X, side=tk.BOTTOM)

    def _new_note(self, color="#fffde7"):
        note = {
            "id":      self._next_id,
            "content": "",
            "color":   color,
            "x":       200 + self._next_id * 30,
            "y":       100 + self._next_id * 30,
            "w":       220,
            "h":       180,
            "visible": True,
            "topmost": False,
            "updated": datetime.now().strftime("%m/%d %H:%M"),
        }
        self._next_id += 1
        self._notes.append(note)
        self._open_window(note)
        self._refresh_list()
        self._save()

    def _open_window(self, note):
        note_id = note["id"]
        if note_id in self._windows:
            self._windows[note_id].win.deiconify()
            return
        sn = StickyNote(
            self.root, note,
            on_save=self._on_note_save,
            on_delete=self._on_note_delete)
        self._windows[note_id] = sn

    def _on_note_save(self):
        for n in self._notes:
            n["updated"] = datetime.now().strftime("%m/%d %H:%M")
        self._refresh_list()
        self._save()

    def _on_note_delete(self, note_id):
        self._notes = [n for n in self._notes if n["id"] != note_id]
        if note_id in self._windows:
            del self._windows[note_id]
        self._refresh_list()
        self._save()

    def _refresh_list(self):
        self.tree.delete(*self.tree.get_children())
        for n in self._notes:
            preview = n.get("content", "")[:30].replace("\n", " ")
            self.tree.insert("", tk.END, text=n["id"],
                              values=("●", preview, n.get("updated", "")))
        self.status_var.set(f"付箋: {len(self._notes)} 枚")

    def _on_select(self, _=None):
        pass

    def _show_selected(self, _=None):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        note = next((n for n in self._notes if n["id"] == note_id), None)
        if note:
            note["visible"] = True
            self._open_window(note)
            if note_id in self._windows:
                self._windows[note_id].win.deiconify()

    def _show_all(self):
        for n in self._notes:
            n["visible"] = True
            self._open_window(n)
            if n["id"] in self._windows:
                self._windows[n["id"]].win.deiconify()

    def _hide_all(self):
        for sn in self._windows.values():
            sn.win.withdraw()

    def _delete_selected(self):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        if note_id in self._windows:
            self._windows[note_id].win.destroy()
            del self._windows[note_id]
        self._notes = [n for n in self._notes if n["id"] != note_id]
        self._refresh_list()
        self._save()

    def _save(self):
        try:
            with open(SAVE_PATH, "w", encoding="utf-8") as f:
                json.dump({"next_id": self._next_id,
                           "notes": self._notes}, f,
                          ensure_ascii=False, indent=2)
        except Exception:
            pass

    def _load(self):
        if not os.path.exists(SAVE_PATH):
            return
        try:
            with open(SAVE_PATH, encoding="utf-8") as f:
                data = json.load(f)
            self._next_id = data.get("next_id", 1)
            self._notes   = data.get("notes", [])
            for n in self._notes:
                if n.get("visible", True):
                    self._open_window(n)
            self._refresh_list()
        except Exception:
            pass


if __name__ == "__main__":
    root = tk.Tk()
    app = App087(root)
    root.mainloop()

5. コード解説

デスクトップ付箋アプリのコードを詳しく解説します。クラスベースの設計で各機能を整理して実装しています。

クラス設計とコンストラクタ

App087クラスにアプリの全機能をまとめています。__init__でウィンドウ設定、_build_ui()でUI構築、process()でメイン処理を担当します。責任の分離により、コードが読みやすくなります。

import tkinter as tk
from tkinter import ttk, messagebox, colorchooser
import json
import os
from datetime import datetime

SAVE_PATH = os.path.join(os.path.dirname(__file__), "sticky_notes.json")

NOTE_COLORS = {
    "黄":  "#fffde7",
    "青":  "#e3f2fd",
    "緑":  "#e8f5e9",
    "ピンク": "#fce4ec",
    "紫":  "#f3e5f5",
    "白":  "#ffffff",
}


class StickyNote:
    """個別の付箋ウィンドウ"""

    def __init__(self, master, note_data, on_save, on_delete):
        self._data = note_data
        self._on_save   = on_save
        self._on_delete = on_delete

        color = note_data.get("color", "#fffde7")
        x = note_data.get("x", 100)
        y = note_data.get("y", 100)
        w = note_data.get("w", 220)
        h = note_data.get("h", 180)

        self.win = tk.Toplevel(master)
        self.win.overrideredirect(True)   # タイトルバー非表示
        self.win.geometry(f"{w}x{h}+{x}+{y}")
        self.win.configure(bg=color)
        self.win.attributes("-topmost", note_data.get("topmost", False))

        # タイトルバー代替
        title_bar = tk.Frame(self.win, bg=self._darken(color), height=22)
        title_bar.pack(fill=tk.X)
        title_bar.pack_propagate(False)

        tk.Label(title_bar, text="📌", bg=self._darken(color),
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=4)

        tk.Button(title_bar, text="✕", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._hide).pack(side=tk.RIGHT, padx=2)
        tk.Button(title_bar, text="⋮", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._menu).pack(side=tk.RIGHT, padx=2)

        # ドラッグ移動
        self._drag_x = self._drag_y = 0
        title_bar.bind("<ButtonPress-1>",   self._start_move)
        title_bar.bind("<B1-Motion>",       self._do_move)
        title_bar.bind("<ButtonRelease-1>", self._end_move)

        # テキストエリア
        self.text = tk.Text(self.win, bg=color, fg="#333",
                             font=("Noto Sans JP", 10), relief=tk.FLAT,
                             insertbackground="#333", wrap=tk.WORD,
                             padx=4, pady=2, undo=True)
        self.text.pack(fill=tk.BOTH, expand=True)
        self.text.insert("1.0", note_data.get("content", ""))
        self.text.bind("<<Modified>>", self._on_modified)

        # リサイズグリップ
        grip = tk.Label(self.win, text="◢", bg=self._darken(color),
                         fg="#999", font=("Arial", 7), cursor="size_nw_se")
        grip.place(relx=1.0, rely=1.0, anchor="se")
        grip.bind("<ButtonPress-1>",   self._start_resize)
        grip.bind("<B1-Motion>",       self._do_resize)

    def _darken(self, hex_color):
        """16進カラーを少し暗くする"""
        try:
            r = int(hex_color[1:3], 16)
            g = int(hex_color[3:5], 16)
            b = int(hex_color[5:7], 16)
            return f"#{max(0,r-30):02x}{max(0,g-30):02x}{max(0,b-30):02x}"
        except Exception:
            return hex_color

    def _start_move(self, e):
        self._drag_x = e.x
        self._drag_y = e.y

    def _do_move(self, e):
        dx = e.x - self._drag_x
        dy = e.y - self._drag_y
        x = self.win.winfo_x() + dx
        y = self.win.winfo_y() + dy
        self.win.geometry(f"+{x}+{y}")

    def _end_move(self, _e):
        self._data["x"] = self.win.winfo_x()
        self._data["y"] = self.win.winfo_y()
        self._on_save()

    def _start_resize(self, e):
        self._rx = e.x_root
        self._ry = e.y_root
        self._rw = self.win.winfo_width()
        self._rh = self.win.winfo_height()

    def _do_resize(self, e):
        dw = e.x_root - self._rx
        dh = e.y_root - self._ry
        nw = max(150, self._rw + dw)
        nh = max(100, self._rh + dh)
        self.win.geometry(f"{nw}x{nh}")
        self._data["w"] = nw
        self._data["h"] = nh

    def _on_modified(self, _=None):
        self._data["content"] = self.text.get("1.0", tk.END).rstrip()
        self.text.edit_modified(False)
        self._on_save()

    def _hide(self):
        self._data["visible"] = False
        self.win.withdraw()
        self._on_save()

    def _menu(self):
        m = tk.Menu(self.win, tearoff=False, bg="#252526", fg="#c9d1d9")
        m.add_command(label="🗑 削除", command=self._delete)
        m.add_separator()
        for name, hex_c in NOTE_COLORS.items():
            m.add_command(label=f"🎨 {name}",
                           command=lambda c=hex_c: self._change_color(c))
        pin_label = "📌 ピン留め解除" if self._data.get("topmost") else "📌 ピン留め"
        m.add_command(label=pin_label, command=self._toggle_topmost)
        m.post(self.win.winfo_rootx() + self.win.winfo_width() - 60,
               self.win.winfo_rooty() + 22)

    def _delete(self):
        self._data["deleted"] = True
        self.win.destroy()
        self._on_delete(self._data["id"])

    def _change_color(self, color):
        self._data["color"] = color
        self.win.configure(bg=color)
        self.text.configure(bg=color)
        self._on_save()

    def _toggle_topmost(self):
        val = not self._data.get("topmost", False)
        self._data["topmost"] = val
        self.win.attributes("-topmost", val)
        self._on_save()


class App087:
    """デスクトップ付箋アプリ"""

    def __init__(self, root):
        self.root = root
        self.root.title("デスクトップ付箋アプリ")
        self.root.geometry("320x440")
        self.root.configure(bg="#1e1e1e")
        self._notes   = []   # note data dicts
        self._windows = {}   # {note_id: StickyNote}
        self._next_id = 1
        self._build_ui()
        self._load()

    def _build_ui(self):
        header = tk.Frame(self.root, bg="#252526", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="📌 デスクトップ付箋アプリ",
                 font=("Noto Sans JP", 11, "bold"),
                 bg="#252526", fg="#4fc3f7").pack(side=tk.LEFT, padx=12)

        tb = tk.Frame(self.root, bg="#2d2d2d", pady=4)
        tb.pack(fill=tk.X)
        for color_name, hex_c in NOTE_COLORS.items():
            btn = tk.Button(tb, text=color_name, bg=hex_c, fg="#333",
                             relief=tk.FLAT, font=("Arial", 8), padx=4, pady=3,
                             activebackground=hex_c, bd=1,
                             command=lambda c=hex_c: self._new_note(c))
            btn.pack(side=tk.LEFT, padx=1)

        # 付箋一覧
        tk.Label(self.root, text="付箋一覧", bg="#1e1e1e", fg="#888",
                 font=("Arial", 9)).pack(anchor="w", padx=8, pady=(4, 0))
        cols = ("color", "preview", "updated")
        self.tree = ttk.Treeview(self.root, columns=cols, show="headings",
                                  selectmode="browse")
        self.tree.heading("color",   text="色")
        self.tree.heading("preview", text="内容")
        self.tree.heading("updated", text="更新")
        self.tree.column("color",   width=40,  anchor="center")
        self.tree.column("preview", width=160, anchor="w")
        self.tree.column("updated", width=80,  anchor="center")
        tsb = ttk.Scrollbar(self.root, command=self.tree.yview)
        self.tree.configure(yscrollcommand=tsb.set)
        tsb.pack(side=tk.RIGHT, fill=tk.Y)
        self.tree.pack(fill=tk.BOTH, expand=True, padx=(8, 0))
        self.tree.bind("<<TreeviewSelect>>", self._on_select)
        self.tree.bind("<Double-1>", self._show_selected)

        btn_row = tk.Frame(self.root, bg="#1e1e1e")
        btn_row.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_row, text="表示", command=self._show_selected).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全表示", command=self._show_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全非表示", command=self._hide_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="削除", command=self._delete_selected).pack(side=tk.RIGHT, padx=2)

        self.status_var = tk.StringVar(value="付箋を追加してください")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#252526", fg="#858585", font=("Arial", 9),
                 anchor="w", padx=8).pack(fill=tk.X, side=tk.BOTTOM)

    def _new_note(self, color="#fffde7"):
        note = {
            "id":      self._next_id,
            "content": "",
            "color":   color,
            "x":       200 + self._next_id * 30,
            "y":       100 + self._next_id * 30,
            "w":       220,
            "h":       180,
            "visible": True,
            "topmost": False,
            "updated": datetime.now().strftime("%m/%d %H:%M"),
        }
        self._next_id += 1
        self._notes.append(note)
        self._open_window(note)
        self._refresh_list()
        self._save()

    def _open_window(self, note):
        note_id = note["id"]
        if note_id in self._windows:
            self._windows[note_id].win.deiconify()
            return
        sn = StickyNote(
            self.root, note,
            on_save=self._on_note_save,
            on_delete=self._on_note_delete)
        self._windows[note_id] = sn

    def _on_note_save(self):
        for n in self._notes:
            n["updated"] = datetime.now().strftime("%m/%d %H:%M")
        self._refresh_list()
        self._save()

    def _on_note_delete(self, note_id):
        self._notes = [n for n in self._notes if n["id"] != note_id]
        if note_id in self._windows:
            del self._windows[note_id]
        self._refresh_list()
        self._save()

    def _refresh_list(self):
        self.tree.delete(*self.tree.get_children())
        for n in self._notes:
            preview = n.get("content", "")[:30].replace("\n", " ")
            self.tree.insert("", tk.END, text=n["id"],
                              values=("●", preview, n.get("updated", "")))
        self.status_var.set(f"付箋: {len(self._notes)} 枚")

    def _on_select(self, _=None):
        pass

    def _show_selected(self, _=None):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        note = next((n for n in self._notes if n["id"] == note_id), None)
        if note:
            note["visible"] = True
            self._open_window(note)
            if note_id in self._windows:
                self._windows[note_id].win.deiconify()

    def _show_all(self):
        for n in self._notes:
            n["visible"] = True
            self._open_window(n)
            if n["id"] in self._windows:
                self._windows[n["id"]].win.deiconify()

    def _hide_all(self):
        for sn in self._windows.values():
            sn.win.withdraw()

    def _delete_selected(self):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        if note_id in self._windows:
            self._windows[note_id].win.destroy()
            del self._windows[note_id]
        self._notes = [n for n in self._notes if n["id"] != note_id]
        self._refresh_list()
        self._save()

    def _save(self):
        try:
            with open(SAVE_PATH, "w", encoding="utf-8") as f:
                json.dump({"next_id": self._next_id,
                           "notes": self._notes}, f,
                          ensure_ascii=False, indent=2)
        except Exception:
            pass

    def _load(self):
        if not os.path.exists(SAVE_PATH):
            return
        try:
            with open(SAVE_PATH, encoding="utf-8") as f:
                data = json.load(f)
            self._next_id = data.get("next_id", 1)
            self._notes   = data.get("notes", [])
            for n in self._notes:
                if n.get("visible", True):
                    self._open_window(n)
            self._refresh_list()
        except Exception:
            pass


if __name__ == "__main__":
    root = tk.Tk()
    app = App087(root)
    root.mainloop()

UIレイアウトの構築

LabelFrameで入力エリアと結果エリアを視覚的に分けています。pack()で縦に並べ、expand=Trueで結果エリアが画面いっぱいに広がるよう設定しています。

import tkinter as tk
from tkinter import ttk, messagebox, colorchooser
import json
import os
from datetime import datetime

SAVE_PATH = os.path.join(os.path.dirname(__file__), "sticky_notes.json")

NOTE_COLORS = {
    "黄":  "#fffde7",
    "青":  "#e3f2fd",
    "緑":  "#e8f5e9",
    "ピンク": "#fce4ec",
    "紫":  "#f3e5f5",
    "白":  "#ffffff",
}


class StickyNote:
    """個別の付箋ウィンドウ"""

    def __init__(self, master, note_data, on_save, on_delete):
        self._data = note_data
        self._on_save   = on_save
        self._on_delete = on_delete

        color = note_data.get("color", "#fffde7")
        x = note_data.get("x", 100)
        y = note_data.get("y", 100)
        w = note_data.get("w", 220)
        h = note_data.get("h", 180)

        self.win = tk.Toplevel(master)
        self.win.overrideredirect(True)   # タイトルバー非表示
        self.win.geometry(f"{w}x{h}+{x}+{y}")
        self.win.configure(bg=color)
        self.win.attributes("-topmost", note_data.get("topmost", False))

        # タイトルバー代替
        title_bar = tk.Frame(self.win, bg=self._darken(color), height=22)
        title_bar.pack(fill=tk.X)
        title_bar.pack_propagate(False)

        tk.Label(title_bar, text="📌", bg=self._darken(color),
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=4)

        tk.Button(title_bar, text="✕", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._hide).pack(side=tk.RIGHT, padx=2)
        tk.Button(title_bar, text="⋮", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._menu).pack(side=tk.RIGHT, padx=2)

        # ドラッグ移動
        self._drag_x = self._drag_y = 0
        title_bar.bind("<ButtonPress-1>",   self._start_move)
        title_bar.bind("<B1-Motion>",       self._do_move)
        title_bar.bind("<ButtonRelease-1>", self._end_move)

        # テキストエリア
        self.text = tk.Text(self.win, bg=color, fg="#333",
                             font=("Noto Sans JP", 10), relief=tk.FLAT,
                             insertbackground="#333", wrap=tk.WORD,
                             padx=4, pady=2, undo=True)
        self.text.pack(fill=tk.BOTH, expand=True)
        self.text.insert("1.0", note_data.get("content", ""))
        self.text.bind("<<Modified>>", self._on_modified)

        # リサイズグリップ
        grip = tk.Label(self.win, text="◢", bg=self._darken(color),
                         fg="#999", font=("Arial", 7), cursor="size_nw_se")
        grip.place(relx=1.0, rely=1.0, anchor="se")
        grip.bind("<ButtonPress-1>",   self._start_resize)
        grip.bind("<B1-Motion>",       self._do_resize)

    def _darken(self, hex_color):
        """16進カラーを少し暗くする"""
        try:
            r = int(hex_color[1:3], 16)
            g = int(hex_color[3:5], 16)
            b = int(hex_color[5:7], 16)
            return f"#{max(0,r-30):02x}{max(0,g-30):02x}{max(0,b-30):02x}"
        except Exception:
            return hex_color

    def _start_move(self, e):
        self._drag_x = e.x
        self._drag_y = e.y

    def _do_move(self, e):
        dx = e.x - self._drag_x
        dy = e.y - self._drag_y
        x = self.win.winfo_x() + dx
        y = self.win.winfo_y() + dy
        self.win.geometry(f"+{x}+{y}")

    def _end_move(self, _e):
        self._data["x"] = self.win.winfo_x()
        self._data["y"] = self.win.winfo_y()
        self._on_save()

    def _start_resize(self, e):
        self._rx = e.x_root
        self._ry = e.y_root
        self._rw = self.win.winfo_width()
        self._rh = self.win.winfo_height()

    def _do_resize(self, e):
        dw = e.x_root - self._rx
        dh = e.y_root - self._ry
        nw = max(150, self._rw + dw)
        nh = max(100, self._rh + dh)
        self.win.geometry(f"{nw}x{nh}")
        self._data["w"] = nw
        self._data["h"] = nh

    def _on_modified(self, _=None):
        self._data["content"] = self.text.get("1.0", tk.END).rstrip()
        self.text.edit_modified(False)
        self._on_save()

    def _hide(self):
        self._data["visible"] = False
        self.win.withdraw()
        self._on_save()

    def _menu(self):
        m = tk.Menu(self.win, tearoff=False, bg="#252526", fg="#c9d1d9")
        m.add_command(label="🗑 削除", command=self._delete)
        m.add_separator()
        for name, hex_c in NOTE_COLORS.items():
            m.add_command(label=f"🎨 {name}",
                           command=lambda c=hex_c: self._change_color(c))
        pin_label = "📌 ピン留め解除" if self._data.get("topmost") else "📌 ピン留め"
        m.add_command(label=pin_label, command=self._toggle_topmost)
        m.post(self.win.winfo_rootx() + self.win.winfo_width() - 60,
               self.win.winfo_rooty() + 22)

    def _delete(self):
        self._data["deleted"] = True
        self.win.destroy()
        self._on_delete(self._data["id"])

    def _change_color(self, color):
        self._data["color"] = color
        self.win.configure(bg=color)
        self.text.configure(bg=color)
        self._on_save()

    def _toggle_topmost(self):
        val = not self._data.get("topmost", False)
        self._data["topmost"] = val
        self.win.attributes("-topmost", val)
        self._on_save()


class App087:
    """デスクトップ付箋アプリ"""

    def __init__(self, root):
        self.root = root
        self.root.title("デスクトップ付箋アプリ")
        self.root.geometry("320x440")
        self.root.configure(bg="#1e1e1e")
        self._notes   = []   # note data dicts
        self._windows = {}   # {note_id: StickyNote}
        self._next_id = 1
        self._build_ui()
        self._load()

    def _build_ui(self):
        header = tk.Frame(self.root, bg="#252526", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="📌 デスクトップ付箋アプリ",
                 font=("Noto Sans JP", 11, "bold"),
                 bg="#252526", fg="#4fc3f7").pack(side=tk.LEFT, padx=12)

        tb = tk.Frame(self.root, bg="#2d2d2d", pady=4)
        tb.pack(fill=tk.X)
        for color_name, hex_c in NOTE_COLORS.items():
            btn = tk.Button(tb, text=color_name, bg=hex_c, fg="#333",
                             relief=tk.FLAT, font=("Arial", 8), padx=4, pady=3,
                             activebackground=hex_c, bd=1,
                             command=lambda c=hex_c: self._new_note(c))
            btn.pack(side=tk.LEFT, padx=1)

        # 付箋一覧
        tk.Label(self.root, text="付箋一覧", bg="#1e1e1e", fg="#888",
                 font=("Arial", 9)).pack(anchor="w", padx=8, pady=(4, 0))
        cols = ("color", "preview", "updated")
        self.tree = ttk.Treeview(self.root, columns=cols, show="headings",
                                  selectmode="browse")
        self.tree.heading("color",   text="色")
        self.tree.heading("preview", text="内容")
        self.tree.heading("updated", text="更新")
        self.tree.column("color",   width=40,  anchor="center")
        self.tree.column("preview", width=160, anchor="w")
        self.tree.column("updated", width=80,  anchor="center")
        tsb = ttk.Scrollbar(self.root, command=self.tree.yview)
        self.tree.configure(yscrollcommand=tsb.set)
        tsb.pack(side=tk.RIGHT, fill=tk.Y)
        self.tree.pack(fill=tk.BOTH, expand=True, padx=(8, 0))
        self.tree.bind("<<TreeviewSelect>>", self._on_select)
        self.tree.bind("<Double-1>", self._show_selected)

        btn_row = tk.Frame(self.root, bg="#1e1e1e")
        btn_row.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_row, text="表示", command=self._show_selected).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全表示", command=self._show_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全非表示", command=self._hide_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="削除", command=self._delete_selected).pack(side=tk.RIGHT, padx=2)

        self.status_var = tk.StringVar(value="付箋を追加してください")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#252526", fg="#858585", font=("Arial", 9),
                 anchor="w", padx=8).pack(fill=tk.X, side=tk.BOTTOM)

    def _new_note(self, color="#fffde7"):
        note = {
            "id":      self._next_id,
            "content": "",
            "color":   color,
            "x":       200 + self._next_id * 30,
            "y":       100 + self._next_id * 30,
            "w":       220,
            "h":       180,
            "visible": True,
            "topmost": False,
            "updated": datetime.now().strftime("%m/%d %H:%M"),
        }
        self._next_id += 1
        self._notes.append(note)
        self._open_window(note)
        self._refresh_list()
        self._save()

    def _open_window(self, note):
        note_id = note["id"]
        if note_id in self._windows:
            self._windows[note_id].win.deiconify()
            return
        sn = StickyNote(
            self.root, note,
            on_save=self._on_note_save,
            on_delete=self._on_note_delete)
        self._windows[note_id] = sn

    def _on_note_save(self):
        for n in self._notes:
            n["updated"] = datetime.now().strftime("%m/%d %H:%M")
        self._refresh_list()
        self._save()

    def _on_note_delete(self, note_id):
        self._notes = [n for n in self._notes if n["id"] != note_id]
        if note_id in self._windows:
            del self._windows[note_id]
        self._refresh_list()
        self._save()

    def _refresh_list(self):
        self.tree.delete(*self.tree.get_children())
        for n in self._notes:
            preview = n.get("content", "")[:30].replace("\n", " ")
            self.tree.insert("", tk.END, text=n["id"],
                              values=("●", preview, n.get("updated", "")))
        self.status_var.set(f"付箋: {len(self._notes)} 枚")

    def _on_select(self, _=None):
        pass

    def _show_selected(self, _=None):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        note = next((n for n in self._notes if n["id"] == note_id), None)
        if note:
            note["visible"] = True
            self._open_window(note)
            if note_id in self._windows:
                self._windows[note_id].win.deiconify()

    def _show_all(self):
        for n in self._notes:
            n["visible"] = True
            self._open_window(n)
            if n["id"] in self._windows:
                self._windows[n["id"]].win.deiconify()

    def _hide_all(self):
        for sn in self._windows.values():
            sn.win.withdraw()

    def _delete_selected(self):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        if note_id in self._windows:
            self._windows[note_id].win.destroy()
            del self._windows[note_id]
        self._notes = [n for n in self._notes if n["id"] != note_id]
        self._refresh_list()
        self._save()

    def _save(self):
        try:
            with open(SAVE_PATH, "w", encoding="utf-8") as f:
                json.dump({"next_id": self._next_id,
                           "notes": self._notes}, f,
                          ensure_ascii=False, indent=2)
        except Exception:
            pass

    def _load(self):
        if not os.path.exists(SAVE_PATH):
            return
        try:
            with open(SAVE_PATH, encoding="utf-8") as f:
                data = json.load(f)
            self._next_id = data.get("next_id", 1)
            self._notes   = data.get("notes", [])
            for n in self._notes:
                if n.get("visible", True):
                    self._open_window(n)
            self._refresh_list()
        except Exception:
            pass


if __name__ == "__main__":
    root = tk.Tk()
    app = App087(root)
    root.mainloop()

イベント処理

ボタンのcommand引数でクリックイベントを、bind('')でEnterキーイベントを処理します。どちらの操作でも同じprocess()が呼ばれ、コードの重複を避けられます。

import tkinter as tk
from tkinter import ttk, messagebox, colorchooser
import json
import os
from datetime import datetime

SAVE_PATH = os.path.join(os.path.dirname(__file__), "sticky_notes.json")

NOTE_COLORS = {
    "黄":  "#fffde7",
    "青":  "#e3f2fd",
    "緑":  "#e8f5e9",
    "ピンク": "#fce4ec",
    "紫":  "#f3e5f5",
    "白":  "#ffffff",
}


class StickyNote:
    """個別の付箋ウィンドウ"""

    def __init__(self, master, note_data, on_save, on_delete):
        self._data = note_data
        self._on_save   = on_save
        self._on_delete = on_delete

        color = note_data.get("color", "#fffde7")
        x = note_data.get("x", 100)
        y = note_data.get("y", 100)
        w = note_data.get("w", 220)
        h = note_data.get("h", 180)

        self.win = tk.Toplevel(master)
        self.win.overrideredirect(True)   # タイトルバー非表示
        self.win.geometry(f"{w}x{h}+{x}+{y}")
        self.win.configure(bg=color)
        self.win.attributes("-topmost", note_data.get("topmost", False))

        # タイトルバー代替
        title_bar = tk.Frame(self.win, bg=self._darken(color), height=22)
        title_bar.pack(fill=tk.X)
        title_bar.pack_propagate(False)

        tk.Label(title_bar, text="📌", bg=self._darken(color),
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=4)

        tk.Button(title_bar, text="✕", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._hide).pack(side=tk.RIGHT, padx=2)
        tk.Button(title_bar, text="⋮", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._menu).pack(side=tk.RIGHT, padx=2)

        # ドラッグ移動
        self._drag_x = self._drag_y = 0
        title_bar.bind("<ButtonPress-1>",   self._start_move)
        title_bar.bind("<B1-Motion>",       self._do_move)
        title_bar.bind("<ButtonRelease-1>", self._end_move)

        # テキストエリア
        self.text = tk.Text(self.win, bg=color, fg="#333",
                             font=("Noto Sans JP", 10), relief=tk.FLAT,
                             insertbackground="#333", wrap=tk.WORD,
                             padx=4, pady=2, undo=True)
        self.text.pack(fill=tk.BOTH, expand=True)
        self.text.insert("1.0", note_data.get("content", ""))
        self.text.bind("<<Modified>>", self._on_modified)

        # リサイズグリップ
        grip = tk.Label(self.win, text="◢", bg=self._darken(color),
                         fg="#999", font=("Arial", 7), cursor="size_nw_se")
        grip.place(relx=1.0, rely=1.0, anchor="se")
        grip.bind("<ButtonPress-1>",   self._start_resize)
        grip.bind("<B1-Motion>",       self._do_resize)

    def _darken(self, hex_color):
        """16進カラーを少し暗くする"""
        try:
            r = int(hex_color[1:3], 16)
            g = int(hex_color[3:5], 16)
            b = int(hex_color[5:7], 16)
            return f"#{max(0,r-30):02x}{max(0,g-30):02x}{max(0,b-30):02x}"
        except Exception:
            return hex_color

    def _start_move(self, e):
        self._drag_x = e.x
        self._drag_y = e.y

    def _do_move(self, e):
        dx = e.x - self._drag_x
        dy = e.y - self._drag_y
        x = self.win.winfo_x() + dx
        y = self.win.winfo_y() + dy
        self.win.geometry(f"+{x}+{y}")

    def _end_move(self, _e):
        self._data["x"] = self.win.winfo_x()
        self._data["y"] = self.win.winfo_y()
        self._on_save()

    def _start_resize(self, e):
        self._rx = e.x_root
        self._ry = e.y_root
        self._rw = self.win.winfo_width()
        self._rh = self.win.winfo_height()

    def _do_resize(self, e):
        dw = e.x_root - self._rx
        dh = e.y_root - self._ry
        nw = max(150, self._rw + dw)
        nh = max(100, self._rh + dh)
        self.win.geometry(f"{nw}x{nh}")
        self._data["w"] = nw
        self._data["h"] = nh

    def _on_modified(self, _=None):
        self._data["content"] = self.text.get("1.0", tk.END).rstrip()
        self.text.edit_modified(False)
        self._on_save()

    def _hide(self):
        self._data["visible"] = False
        self.win.withdraw()
        self._on_save()

    def _menu(self):
        m = tk.Menu(self.win, tearoff=False, bg="#252526", fg="#c9d1d9")
        m.add_command(label="🗑 削除", command=self._delete)
        m.add_separator()
        for name, hex_c in NOTE_COLORS.items():
            m.add_command(label=f"🎨 {name}",
                           command=lambda c=hex_c: self._change_color(c))
        pin_label = "📌 ピン留め解除" if self._data.get("topmost") else "📌 ピン留め"
        m.add_command(label=pin_label, command=self._toggle_topmost)
        m.post(self.win.winfo_rootx() + self.win.winfo_width() - 60,
               self.win.winfo_rooty() + 22)

    def _delete(self):
        self._data["deleted"] = True
        self.win.destroy()
        self._on_delete(self._data["id"])

    def _change_color(self, color):
        self._data["color"] = color
        self.win.configure(bg=color)
        self.text.configure(bg=color)
        self._on_save()

    def _toggle_topmost(self):
        val = not self._data.get("topmost", False)
        self._data["topmost"] = val
        self.win.attributes("-topmost", val)
        self._on_save()


class App087:
    """デスクトップ付箋アプリ"""

    def __init__(self, root):
        self.root = root
        self.root.title("デスクトップ付箋アプリ")
        self.root.geometry("320x440")
        self.root.configure(bg="#1e1e1e")
        self._notes   = []   # note data dicts
        self._windows = {}   # {note_id: StickyNote}
        self._next_id = 1
        self._build_ui()
        self._load()

    def _build_ui(self):
        header = tk.Frame(self.root, bg="#252526", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="📌 デスクトップ付箋アプリ",
                 font=("Noto Sans JP", 11, "bold"),
                 bg="#252526", fg="#4fc3f7").pack(side=tk.LEFT, padx=12)

        tb = tk.Frame(self.root, bg="#2d2d2d", pady=4)
        tb.pack(fill=tk.X)
        for color_name, hex_c in NOTE_COLORS.items():
            btn = tk.Button(tb, text=color_name, bg=hex_c, fg="#333",
                             relief=tk.FLAT, font=("Arial", 8), padx=4, pady=3,
                             activebackground=hex_c, bd=1,
                             command=lambda c=hex_c: self._new_note(c))
            btn.pack(side=tk.LEFT, padx=1)

        # 付箋一覧
        tk.Label(self.root, text="付箋一覧", bg="#1e1e1e", fg="#888",
                 font=("Arial", 9)).pack(anchor="w", padx=8, pady=(4, 0))
        cols = ("color", "preview", "updated")
        self.tree = ttk.Treeview(self.root, columns=cols, show="headings",
                                  selectmode="browse")
        self.tree.heading("color",   text="色")
        self.tree.heading("preview", text="内容")
        self.tree.heading("updated", text="更新")
        self.tree.column("color",   width=40,  anchor="center")
        self.tree.column("preview", width=160, anchor="w")
        self.tree.column("updated", width=80,  anchor="center")
        tsb = ttk.Scrollbar(self.root, command=self.tree.yview)
        self.tree.configure(yscrollcommand=tsb.set)
        tsb.pack(side=tk.RIGHT, fill=tk.Y)
        self.tree.pack(fill=tk.BOTH, expand=True, padx=(8, 0))
        self.tree.bind("<<TreeviewSelect>>", self._on_select)
        self.tree.bind("<Double-1>", self._show_selected)

        btn_row = tk.Frame(self.root, bg="#1e1e1e")
        btn_row.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_row, text="表示", command=self._show_selected).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全表示", command=self._show_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全非表示", command=self._hide_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="削除", command=self._delete_selected).pack(side=tk.RIGHT, padx=2)

        self.status_var = tk.StringVar(value="付箋を追加してください")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#252526", fg="#858585", font=("Arial", 9),
                 anchor="w", padx=8).pack(fill=tk.X, side=tk.BOTTOM)

    def _new_note(self, color="#fffde7"):
        note = {
            "id":      self._next_id,
            "content": "",
            "color":   color,
            "x":       200 + self._next_id * 30,
            "y":       100 + self._next_id * 30,
            "w":       220,
            "h":       180,
            "visible": True,
            "topmost": False,
            "updated": datetime.now().strftime("%m/%d %H:%M"),
        }
        self._next_id += 1
        self._notes.append(note)
        self._open_window(note)
        self._refresh_list()
        self._save()

    def _open_window(self, note):
        note_id = note["id"]
        if note_id in self._windows:
            self._windows[note_id].win.deiconify()
            return
        sn = StickyNote(
            self.root, note,
            on_save=self._on_note_save,
            on_delete=self._on_note_delete)
        self._windows[note_id] = sn

    def _on_note_save(self):
        for n in self._notes:
            n["updated"] = datetime.now().strftime("%m/%d %H:%M")
        self._refresh_list()
        self._save()

    def _on_note_delete(self, note_id):
        self._notes = [n for n in self._notes if n["id"] != note_id]
        if note_id in self._windows:
            del self._windows[note_id]
        self._refresh_list()
        self._save()

    def _refresh_list(self):
        self.tree.delete(*self.tree.get_children())
        for n in self._notes:
            preview = n.get("content", "")[:30].replace("\n", " ")
            self.tree.insert("", tk.END, text=n["id"],
                              values=("●", preview, n.get("updated", "")))
        self.status_var.set(f"付箋: {len(self._notes)} 枚")

    def _on_select(self, _=None):
        pass

    def _show_selected(self, _=None):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        note = next((n for n in self._notes if n["id"] == note_id), None)
        if note:
            note["visible"] = True
            self._open_window(note)
            if note_id in self._windows:
                self._windows[note_id].win.deiconify()

    def _show_all(self):
        for n in self._notes:
            n["visible"] = True
            self._open_window(n)
            if n["id"] in self._windows:
                self._windows[n["id"]].win.deiconify()

    def _hide_all(self):
        for sn in self._windows.values():
            sn.win.withdraw()

    def _delete_selected(self):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        if note_id in self._windows:
            self._windows[note_id].win.destroy()
            del self._windows[note_id]
        self._notes = [n for n in self._notes if n["id"] != note_id]
        self._refresh_list()
        self._save()

    def _save(self):
        try:
            with open(SAVE_PATH, "w", encoding="utf-8") as f:
                json.dump({"next_id": self._next_id,
                           "notes": self._notes}, f,
                          ensure_ascii=False, indent=2)
        except Exception:
            pass

    def _load(self):
        if not os.path.exists(SAVE_PATH):
            return
        try:
            with open(SAVE_PATH, encoding="utf-8") as f:
                data = json.load(f)
            self._next_id = data.get("next_id", 1)
            self._notes   = data.get("notes", [])
            for n in self._notes:
                if n.get("visible", True):
                    self._open_window(n)
            self._refresh_list()
        except Exception:
            pass


if __name__ == "__main__":
    root = tk.Tk()
    app = App087(root)
    root.mainloop()

Textウィジェットでの結果表示

tk.Textウィジェットをstate=DISABLED(読み取り専用)で作成し、更新時はNORMALに変更してinsert()で内容を書き込み、再びDISABLEDに戻します。

import tkinter as tk
from tkinter import ttk, messagebox, colorchooser
import json
import os
from datetime import datetime

SAVE_PATH = os.path.join(os.path.dirname(__file__), "sticky_notes.json")

NOTE_COLORS = {
    "黄":  "#fffde7",
    "青":  "#e3f2fd",
    "緑":  "#e8f5e9",
    "ピンク": "#fce4ec",
    "紫":  "#f3e5f5",
    "白":  "#ffffff",
}


class StickyNote:
    """個別の付箋ウィンドウ"""

    def __init__(self, master, note_data, on_save, on_delete):
        self._data = note_data
        self._on_save   = on_save
        self._on_delete = on_delete

        color = note_data.get("color", "#fffde7")
        x = note_data.get("x", 100)
        y = note_data.get("y", 100)
        w = note_data.get("w", 220)
        h = note_data.get("h", 180)

        self.win = tk.Toplevel(master)
        self.win.overrideredirect(True)   # タイトルバー非表示
        self.win.geometry(f"{w}x{h}+{x}+{y}")
        self.win.configure(bg=color)
        self.win.attributes("-topmost", note_data.get("topmost", False))

        # タイトルバー代替
        title_bar = tk.Frame(self.win, bg=self._darken(color), height=22)
        title_bar.pack(fill=tk.X)
        title_bar.pack_propagate(False)

        tk.Label(title_bar, text="📌", bg=self._darken(color),
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=4)

        tk.Button(title_bar, text="✕", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._hide).pack(side=tk.RIGHT, padx=2)
        tk.Button(title_bar, text="⋮", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._menu).pack(side=tk.RIGHT, padx=2)

        # ドラッグ移動
        self._drag_x = self._drag_y = 0
        title_bar.bind("<ButtonPress-1>",   self._start_move)
        title_bar.bind("<B1-Motion>",       self._do_move)
        title_bar.bind("<ButtonRelease-1>", self._end_move)

        # テキストエリア
        self.text = tk.Text(self.win, bg=color, fg="#333",
                             font=("Noto Sans JP", 10), relief=tk.FLAT,
                             insertbackground="#333", wrap=tk.WORD,
                             padx=4, pady=2, undo=True)
        self.text.pack(fill=tk.BOTH, expand=True)
        self.text.insert("1.0", note_data.get("content", ""))
        self.text.bind("<<Modified>>", self._on_modified)

        # リサイズグリップ
        grip = tk.Label(self.win, text="◢", bg=self._darken(color),
                         fg="#999", font=("Arial", 7), cursor="size_nw_se")
        grip.place(relx=1.0, rely=1.0, anchor="se")
        grip.bind("<ButtonPress-1>",   self._start_resize)
        grip.bind("<B1-Motion>",       self._do_resize)

    def _darken(self, hex_color):
        """16進カラーを少し暗くする"""
        try:
            r = int(hex_color[1:3], 16)
            g = int(hex_color[3:5], 16)
            b = int(hex_color[5:7], 16)
            return f"#{max(0,r-30):02x}{max(0,g-30):02x}{max(0,b-30):02x}"
        except Exception:
            return hex_color

    def _start_move(self, e):
        self._drag_x = e.x
        self._drag_y = e.y

    def _do_move(self, e):
        dx = e.x - self._drag_x
        dy = e.y - self._drag_y
        x = self.win.winfo_x() + dx
        y = self.win.winfo_y() + dy
        self.win.geometry(f"+{x}+{y}")

    def _end_move(self, _e):
        self._data["x"] = self.win.winfo_x()
        self._data["y"] = self.win.winfo_y()
        self._on_save()

    def _start_resize(self, e):
        self._rx = e.x_root
        self._ry = e.y_root
        self._rw = self.win.winfo_width()
        self._rh = self.win.winfo_height()

    def _do_resize(self, e):
        dw = e.x_root - self._rx
        dh = e.y_root - self._ry
        nw = max(150, self._rw + dw)
        nh = max(100, self._rh + dh)
        self.win.geometry(f"{nw}x{nh}")
        self._data["w"] = nw
        self._data["h"] = nh

    def _on_modified(self, _=None):
        self._data["content"] = self.text.get("1.0", tk.END).rstrip()
        self.text.edit_modified(False)
        self._on_save()

    def _hide(self):
        self._data["visible"] = False
        self.win.withdraw()
        self._on_save()

    def _menu(self):
        m = tk.Menu(self.win, tearoff=False, bg="#252526", fg="#c9d1d9")
        m.add_command(label="🗑 削除", command=self._delete)
        m.add_separator()
        for name, hex_c in NOTE_COLORS.items():
            m.add_command(label=f"🎨 {name}",
                           command=lambda c=hex_c: self._change_color(c))
        pin_label = "📌 ピン留め解除" if self._data.get("topmost") else "📌 ピン留め"
        m.add_command(label=pin_label, command=self._toggle_topmost)
        m.post(self.win.winfo_rootx() + self.win.winfo_width() - 60,
               self.win.winfo_rooty() + 22)

    def _delete(self):
        self._data["deleted"] = True
        self.win.destroy()
        self._on_delete(self._data["id"])

    def _change_color(self, color):
        self._data["color"] = color
        self.win.configure(bg=color)
        self.text.configure(bg=color)
        self._on_save()

    def _toggle_topmost(self):
        val = not self._data.get("topmost", False)
        self._data["topmost"] = val
        self.win.attributes("-topmost", val)
        self._on_save()


class App087:
    """デスクトップ付箋アプリ"""

    def __init__(self, root):
        self.root = root
        self.root.title("デスクトップ付箋アプリ")
        self.root.geometry("320x440")
        self.root.configure(bg="#1e1e1e")
        self._notes   = []   # note data dicts
        self._windows = {}   # {note_id: StickyNote}
        self._next_id = 1
        self._build_ui()
        self._load()

    def _build_ui(self):
        header = tk.Frame(self.root, bg="#252526", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="📌 デスクトップ付箋アプリ",
                 font=("Noto Sans JP", 11, "bold"),
                 bg="#252526", fg="#4fc3f7").pack(side=tk.LEFT, padx=12)

        tb = tk.Frame(self.root, bg="#2d2d2d", pady=4)
        tb.pack(fill=tk.X)
        for color_name, hex_c in NOTE_COLORS.items():
            btn = tk.Button(tb, text=color_name, bg=hex_c, fg="#333",
                             relief=tk.FLAT, font=("Arial", 8), padx=4, pady=3,
                             activebackground=hex_c, bd=1,
                             command=lambda c=hex_c: self._new_note(c))
            btn.pack(side=tk.LEFT, padx=1)

        # 付箋一覧
        tk.Label(self.root, text="付箋一覧", bg="#1e1e1e", fg="#888",
                 font=("Arial", 9)).pack(anchor="w", padx=8, pady=(4, 0))
        cols = ("color", "preview", "updated")
        self.tree = ttk.Treeview(self.root, columns=cols, show="headings",
                                  selectmode="browse")
        self.tree.heading("color",   text="色")
        self.tree.heading("preview", text="内容")
        self.tree.heading("updated", text="更新")
        self.tree.column("color",   width=40,  anchor="center")
        self.tree.column("preview", width=160, anchor="w")
        self.tree.column("updated", width=80,  anchor="center")
        tsb = ttk.Scrollbar(self.root, command=self.tree.yview)
        self.tree.configure(yscrollcommand=tsb.set)
        tsb.pack(side=tk.RIGHT, fill=tk.Y)
        self.tree.pack(fill=tk.BOTH, expand=True, padx=(8, 0))
        self.tree.bind("<<TreeviewSelect>>", self._on_select)
        self.tree.bind("<Double-1>", self._show_selected)

        btn_row = tk.Frame(self.root, bg="#1e1e1e")
        btn_row.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_row, text="表示", command=self._show_selected).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全表示", command=self._show_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全非表示", command=self._hide_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="削除", command=self._delete_selected).pack(side=tk.RIGHT, padx=2)

        self.status_var = tk.StringVar(value="付箋を追加してください")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#252526", fg="#858585", font=("Arial", 9),
                 anchor="w", padx=8).pack(fill=tk.X, side=tk.BOTTOM)

    def _new_note(self, color="#fffde7"):
        note = {
            "id":      self._next_id,
            "content": "",
            "color":   color,
            "x":       200 + self._next_id * 30,
            "y":       100 + self._next_id * 30,
            "w":       220,
            "h":       180,
            "visible": True,
            "topmost": False,
            "updated": datetime.now().strftime("%m/%d %H:%M"),
        }
        self._next_id += 1
        self._notes.append(note)
        self._open_window(note)
        self._refresh_list()
        self._save()

    def _open_window(self, note):
        note_id = note["id"]
        if note_id in self._windows:
            self._windows[note_id].win.deiconify()
            return
        sn = StickyNote(
            self.root, note,
            on_save=self._on_note_save,
            on_delete=self._on_note_delete)
        self._windows[note_id] = sn

    def _on_note_save(self):
        for n in self._notes:
            n["updated"] = datetime.now().strftime("%m/%d %H:%M")
        self._refresh_list()
        self._save()

    def _on_note_delete(self, note_id):
        self._notes = [n for n in self._notes if n["id"] != note_id]
        if note_id in self._windows:
            del self._windows[note_id]
        self._refresh_list()
        self._save()

    def _refresh_list(self):
        self.tree.delete(*self.tree.get_children())
        for n in self._notes:
            preview = n.get("content", "")[:30].replace("\n", " ")
            self.tree.insert("", tk.END, text=n["id"],
                              values=("●", preview, n.get("updated", "")))
        self.status_var.set(f"付箋: {len(self._notes)} 枚")

    def _on_select(self, _=None):
        pass

    def _show_selected(self, _=None):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        note = next((n for n in self._notes if n["id"] == note_id), None)
        if note:
            note["visible"] = True
            self._open_window(note)
            if note_id in self._windows:
                self._windows[note_id].win.deiconify()

    def _show_all(self):
        for n in self._notes:
            n["visible"] = True
            self._open_window(n)
            if n["id"] in self._windows:
                self._windows[n["id"]].win.deiconify()

    def _hide_all(self):
        for sn in self._windows.values():
            sn.win.withdraw()

    def _delete_selected(self):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        if note_id in self._windows:
            self._windows[note_id].win.destroy()
            del self._windows[note_id]
        self._notes = [n for n in self._notes if n["id"] != note_id]
        self._refresh_list()
        self._save()

    def _save(self):
        try:
            with open(SAVE_PATH, "w", encoding="utf-8") as f:
                json.dump({"next_id": self._next_id,
                           "notes": self._notes}, f,
                          ensure_ascii=False, indent=2)
        except Exception:
            pass

    def _load(self):
        if not os.path.exists(SAVE_PATH):
            return
        try:
            with open(SAVE_PATH, encoding="utf-8") as f:
                data = json.load(f)
            self._next_id = data.get("next_id", 1)
            self._notes   = data.get("notes", [])
            for n in self._notes:
                if n.get("visible", True):
                    self._open_window(n)
            self._refresh_list()
        except Exception:
            pass


if __name__ == "__main__":
    root = tk.Tk()
    app = App087(root)
    root.mainloop()

例外処理とエラーハンドリング

try-exceptでValueErrorとExceptionを捕捉し、messagebox.showerror()でエラーメッセージを表示します。予期しないエラーも処理することで、アプリの堅牢性が向上します。

import tkinter as tk
from tkinter import ttk, messagebox, colorchooser
import json
import os
from datetime import datetime

SAVE_PATH = os.path.join(os.path.dirname(__file__), "sticky_notes.json")

NOTE_COLORS = {
    "黄":  "#fffde7",
    "青":  "#e3f2fd",
    "緑":  "#e8f5e9",
    "ピンク": "#fce4ec",
    "紫":  "#f3e5f5",
    "白":  "#ffffff",
}


class StickyNote:
    """個別の付箋ウィンドウ"""

    def __init__(self, master, note_data, on_save, on_delete):
        self._data = note_data
        self._on_save   = on_save
        self._on_delete = on_delete

        color = note_data.get("color", "#fffde7")
        x = note_data.get("x", 100)
        y = note_data.get("y", 100)
        w = note_data.get("w", 220)
        h = note_data.get("h", 180)

        self.win = tk.Toplevel(master)
        self.win.overrideredirect(True)   # タイトルバー非表示
        self.win.geometry(f"{w}x{h}+{x}+{y}")
        self.win.configure(bg=color)
        self.win.attributes("-topmost", note_data.get("topmost", False))

        # タイトルバー代替
        title_bar = tk.Frame(self.win, bg=self._darken(color), height=22)
        title_bar.pack(fill=tk.X)
        title_bar.pack_propagate(False)

        tk.Label(title_bar, text="📌", bg=self._darken(color),
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=4)

        tk.Button(title_bar, text="✕", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._hide).pack(side=tk.RIGHT, padx=2)
        tk.Button(title_bar, text="⋮", bg=self._darken(color), fg="#555",
                  relief=tk.FLAT, font=("Arial", 8), bd=0,
                  command=self._menu).pack(side=tk.RIGHT, padx=2)

        # ドラッグ移動
        self._drag_x = self._drag_y = 0
        title_bar.bind("<ButtonPress-1>",   self._start_move)
        title_bar.bind("<B1-Motion>",       self._do_move)
        title_bar.bind("<ButtonRelease-1>", self._end_move)

        # テキストエリア
        self.text = tk.Text(self.win, bg=color, fg="#333",
                             font=("Noto Sans JP", 10), relief=tk.FLAT,
                             insertbackground="#333", wrap=tk.WORD,
                             padx=4, pady=2, undo=True)
        self.text.pack(fill=tk.BOTH, expand=True)
        self.text.insert("1.0", note_data.get("content", ""))
        self.text.bind("<<Modified>>", self._on_modified)

        # リサイズグリップ
        grip = tk.Label(self.win, text="◢", bg=self._darken(color),
                         fg="#999", font=("Arial", 7), cursor="size_nw_se")
        grip.place(relx=1.0, rely=1.0, anchor="se")
        grip.bind("<ButtonPress-1>",   self._start_resize)
        grip.bind("<B1-Motion>",       self._do_resize)

    def _darken(self, hex_color):
        """16進カラーを少し暗くする"""
        try:
            r = int(hex_color[1:3], 16)
            g = int(hex_color[3:5], 16)
            b = int(hex_color[5:7], 16)
            return f"#{max(0,r-30):02x}{max(0,g-30):02x}{max(0,b-30):02x}"
        except Exception:
            return hex_color

    def _start_move(self, e):
        self._drag_x = e.x
        self._drag_y = e.y

    def _do_move(self, e):
        dx = e.x - self._drag_x
        dy = e.y - self._drag_y
        x = self.win.winfo_x() + dx
        y = self.win.winfo_y() + dy
        self.win.geometry(f"+{x}+{y}")

    def _end_move(self, _e):
        self._data["x"] = self.win.winfo_x()
        self._data["y"] = self.win.winfo_y()
        self._on_save()

    def _start_resize(self, e):
        self._rx = e.x_root
        self._ry = e.y_root
        self._rw = self.win.winfo_width()
        self._rh = self.win.winfo_height()

    def _do_resize(self, e):
        dw = e.x_root - self._rx
        dh = e.y_root - self._ry
        nw = max(150, self._rw + dw)
        nh = max(100, self._rh + dh)
        self.win.geometry(f"{nw}x{nh}")
        self._data["w"] = nw
        self._data["h"] = nh

    def _on_modified(self, _=None):
        self._data["content"] = self.text.get("1.0", tk.END).rstrip()
        self.text.edit_modified(False)
        self._on_save()

    def _hide(self):
        self._data["visible"] = False
        self.win.withdraw()
        self._on_save()

    def _menu(self):
        m = tk.Menu(self.win, tearoff=False, bg="#252526", fg="#c9d1d9")
        m.add_command(label="🗑 削除", command=self._delete)
        m.add_separator()
        for name, hex_c in NOTE_COLORS.items():
            m.add_command(label=f"🎨 {name}",
                           command=lambda c=hex_c: self._change_color(c))
        pin_label = "📌 ピン留め解除" if self._data.get("topmost") else "📌 ピン留め"
        m.add_command(label=pin_label, command=self._toggle_topmost)
        m.post(self.win.winfo_rootx() + self.win.winfo_width() - 60,
               self.win.winfo_rooty() + 22)

    def _delete(self):
        self._data["deleted"] = True
        self.win.destroy()
        self._on_delete(self._data["id"])

    def _change_color(self, color):
        self._data["color"] = color
        self.win.configure(bg=color)
        self.text.configure(bg=color)
        self._on_save()

    def _toggle_topmost(self):
        val = not self._data.get("topmost", False)
        self._data["topmost"] = val
        self.win.attributes("-topmost", val)
        self._on_save()


class App087:
    """デスクトップ付箋アプリ"""

    def __init__(self, root):
        self.root = root
        self.root.title("デスクトップ付箋アプリ")
        self.root.geometry("320x440")
        self.root.configure(bg="#1e1e1e")
        self._notes   = []   # note data dicts
        self._windows = {}   # {note_id: StickyNote}
        self._next_id = 1
        self._build_ui()
        self._load()

    def _build_ui(self):
        header = tk.Frame(self.root, bg="#252526", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="📌 デスクトップ付箋アプリ",
                 font=("Noto Sans JP", 11, "bold"),
                 bg="#252526", fg="#4fc3f7").pack(side=tk.LEFT, padx=12)

        tb = tk.Frame(self.root, bg="#2d2d2d", pady=4)
        tb.pack(fill=tk.X)
        for color_name, hex_c in NOTE_COLORS.items():
            btn = tk.Button(tb, text=color_name, bg=hex_c, fg="#333",
                             relief=tk.FLAT, font=("Arial", 8), padx=4, pady=3,
                             activebackground=hex_c, bd=1,
                             command=lambda c=hex_c: self._new_note(c))
            btn.pack(side=tk.LEFT, padx=1)

        # 付箋一覧
        tk.Label(self.root, text="付箋一覧", bg="#1e1e1e", fg="#888",
                 font=("Arial", 9)).pack(anchor="w", padx=8, pady=(4, 0))
        cols = ("color", "preview", "updated")
        self.tree = ttk.Treeview(self.root, columns=cols, show="headings",
                                  selectmode="browse")
        self.tree.heading("color",   text="色")
        self.tree.heading("preview", text="内容")
        self.tree.heading("updated", text="更新")
        self.tree.column("color",   width=40,  anchor="center")
        self.tree.column("preview", width=160, anchor="w")
        self.tree.column("updated", width=80,  anchor="center")
        tsb = ttk.Scrollbar(self.root, command=self.tree.yview)
        self.tree.configure(yscrollcommand=tsb.set)
        tsb.pack(side=tk.RIGHT, fill=tk.Y)
        self.tree.pack(fill=tk.BOTH, expand=True, padx=(8, 0))
        self.tree.bind("<<TreeviewSelect>>", self._on_select)
        self.tree.bind("<Double-1>", self._show_selected)

        btn_row = tk.Frame(self.root, bg="#1e1e1e")
        btn_row.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_row, text="表示", command=self._show_selected).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全表示", command=self._show_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="全非表示", command=self._hide_all).pack(side=tk.LEFT, padx=2)
        ttk.Button(btn_row, text="削除", command=self._delete_selected).pack(side=tk.RIGHT, padx=2)

        self.status_var = tk.StringVar(value="付箋を追加してください")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#252526", fg="#858585", font=("Arial", 9),
                 anchor="w", padx=8).pack(fill=tk.X, side=tk.BOTTOM)

    def _new_note(self, color="#fffde7"):
        note = {
            "id":      self._next_id,
            "content": "",
            "color":   color,
            "x":       200 + self._next_id * 30,
            "y":       100 + self._next_id * 30,
            "w":       220,
            "h":       180,
            "visible": True,
            "topmost": False,
            "updated": datetime.now().strftime("%m/%d %H:%M"),
        }
        self._next_id += 1
        self._notes.append(note)
        self._open_window(note)
        self._refresh_list()
        self._save()

    def _open_window(self, note):
        note_id = note["id"]
        if note_id in self._windows:
            self._windows[note_id].win.deiconify()
            return
        sn = StickyNote(
            self.root, note,
            on_save=self._on_note_save,
            on_delete=self._on_note_delete)
        self._windows[note_id] = sn

    def _on_note_save(self):
        for n in self._notes:
            n["updated"] = datetime.now().strftime("%m/%d %H:%M")
        self._refresh_list()
        self._save()

    def _on_note_delete(self, note_id):
        self._notes = [n for n in self._notes if n["id"] != note_id]
        if note_id in self._windows:
            del self._windows[note_id]
        self._refresh_list()
        self._save()

    def _refresh_list(self):
        self.tree.delete(*self.tree.get_children())
        for n in self._notes:
            preview = n.get("content", "")[:30].replace("\n", " ")
            self.tree.insert("", tk.END, text=n["id"],
                              values=("●", preview, n.get("updated", "")))
        self.status_var.set(f"付箋: {len(self._notes)} 枚")

    def _on_select(self, _=None):
        pass

    def _show_selected(self, _=None):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        note = next((n for n in self._notes if n["id"] == note_id), None)
        if note:
            note["visible"] = True
            self._open_window(note)
            if note_id in self._windows:
                self._windows[note_id].win.deiconify()

    def _show_all(self):
        for n in self._notes:
            n["visible"] = True
            self._open_window(n)
            if n["id"] in self._windows:
                self._windows[n["id"]].win.deiconify()

    def _hide_all(self):
        for sn in self._windows.values():
            sn.win.withdraw()

    def _delete_selected(self):
        sel = self.tree.selection()
        if not sel:
            return
        note_id = int(self.tree.item(sel[0], "text"))
        if note_id in self._windows:
            self._windows[note_id].win.destroy()
            del self._windows[note_id]
        self._notes = [n for n in self._notes if n["id"] != note_id]
        self._refresh_list()
        self._save()

    def _save(self):
        try:
            with open(SAVE_PATH, "w", encoding="utf-8") as f:
                json.dump({"next_id": self._next_id,
                           "notes": self._notes}, f,
                          ensure_ascii=False, indent=2)
        except Exception:
            pass

    def _load(self):
        if not os.path.exists(SAVE_PATH):
            return
        try:
            with open(SAVE_PATH, encoding="utf-8") as f:
                data = json.load(f)
            self._next_id = data.get("next_id", 1)
            self._notes   = data.get("notes", [])
            for n in self._notes:
                if n.get("visible", True):
                    self._open_window(n)
            self._refresh_list()
        except Exception:
            pass


if __name__ == "__main__":
    root = tk.Tk()
    app = App087(root)
    root.mainloop()

6. ステップバイステップガイド

このアプリをゼロから自分で作る手順を解説します。コードをコピーするだけでなく、実際に手順を追って自分で書いてみましょう。

  1. 1
    ファイルを作成する

    新しいファイルを作成して app087.py と保存します。

  2. 2
    クラスの骨格を作る

    App087クラスを定義し、__init__とmainloop()の最小構成を作ります。

  3. 3
    タイトルバーを作る

    Frameを使ってカラーバー付きのタイトルエリアを作ります。

  4. 4
    入力フォームを実装する

    LabelFrameとEntryウィジェットで入力エリアを作ります。

  5. 5
    処理ロジックを実装する

    _execute()メソッドにメインロジックを実装します。

  6. 6
    結果表示を実装する

    TextウィジェットかLabelに結果を表示する_show_result()を実装します。

  7. 7
    エラー処理を追加する

    try-exceptとmessageboxでエラーハンドリングを追加します。

7. カスタマイズアイデア

基本機能を習得したら、以下のカスタマイズに挑戦してみましょう。

💡 ダークモードを追加する

bg色・fg色を辞書で管理し、ボタン1つでダークモード・ライトモードを切り替えられるようにしましょう。

💡 データの保存機能

処理結果をCSV・TXTファイルに保存する機能を追加しましょう。filedialog.asksaveasfilename()でファイル保存ダイアログが使えます。

💡 設定ダイアログ

フォントサイズや色などの設定をユーザーが変更できるオプションダイアログを追加しましょう。

8. よくある問題と解決法

❌ 日本語フォントが表示されない

原因:システムに日本語フォントが見つからない場合があります。

解決法:font引数を省略するかシステムに合ったフォントを指定してください。

❌ ライブラリのインポートエラー

原因:必要なライブラリがインストールされていません。

解決法:pip install コマンドで必要なライブラリをインストールしてください。

❌ ウィンドウサイズが合わない

原因:画面解像度や表示スケールによって異なる場合があります。

解決法:root.geometry()で適切なサイズに調整してください。

9. 練習問題

アプリの理解を深めるための練習問題です。

  1. 課題1:機能拡張

    デスクトップ付箋アプリに新しい機能を1つ追加してみましょう。

  2. 課題2:UIの改善

    色・フォント・レイアウトを変更して、より使いやすいUIにカスタマイズしましょう。

  3. 課題3:保存機能の追加

    処理結果をファイルに保存する機能を追加しましょう。

🚀
次に挑戦するアプリ

このアプリをマスターしたら、次のアプリに挑戦しましょう。