中級者向け No.42

習慣トラッカー(分析付き)

毎日の習慣を記録してグラフで継続率・傾向を分析するアプリ。SQLiteとmatplotlibで本格的な分析を実装します。

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

1. アプリ概要

毎日の習慣を記録してグラフで継続率・傾向を分析するアプリ。SQLiteとmatplotlibで本格的な分析を実装します。

このアプリは中級カテゴリに分類される実践的なGUIアプリです。使用ライブラリは tkinter(標準ライブラリ)・matplotlib で、難易度は ★★☆ です。

Pythonでは tkinter を使うことで、クロスプラットフォームなGUIアプリを簡単に作成できます。このアプリを通じて、ウィジェットの配置・イベント処理・データ管理など、GUI開発の実践的なスキルを習得できます。

ソースコードは完全な動作状態で提供しており、コピーしてそのまま実行できます。まずは実行して動作を確認し、その後コードを読んで仕組みを理解していきましょう。カスタマイズセクションでは機能拡張のアイデアも紹介しています。

GUIアプリ開発は、プログラミングの楽しさを実感できる最も効果的な学習方法のひとつです。アプリを作ることで、変数・関数・クラス・イベント処理など、プログラミングの重要な概念が自然と身についていきます。このアプリをきっかけに、オリジナルアプリの開発にも挑戦してみてください。

2. 機能一覧

  • 習慣トラッカー(分析付き)のメイン機能
  • 直感的なGUIインターフェース
  • 入力値のバリデーション
  • エラーハンドリング
  • 結果の見やすい表示
  • キーボードショートカット対応

3. 事前準備・環境

ℹ️
動作確認環境

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

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

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

インストールが必要なライブラリ

pip install matplotlib

4. 完全なソースコード

💡
コードのコピー方法

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

app42.py
import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3
import os
from datetime import datetime, timedelta, date

try:
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    MPL_AVAILABLE = True
except ImportError:
    MPL_AVAILABLE = False


class App42:
    """習慣トラッカー(分析付き)"""

    DB_PATH = os.path.join(os.path.dirname(__file__), "habits.db")

    def __init__(self, root):
        self.root = root
        self.root.title("習慣トラッカー(分析付き)")
        self.root.geometry("1000x680")
        self.root.configure(bg="#f8f9fc")
        self._init_db()
        self._build_ui()
        self._refresh_habits()

    def _init_db(self):
        self.conn = sqlite3.connect(self.DB_PATH)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habits (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                description TEXT,
                color TEXT DEFAULT '#4caf50',
                created_at TEXT,
                active INTEGER DEFAULT 1
            )
        """)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habit_logs (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                habit_id INTEGER,
                log_date TEXT,
                completed INTEGER DEFAULT 1,
                note TEXT,
                UNIQUE(habit_id, log_date),
                FOREIGN KEY(habit_id) REFERENCES habits(id)
            )
        """)
        self.conn.commit()
        # サンプルデータ挿入
        if not self.conn.execute("SELECT 1 FROM habits").fetchone():
            samples = [("早起き", "7時前に起床", "#ff9800"),
                       ("運動", "30分以上の運動", "#2196f3"),
                       ("読書", "30分以上の読書", "#9c27b0"),
                       ("瞑想", "10分以上の瞑想", "#009688"),
                       ("日記", "日記を書く", "#f44336")]
            for name, desc, color in samples:
                self.conn.execute(
                    "INSERT INTO habits (name, description, color, created_at) "
                    "VALUES (?,?,?,?)",
                    (name, desc, color, date.today().isoformat()))
            self.conn.commit()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#388e3c", pady=8)
        header.pack(fill=tk.X)
        tk.Label(header, text="📈 習慣トラッカー",
                 font=("Noto Sans JP", 13, "bold"),
                 bg="#388e3c", fg="white").pack(side=tk.LEFT, padx=12)
        self.today_label = tk.Label(header,
                                     text=f"今日: {date.today().strftime('%Y年%m月%d日')}",
                                     bg="#388e3c", fg="#c8e6c9",
                                     font=("Arial", 10))
        self.today_label.pack(side=tk.RIGHT, padx=12)

        notebook = ttk.Notebook(self.root)
        notebook.pack(fill=tk.BOTH, expand=True, padx=6, pady=6)

        # 今日の記録タブ
        today_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(today_tab, text="📅 今日の記録")
        self._build_today_tab(today_tab)

        # カレンダービュータブ
        cal_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(cal_tab, text="📆 カレンダー")
        self._build_calendar_tab(cal_tab)

        # 分析タブ
        analysis_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(analysis_tab, text="📊 分析")
        self._build_analysis_tab(analysis_tab)

        # 習慣管理タブ
        manage_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(manage_tab, text="⚙ 習慣管理")
        self._build_manage_tab(manage_tab)

        self.status_var = tk.StringVar(value="習慣を記録しましょう!")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#dde", font=("Arial", 9), anchor="w", padx=8
                 ).pack(fill=tk.X, side=tk.BOTTOM)

    # ── 今日の記録タブ ────────────────────────────────────────────

    def _build_today_tab(self, parent):
        # 日付選択
        date_f = tk.Frame(parent, bg="#f8f9fc")
        date_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(date_f, text="日付:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.log_date_var = tk.StringVar(value=date.today().isoformat())
        ttk.Entry(date_f, textvariable=self.log_date_var,
                  width=12).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="今日",
                   command=lambda: (
                       self.log_date_var.set(date.today().isoformat()),
                       self._refresh_today())).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="< 前日",
                   command=self._prev_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="翌日 >",
                   command=self._next_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="🔄 更新",
                   command=self._refresh_today).pack(side=tk.LEFT, padx=4)

        # 習慣チェックリスト
        list_f = ttk.LabelFrame(parent, text="習慣チェックリスト", padding=8)
        list_f.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)

        canvas = tk.Canvas(list_f, bg="#f8f9fc", highlightthickness=0)
        sb = ttk.Scrollbar(list_f, orient=tk.VERTICAL, command=canvas.yview)
        canvas.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        canvas.pack(fill=tk.BOTH, expand=True)

        self.check_frame = tk.Frame(canvas, bg="#f8f9fc")
        canvas.create_window((0, 0), window=self.check_frame, anchor="nw")
        self.check_frame.bind("<Configure>",
                              lambda e: canvas.configure(
                                  scrollregion=canvas.bbox("all")))

        # 今日の進捗
        prog_f = tk.Frame(parent, bg="#f8f9fc")
        prog_f.pack(fill=tk.X, padx=8, pady=4)
        self.progress_label = tk.Label(prog_f, text="",
                                        bg="#f8f9fc", font=("Arial", 11, "bold"))
        self.progress_label.pack(side=tk.LEFT)
        self.day_progress = ttk.Progressbar(prog_f, length=300,
                                             maximum=100, mode="determinate")
        self.day_progress.pack(side=tk.LEFT, padx=8)

        self._check_vars = {}

    def _refresh_today(self):
        log_date = self.log_date_var.get()
        for widget in self.check_frame.winfo_children():
            widget.destroy()
        self._check_vars.clear()

        habits = self.conn.execute(
            "SELECT id, name, description, color FROM habits WHERE active=1 "
            "ORDER BY id").fetchall()

        for habit_id, name, desc, color in habits:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date)).fetchone()
            done = bool(row and row[0]) if row else False

            var = tk.BooleanVar(value=done)
            self._check_vars[habit_id] = var

            card = tk.Frame(self.check_frame, bg="white", relief=tk.FLAT, bd=1)
            card.pack(fill=tk.X, padx=4, pady=3, ipady=4)

            # 色インジケーター
            tk.Frame(card, bg=color, width=6).pack(side=tk.LEFT, fill=tk.Y)

            # チェックボックス
            cb = tk.Checkbutton(card, variable=var, bg="white",
                                 activebackground="white",
                                 command=lambda hid=habit_id, v=var: self._toggle_habit(hid, v))
            cb.pack(side=tk.LEFT, padx=6)

            info_f = tk.Frame(card, bg="white")
            info_f.pack(side=tk.LEFT, fill=tk.X, expand=True)
            tk.Label(info_f, text=name, bg="white",
                     font=("Arial", 12, "bold" if not done else "normal"),
                     fg="#333" if not done else "#999").pack(anchor="w")
            if desc:
                tk.Label(info_f, text=desc, bg="white",
                         font=("Arial", 9), fg="#888").pack(anchor="w")

            # 連続記録
            streak = self._get_streak(habit_id, log_date)
            streak_lbl = tk.Label(card, text=f"🔥 {streak}日",
                                   bg="white", fg="#ff6f00",
                                   font=("Arial", 10, "bold"))
            streak_lbl.pack(side=tk.RIGHT, padx=8)

        self._update_day_progress(len(habits))

    def _toggle_habit(self, habit_id, var):
        log_date = self.log_date_var.get()
        done = var.get()
        if done:
            self.conn.execute(
                "INSERT OR REPLACE INTO habit_logs "
                "(habit_id, log_date, completed) VALUES (?,?,1)",
                (habit_id, log_date))
        else:
            self.conn.execute(
                "DELETE FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date))
        self.conn.commit()
        total = len(self._check_vars)
        self._update_day_progress(total)
        self._refresh_today()

    def _update_day_progress(self, total):
        if not total:
            return
        done = sum(1 for v in self._check_vars.values() if v.get())
        pct = int(done / total * 100)
        self.day_progress["value"] = pct
        self.progress_label.config(
            text=f"{done}/{total} 完了 ({pct}%)",
            fg="#2e7d32" if pct == 100 else "#333")

    def _get_streak(self, habit_id, until_date):
        """連続達成日数を計算"""
        streak = 0
        d = datetime.fromisoformat(until_date).date()
        while True:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, d.isoformat())).fetchone()
            if row and row[0]:
                streak += 1
                d -= timedelta(days=1)
            else:
                break
        return streak

    def _prev_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() - timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    def _next_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() + timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    # ── カレンダータブ ────────────────────────────────────────────

    def _build_calendar_tab(self, parent):
        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)

        self.cal_year_var = tk.IntVar(value=date.today().year)
        self.cal_month_var = tk.IntVar(value=date.today().month)
        ttk.Button(ctrl_f, text="◀",
                   command=self._cal_prev_month).pack(side=tk.LEFT)
        self.cal_title = tk.Label(ctrl_f, text="",
                                   bg="#f8f9fc", font=("Arial", 13, "bold"))
        self.cal_title.pack(side=tk.LEFT, padx=12)
        ttk.Button(ctrl_f, text="▶",
                   command=self._cal_next_month).pack(side=tk.LEFT)

        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.cal_habit_var = tk.StringVar(value="すべて")
        self.cal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.cal_habit_var,
                                          state="readonly", width=14)
        self.cal_habit_cb.pack(side=tk.LEFT)
        self.cal_habit_cb.bind("<<ComboboxSelected>>", lambda e: self._refresh_calendar())

        self.cal_canvas = tk.Canvas(parent, bg="#f8f9fc", highlightthickness=0)
        self.cal_canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self._refresh_calendar()

    def _cal_prev_month(self):
        m = self.cal_month_var.get() - 1
        y = self.cal_year_var.get()
        if m < 1:
            m = 12; y -= 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _cal_next_month(self):
        m = self.cal_month_var.get() + 1
        y = self.cal_year_var.get()
        if m > 12:
            m = 1; y += 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _refresh_calendar(self):
        import calendar
        y = self.cal_year_var.get()
        m = self.cal_month_var.get()
        self.cal_title.config(text=f"{y}年 {m}月")
        self.cal_canvas.delete("all")

        # 月の達成率データ
        habit_name = self.cal_habit_var.get()
        habit_id = None
        if habit_name != "すべて":
            row = self.conn.execute("SELECT id FROM habits WHERE name=?",
                                    (habit_name,)).fetchone()
            if row:
                habit_id = row[0]

        CELL_W, CELL_H = 52, 52
        DAYS = ["月", "火", "水", "木", "金", "土", "日"]
        x0, y0 = 20, 20

        for i, d in enumerate(DAYS):
            color = "#f44336" if d == "日" else "#1976d2" if d == "土" else "#333"
            self.cal_canvas.create_text(
                x0 + i * CELL_W + CELL_W // 2, y0,
                text=d, fill=color, font=("Arial", 11, "bold"))

        cal = calendar.monthcalendar(y, m)
        today = date.today()
        total_habits = self.conn.execute(
            "SELECT COUNT(*) FROM habits WHERE active=1").fetchone()[0]

        for week_idx, week in enumerate(cal):
            for day_idx, day in enumerate(week):
                if day == 0:
                    continue
                dx = x0 + day_idx * CELL_W
                dy = y0 + 30 + week_idx * CELL_H
                d_str = f"{y}-{m:02d}-{day:02d}"
                d_date = date(y, m, day)

                # 達成数を取得
                if habit_id:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                        "AND log_date=? AND completed=1",
                        (habit_id, d_str)).fetchone()[0]
                    total = 1
                else:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs l "
                        "JOIN habits h ON l.habit_id=h.id "
                        "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                        (d_str,)).fetchone()[0]
                    total = total_habits

                # セル色
                if d_date > today:
                    bg = "#f5f5f5"
                elif total > 0 and done == total:
                    bg = "#a5d6a7"  # 全達成
                elif done > 0:
                    bg = "#fff9c4"  # 一部達成
                else:
                    bg = "#ffcdd2"  # 未達成

                self.cal_canvas.create_rectangle(
                    dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                    fill=bg, outline="#ccc")

                day_color = "#f44336" if day_idx == 6 else "#1976d2" if day_idx == 5 else "#333"
                if d_date == today:
                    day_color = "#0d47a1"
                    self.cal_canvas.create_rectangle(
                        dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                        fill=bg, outline="#0d47a1", width=2)
                self.cal_canvas.create_text(
                    dx + CELL_W // 2, dy + 16,
                    text=str(day), fill=day_color, font=("Arial", 10))
                if total > 0:
                    self.cal_canvas.create_text(
                        dx + CELL_W // 2, dy + 34,
                        text=f"{done}/{total}", fill="#555", font=("Arial", 8))

    # ── 分析タブ ──────────────────────────────────────────────────

    def _build_analysis_tab(self, parent):
        if not MPL_AVAILABLE:
            tk.Label(parent,
                     text="⚠ matplotlib が未インストールです (pip install matplotlib)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)
            return

        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.anal_habit_var = tk.StringVar(value="すべて")
        self.anal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.anal_habit_var,
                                           state="readonly", width=14)
        self.anal_habit_cb.pack(side=tk.LEFT, padx=4)
        tk.Label(ctrl_f, text="期間:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT, padx=(8, 2))
        self.period_var = tk.StringVar(value="30日")
        ttk.Combobox(ctrl_f, textvariable=self.period_var,
                     values=["7日", "30日", "90日"],
                     state="readonly", width=6).pack(side=tk.LEFT)
        ttk.Button(ctrl_f, text="🔄 更新",
                   command=self._update_analysis).pack(side=tk.LEFT, padx=8)

        fig = Figure(figsize=(10, 5), facecolor="#f8f9fc")
        self.anal_axes = [fig.add_subplot(1, 2, i + 1) for i in range(2)]
        fig.tight_layout(pad=2.0)
        self.anal_canvas_widget = FigureCanvasTkAgg(fig, master=parent)
        self.anal_canvas_widget.get_tk_widget().pack(fill=tk.BOTH, expand=True)
        self.anal_fig = fig

        # 統計テキスト
        self.anal_stats = tk.Text(parent, height=4, bg="#1e1e1e", fg="#c9d1d9",
                                   font=("Courier New", 10), state=tk.DISABLED)
        self.anal_stats.pack(fill=tk.X, padx=8, pady=4)

    def _update_analysis(self):
        if not MPL_AVAILABLE:
            return
        period_map = {"7日": 7, "30日": 30, "90日": 90}
        days = period_map.get(self.period_var.get(), 30)
        today = date.today()
        start = today - timedelta(days=days - 1)

        habits = self.conn.execute(
            "SELECT id, name, color FROM habits WHERE active=1").fetchall()
        if not habits:
            return

        for ax in self.anal_axes:
            ax.clear()

        # グラフ1: 習慣別達成率
        names = []
        rates = []
        colors = []
        for hid, name, color in habits:
            done = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                "AND log_date >= ? AND completed=1",
                (hid, start.isoformat())).fetchone()[0]
            rate = done / days * 100
            names.append(name[:6])
            rates.append(rate)
            colors.append(color)

        ax1 = self.anal_axes[0]
        ax1.barh(names, rates, color=colors, alpha=0.8)
        ax1.set_xlim(0, 100)
        ax1.set_xlabel("達成率 (%)")
        ax1.set_title(f"習慣別達成率 (過去{days}日間)")
        for i, (r, n) in enumerate(zip(rates, names)):
            ax1.text(r + 1, i, f"{r:.0f}%", va="center", fontsize=9)

        # グラフ2: 日別総達成数(折れ線)
        ax2 = self.anal_axes[1]
        dates_list = []
        totals = []
        for i in range(days):
            d = start + timedelta(days=i)
            cnt = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs l "
                "JOIN habits h ON l.habit_id=h.id "
                "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                (d.isoformat(),)).fetchone()[0]
            dates_list.append(i)
            totals.append(cnt)

        ax2.plot(dates_list, totals, color="#4caf50", linewidth=2)
        ax2.fill_between(dates_list, totals, alpha=0.2, color="#4caf50")
        ax2.set_title(f"日別達成数 (過去{days}日間)")
        ax2.set_ylabel("達成習慣数")
        ax2.set_ylim(0, max(len(habits), 1))

        self.anal_fig.tight_layout(pad=2.0)
        self.anal_canvas_widget.draw()

        # 統計情報
        total_logs = self.conn.execute(
            "SELECT COUNT(*) FROM habit_logs WHERE log_date >= ? AND completed=1",
            (start.isoformat(),)).fetchone()[0]
        avg = total_logs / days if days else 0
        stats = (f"期間: {start} 〜 {today}  |  "
                 f"合計達成: {total_logs} 回  |  "
                 f"1日平均: {avg:.1f} 習慣\n")
        for hid, name, _ in habits:
            streak = self._get_streak(hid, today.isoformat())
            stats += f"  {name}: 現在の連続 {streak}日  "
        self.anal_stats.config(state=tk.NORMAL)
        self.anal_stats.delete("1.0", tk.END)
        self.anal_stats.insert("1.0", stats)
        self.anal_stats.config(state=tk.DISABLED)

    # ── 習慣管理タブ ──────────────────────────────────────────────

    def _build_manage_tab(self, parent):
        # 追加フォーム
        add_f = ttk.LabelFrame(parent, text="習慣を追加", padding=8)
        add_f.pack(fill=tk.X, padx=8, pady=6)
        for lbl, attr in [("名前:", "new_name_var"), ("説明:", "new_desc_var")]:
            row = tk.Frame(add_f, bg=add_f.cget("background"))
            row.pack(fill=tk.X, pady=2)
            tk.Label(row, text=lbl, bg=row.cget("bg"), width=8,
                     anchor="e").pack(side=tk.LEFT)
            var = tk.StringVar()
            setattr(self, attr, var)
            ttk.Entry(row, textvariable=var, width=30).pack(side=tk.LEFT, padx=4)

        row_c = tk.Frame(add_f, bg=add_f.cget("background"))
        row_c.pack(fill=tk.X, pady=2)
        tk.Label(row_c, text="色:", bg=row_c.cget("bg"), width=8,
                 anchor="e").pack(side=tk.LEFT)
        self.new_color_var = tk.StringVar(value="#4caf50")
        for c in ["#4caf50", "#2196f3", "#ff9800", "#f44336",
                  "#9c27b0", "#009688", "#795548"]:
            tk.Button(row_c, bg=c, width=2, relief=tk.FLAT, bd=1,
                      command=lambda cv=c: self.new_color_var.set(cv)
                      ).pack(side=tk.LEFT, padx=2)
        ttk.Button(add_f, text="➕ 追加",
                   command=self._add_habit).pack(pady=4)

        # 習慣リスト
        cols = ("name", "desc", "streak", "total", "active")
        self.habit_tree = ttk.Treeview(parent, columns=cols, show="headings", height=12)
        for c, h, w in [("name", "名前", 120), ("desc", "説明", 200),
                         ("streak", "連続日数", 80), ("total", "合計達成", 80),
                         ("active", "状態", 60)]:
            self.habit_tree.heading(c, text=h)
            self.habit_tree.column(c, width=w, minwidth=40)
        sb = ttk.Scrollbar(parent, command=self.habit_tree.yview)
        self.habit_tree.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        self.habit_tree.pack(fill=tk.BOTH, expand=True, padx=8)

        btn_f = tk.Frame(parent, bg="#f8f9fc")
        btn_f.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_f, text="🔄 更新",
                   command=self._refresh_habits).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="⏸ 無効化/有効化",
                   command=self._toggle_habit_active).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="🗑 削除",
                   command=self._delete_habit).pack(side=tk.LEFT, padx=4)

    def _add_habit(self):
        name = self.new_name_var.get().strip()
        if not name:
            messagebox.showwarning("警告", "名前を入力してください")
            return
        self.conn.execute(
            "INSERT INTO habits (name, description, color, created_at) VALUES (?,?,?,?)",
            (name, self.new_desc_var.get().strip(),
             self.new_color_var.get(),
             date.today().isoformat()))
        self.conn.commit()
        self.new_name_var.set("")
        self.new_desc_var.set("")
        self._refresh_habits()
        self.status_var.set(f"習慣を追加しました: {name}")

    def _refresh_habits(self):
        habits = self.conn.execute(
            "SELECT id, name, description, active FROM habits ORDER BY id").fetchall()
        habit_names = ["すべて"] + [h[1] for h in habits if h[3]]

        # コンボボックス更新
        for cb_attr in ["cal_habit_cb", "anal_habit_cb"]:
            if hasattr(self, cb_attr):
                getattr(self, cb_attr).configure(values=habit_names)

        if hasattr(self, "habit_tree"):
            self.habit_tree.delete(*self.habit_tree.get_children())
            today = date.today()
            for hid, name, desc, active in habits:
                streak = self._get_streak(hid, today.isoformat())
                total = self.conn.execute(
                    "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? AND completed=1",
                    (hid,)).fetchone()[0]
                state = "有効" if active else "無効"
                self.habit_tree.insert("", "end", iid=str(hid),
                                       values=(name, desc or "", f"🔥{streak}日",
                                               f"{total}回", state))

        if hasattr(self, "_check_vars"):
            self._refresh_today()

    def _toggle_habit_active(self):
        sel = self.habit_tree.selection()
        if sel:
            hid = int(sel[0])
            row = self.conn.execute("SELECT active FROM habits WHERE id=?",
                                    (hid,)).fetchone()
            if row:
                new_val = 0 if row[0] else 1
                self.conn.execute("UPDATE habits SET active=? WHERE id=?",
                                  (new_val, hid))
                self.conn.commit()
                self._refresh_habits()

    def _delete_habit(self):
        sel = self.habit_tree.selection()
        if sel and messagebox.askyesno("確認", "習慣とすべてのログを削除しますか?"):
            hid = int(sel[0])
            self.conn.execute("DELETE FROM habit_logs WHERE habit_id=?", (hid,))
            self.conn.execute("DELETE FROM habits WHERE id=?", (hid,))
            self.conn.commit()
            self._refresh_habits()


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

5. コード解説

習慣トラッカー(分析付き)のコードを詳しく解説します。クラスベースの設計で各機能を整理して実装しています。

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

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

import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3
import os
from datetime import datetime, timedelta, date

try:
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    MPL_AVAILABLE = True
except ImportError:
    MPL_AVAILABLE = False


class App42:
    """習慣トラッカー(分析付き)"""

    DB_PATH = os.path.join(os.path.dirname(__file__), "habits.db")

    def __init__(self, root):
        self.root = root
        self.root.title("習慣トラッカー(分析付き)")
        self.root.geometry("1000x680")
        self.root.configure(bg="#f8f9fc")
        self._init_db()
        self._build_ui()
        self._refresh_habits()

    def _init_db(self):
        self.conn = sqlite3.connect(self.DB_PATH)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habits (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                description TEXT,
                color TEXT DEFAULT '#4caf50',
                created_at TEXT,
                active INTEGER DEFAULT 1
            )
        """)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habit_logs (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                habit_id INTEGER,
                log_date TEXT,
                completed INTEGER DEFAULT 1,
                note TEXT,
                UNIQUE(habit_id, log_date),
                FOREIGN KEY(habit_id) REFERENCES habits(id)
            )
        """)
        self.conn.commit()
        # サンプルデータ挿入
        if not self.conn.execute("SELECT 1 FROM habits").fetchone():
            samples = [("早起き", "7時前に起床", "#ff9800"),
                       ("運動", "30分以上の運動", "#2196f3"),
                       ("読書", "30分以上の読書", "#9c27b0"),
                       ("瞑想", "10分以上の瞑想", "#009688"),
                       ("日記", "日記を書く", "#f44336")]
            for name, desc, color in samples:
                self.conn.execute(
                    "INSERT INTO habits (name, description, color, created_at) "
                    "VALUES (?,?,?,?)",
                    (name, desc, color, date.today().isoformat()))
            self.conn.commit()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#388e3c", pady=8)
        header.pack(fill=tk.X)
        tk.Label(header, text="📈 習慣トラッカー",
                 font=("Noto Sans JP", 13, "bold"),
                 bg="#388e3c", fg="white").pack(side=tk.LEFT, padx=12)
        self.today_label = tk.Label(header,
                                     text=f"今日: {date.today().strftime('%Y年%m月%d日')}",
                                     bg="#388e3c", fg="#c8e6c9",
                                     font=("Arial", 10))
        self.today_label.pack(side=tk.RIGHT, padx=12)

        notebook = ttk.Notebook(self.root)
        notebook.pack(fill=tk.BOTH, expand=True, padx=6, pady=6)

        # 今日の記録タブ
        today_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(today_tab, text="📅 今日の記録")
        self._build_today_tab(today_tab)

        # カレンダービュータブ
        cal_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(cal_tab, text="📆 カレンダー")
        self._build_calendar_tab(cal_tab)

        # 分析タブ
        analysis_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(analysis_tab, text="📊 分析")
        self._build_analysis_tab(analysis_tab)

        # 習慣管理タブ
        manage_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(manage_tab, text="⚙ 習慣管理")
        self._build_manage_tab(manage_tab)

        self.status_var = tk.StringVar(value="習慣を記録しましょう!")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#dde", font=("Arial", 9), anchor="w", padx=8
                 ).pack(fill=tk.X, side=tk.BOTTOM)

    # ── 今日の記録タブ ────────────────────────────────────────────

    def _build_today_tab(self, parent):
        # 日付選択
        date_f = tk.Frame(parent, bg="#f8f9fc")
        date_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(date_f, text="日付:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.log_date_var = tk.StringVar(value=date.today().isoformat())
        ttk.Entry(date_f, textvariable=self.log_date_var,
                  width=12).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="今日",
                   command=lambda: (
                       self.log_date_var.set(date.today().isoformat()),
                       self._refresh_today())).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="< 前日",
                   command=self._prev_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="翌日 >",
                   command=self._next_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="🔄 更新",
                   command=self._refresh_today).pack(side=tk.LEFT, padx=4)

        # 習慣チェックリスト
        list_f = ttk.LabelFrame(parent, text="習慣チェックリスト", padding=8)
        list_f.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)

        canvas = tk.Canvas(list_f, bg="#f8f9fc", highlightthickness=0)
        sb = ttk.Scrollbar(list_f, orient=tk.VERTICAL, command=canvas.yview)
        canvas.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        canvas.pack(fill=tk.BOTH, expand=True)

        self.check_frame = tk.Frame(canvas, bg="#f8f9fc")
        canvas.create_window((0, 0), window=self.check_frame, anchor="nw")
        self.check_frame.bind("<Configure>",
                              lambda e: canvas.configure(
                                  scrollregion=canvas.bbox("all")))

        # 今日の進捗
        prog_f = tk.Frame(parent, bg="#f8f9fc")
        prog_f.pack(fill=tk.X, padx=8, pady=4)
        self.progress_label = tk.Label(prog_f, text="",
                                        bg="#f8f9fc", font=("Arial", 11, "bold"))
        self.progress_label.pack(side=tk.LEFT)
        self.day_progress = ttk.Progressbar(prog_f, length=300,
                                             maximum=100, mode="determinate")
        self.day_progress.pack(side=tk.LEFT, padx=8)

        self._check_vars = {}

    def _refresh_today(self):
        log_date = self.log_date_var.get()
        for widget in self.check_frame.winfo_children():
            widget.destroy()
        self._check_vars.clear()

        habits = self.conn.execute(
            "SELECT id, name, description, color FROM habits WHERE active=1 "
            "ORDER BY id").fetchall()

        for habit_id, name, desc, color in habits:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date)).fetchone()
            done = bool(row and row[0]) if row else False

            var = tk.BooleanVar(value=done)
            self._check_vars[habit_id] = var

            card = tk.Frame(self.check_frame, bg="white", relief=tk.FLAT, bd=1)
            card.pack(fill=tk.X, padx=4, pady=3, ipady=4)

            # 色インジケーター
            tk.Frame(card, bg=color, width=6).pack(side=tk.LEFT, fill=tk.Y)

            # チェックボックス
            cb = tk.Checkbutton(card, variable=var, bg="white",
                                 activebackground="white",
                                 command=lambda hid=habit_id, v=var: self._toggle_habit(hid, v))
            cb.pack(side=tk.LEFT, padx=6)

            info_f = tk.Frame(card, bg="white")
            info_f.pack(side=tk.LEFT, fill=tk.X, expand=True)
            tk.Label(info_f, text=name, bg="white",
                     font=("Arial", 12, "bold" if not done else "normal"),
                     fg="#333" if not done else "#999").pack(anchor="w")
            if desc:
                tk.Label(info_f, text=desc, bg="white",
                         font=("Arial", 9), fg="#888").pack(anchor="w")

            # 連続記録
            streak = self._get_streak(habit_id, log_date)
            streak_lbl = tk.Label(card, text=f"🔥 {streak}日",
                                   bg="white", fg="#ff6f00",
                                   font=("Arial", 10, "bold"))
            streak_lbl.pack(side=tk.RIGHT, padx=8)

        self._update_day_progress(len(habits))

    def _toggle_habit(self, habit_id, var):
        log_date = self.log_date_var.get()
        done = var.get()
        if done:
            self.conn.execute(
                "INSERT OR REPLACE INTO habit_logs "
                "(habit_id, log_date, completed) VALUES (?,?,1)",
                (habit_id, log_date))
        else:
            self.conn.execute(
                "DELETE FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date))
        self.conn.commit()
        total = len(self._check_vars)
        self._update_day_progress(total)
        self._refresh_today()

    def _update_day_progress(self, total):
        if not total:
            return
        done = sum(1 for v in self._check_vars.values() if v.get())
        pct = int(done / total * 100)
        self.day_progress["value"] = pct
        self.progress_label.config(
            text=f"{done}/{total} 完了 ({pct}%)",
            fg="#2e7d32" if pct == 100 else "#333")

    def _get_streak(self, habit_id, until_date):
        """連続達成日数を計算"""
        streak = 0
        d = datetime.fromisoformat(until_date).date()
        while True:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, d.isoformat())).fetchone()
            if row and row[0]:
                streak += 1
                d -= timedelta(days=1)
            else:
                break
        return streak

    def _prev_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() - timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    def _next_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() + timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    # ── カレンダータブ ────────────────────────────────────────────

    def _build_calendar_tab(self, parent):
        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)

        self.cal_year_var = tk.IntVar(value=date.today().year)
        self.cal_month_var = tk.IntVar(value=date.today().month)
        ttk.Button(ctrl_f, text="◀",
                   command=self._cal_prev_month).pack(side=tk.LEFT)
        self.cal_title = tk.Label(ctrl_f, text="",
                                   bg="#f8f9fc", font=("Arial", 13, "bold"))
        self.cal_title.pack(side=tk.LEFT, padx=12)
        ttk.Button(ctrl_f, text="▶",
                   command=self._cal_next_month).pack(side=tk.LEFT)

        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.cal_habit_var = tk.StringVar(value="すべて")
        self.cal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.cal_habit_var,
                                          state="readonly", width=14)
        self.cal_habit_cb.pack(side=tk.LEFT)
        self.cal_habit_cb.bind("<<ComboboxSelected>>", lambda e: self._refresh_calendar())

        self.cal_canvas = tk.Canvas(parent, bg="#f8f9fc", highlightthickness=0)
        self.cal_canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self._refresh_calendar()

    def _cal_prev_month(self):
        m = self.cal_month_var.get() - 1
        y = self.cal_year_var.get()
        if m < 1:
            m = 12; y -= 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _cal_next_month(self):
        m = self.cal_month_var.get() + 1
        y = self.cal_year_var.get()
        if m > 12:
            m = 1; y += 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _refresh_calendar(self):
        import calendar
        y = self.cal_year_var.get()
        m = self.cal_month_var.get()
        self.cal_title.config(text=f"{y}年 {m}月")
        self.cal_canvas.delete("all")

        # 月の達成率データ
        habit_name = self.cal_habit_var.get()
        habit_id = None
        if habit_name != "すべて":
            row = self.conn.execute("SELECT id FROM habits WHERE name=?",
                                    (habit_name,)).fetchone()
            if row:
                habit_id = row[0]

        CELL_W, CELL_H = 52, 52
        DAYS = ["月", "火", "水", "木", "金", "土", "日"]
        x0, y0 = 20, 20

        for i, d in enumerate(DAYS):
            color = "#f44336" if d == "日" else "#1976d2" if d == "土" else "#333"
            self.cal_canvas.create_text(
                x0 + i * CELL_W + CELL_W // 2, y0,
                text=d, fill=color, font=("Arial", 11, "bold"))

        cal = calendar.monthcalendar(y, m)
        today = date.today()
        total_habits = self.conn.execute(
            "SELECT COUNT(*) FROM habits WHERE active=1").fetchone()[0]

        for week_idx, week in enumerate(cal):
            for day_idx, day in enumerate(week):
                if day == 0:
                    continue
                dx = x0 + day_idx * CELL_W
                dy = y0 + 30 + week_idx * CELL_H
                d_str = f"{y}-{m:02d}-{day:02d}"
                d_date = date(y, m, day)

                # 達成数を取得
                if habit_id:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                        "AND log_date=? AND completed=1",
                        (habit_id, d_str)).fetchone()[0]
                    total = 1
                else:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs l "
                        "JOIN habits h ON l.habit_id=h.id "
                        "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                        (d_str,)).fetchone()[0]
                    total = total_habits

                # セル色
                if d_date > today:
                    bg = "#f5f5f5"
                elif total > 0 and done == total:
                    bg = "#a5d6a7"  # 全達成
                elif done > 0:
                    bg = "#fff9c4"  # 一部達成
                else:
                    bg = "#ffcdd2"  # 未達成

                self.cal_canvas.create_rectangle(
                    dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                    fill=bg, outline="#ccc")

                day_color = "#f44336" if day_idx == 6 else "#1976d2" if day_idx == 5 else "#333"
                if d_date == today:
                    day_color = "#0d47a1"
                    self.cal_canvas.create_rectangle(
                        dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                        fill=bg, outline="#0d47a1", width=2)
                self.cal_canvas.create_text(
                    dx + CELL_W // 2, dy + 16,
                    text=str(day), fill=day_color, font=("Arial", 10))
                if total > 0:
                    self.cal_canvas.create_text(
                        dx + CELL_W // 2, dy + 34,
                        text=f"{done}/{total}", fill="#555", font=("Arial", 8))

    # ── 分析タブ ──────────────────────────────────────────────────

    def _build_analysis_tab(self, parent):
        if not MPL_AVAILABLE:
            tk.Label(parent,
                     text="⚠ matplotlib が未インストールです (pip install matplotlib)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)
            return

        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.anal_habit_var = tk.StringVar(value="すべて")
        self.anal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.anal_habit_var,
                                           state="readonly", width=14)
        self.anal_habit_cb.pack(side=tk.LEFT, padx=4)
        tk.Label(ctrl_f, text="期間:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT, padx=(8, 2))
        self.period_var = tk.StringVar(value="30日")
        ttk.Combobox(ctrl_f, textvariable=self.period_var,
                     values=["7日", "30日", "90日"],
                     state="readonly", width=6).pack(side=tk.LEFT)
        ttk.Button(ctrl_f, text="🔄 更新",
                   command=self._update_analysis).pack(side=tk.LEFT, padx=8)

        fig = Figure(figsize=(10, 5), facecolor="#f8f9fc")
        self.anal_axes = [fig.add_subplot(1, 2, i + 1) for i in range(2)]
        fig.tight_layout(pad=2.0)
        self.anal_canvas_widget = FigureCanvasTkAgg(fig, master=parent)
        self.anal_canvas_widget.get_tk_widget().pack(fill=tk.BOTH, expand=True)
        self.anal_fig = fig

        # 統計テキスト
        self.anal_stats = tk.Text(parent, height=4, bg="#1e1e1e", fg="#c9d1d9",
                                   font=("Courier New", 10), state=tk.DISABLED)
        self.anal_stats.pack(fill=tk.X, padx=8, pady=4)

    def _update_analysis(self):
        if not MPL_AVAILABLE:
            return
        period_map = {"7日": 7, "30日": 30, "90日": 90}
        days = period_map.get(self.period_var.get(), 30)
        today = date.today()
        start = today - timedelta(days=days - 1)

        habits = self.conn.execute(
            "SELECT id, name, color FROM habits WHERE active=1").fetchall()
        if not habits:
            return

        for ax in self.anal_axes:
            ax.clear()

        # グラフ1: 習慣別達成率
        names = []
        rates = []
        colors = []
        for hid, name, color in habits:
            done = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                "AND log_date >= ? AND completed=1",
                (hid, start.isoformat())).fetchone()[0]
            rate = done / days * 100
            names.append(name[:6])
            rates.append(rate)
            colors.append(color)

        ax1 = self.anal_axes[0]
        ax1.barh(names, rates, color=colors, alpha=0.8)
        ax1.set_xlim(0, 100)
        ax1.set_xlabel("達成率 (%)")
        ax1.set_title(f"習慣別達成率 (過去{days}日間)")
        for i, (r, n) in enumerate(zip(rates, names)):
            ax1.text(r + 1, i, f"{r:.0f}%", va="center", fontsize=9)

        # グラフ2: 日別総達成数(折れ線)
        ax2 = self.anal_axes[1]
        dates_list = []
        totals = []
        for i in range(days):
            d = start + timedelta(days=i)
            cnt = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs l "
                "JOIN habits h ON l.habit_id=h.id "
                "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                (d.isoformat(),)).fetchone()[0]
            dates_list.append(i)
            totals.append(cnt)

        ax2.plot(dates_list, totals, color="#4caf50", linewidth=2)
        ax2.fill_between(dates_list, totals, alpha=0.2, color="#4caf50")
        ax2.set_title(f"日別達成数 (過去{days}日間)")
        ax2.set_ylabel("達成習慣数")
        ax2.set_ylim(0, max(len(habits), 1))

        self.anal_fig.tight_layout(pad=2.0)
        self.anal_canvas_widget.draw()

        # 統計情報
        total_logs = self.conn.execute(
            "SELECT COUNT(*) FROM habit_logs WHERE log_date >= ? AND completed=1",
            (start.isoformat(),)).fetchone()[0]
        avg = total_logs / days if days else 0
        stats = (f"期間: {start} 〜 {today}  |  "
                 f"合計達成: {total_logs} 回  |  "
                 f"1日平均: {avg:.1f} 習慣\n")
        for hid, name, _ in habits:
            streak = self._get_streak(hid, today.isoformat())
            stats += f"  {name}: 現在の連続 {streak}日  "
        self.anal_stats.config(state=tk.NORMAL)
        self.anal_stats.delete("1.0", tk.END)
        self.anal_stats.insert("1.0", stats)
        self.anal_stats.config(state=tk.DISABLED)

    # ── 習慣管理タブ ──────────────────────────────────────────────

    def _build_manage_tab(self, parent):
        # 追加フォーム
        add_f = ttk.LabelFrame(parent, text="習慣を追加", padding=8)
        add_f.pack(fill=tk.X, padx=8, pady=6)
        for lbl, attr in [("名前:", "new_name_var"), ("説明:", "new_desc_var")]:
            row = tk.Frame(add_f, bg=add_f.cget("background"))
            row.pack(fill=tk.X, pady=2)
            tk.Label(row, text=lbl, bg=row.cget("bg"), width=8,
                     anchor="e").pack(side=tk.LEFT)
            var = tk.StringVar()
            setattr(self, attr, var)
            ttk.Entry(row, textvariable=var, width=30).pack(side=tk.LEFT, padx=4)

        row_c = tk.Frame(add_f, bg=add_f.cget("background"))
        row_c.pack(fill=tk.X, pady=2)
        tk.Label(row_c, text="色:", bg=row_c.cget("bg"), width=8,
                 anchor="e").pack(side=tk.LEFT)
        self.new_color_var = tk.StringVar(value="#4caf50")
        for c in ["#4caf50", "#2196f3", "#ff9800", "#f44336",
                  "#9c27b0", "#009688", "#795548"]:
            tk.Button(row_c, bg=c, width=2, relief=tk.FLAT, bd=1,
                      command=lambda cv=c: self.new_color_var.set(cv)
                      ).pack(side=tk.LEFT, padx=2)
        ttk.Button(add_f, text="➕ 追加",
                   command=self._add_habit).pack(pady=4)

        # 習慣リスト
        cols = ("name", "desc", "streak", "total", "active")
        self.habit_tree = ttk.Treeview(parent, columns=cols, show="headings", height=12)
        for c, h, w in [("name", "名前", 120), ("desc", "説明", 200),
                         ("streak", "連続日数", 80), ("total", "合計達成", 80),
                         ("active", "状態", 60)]:
            self.habit_tree.heading(c, text=h)
            self.habit_tree.column(c, width=w, minwidth=40)
        sb = ttk.Scrollbar(parent, command=self.habit_tree.yview)
        self.habit_tree.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        self.habit_tree.pack(fill=tk.BOTH, expand=True, padx=8)

        btn_f = tk.Frame(parent, bg="#f8f9fc")
        btn_f.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_f, text="🔄 更新",
                   command=self._refresh_habits).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="⏸ 無効化/有効化",
                   command=self._toggle_habit_active).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="🗑 削除",
                   command=self._delete_habit).pack(side=tk.LEFT, padx=4)

    def _add_habit(self):
        name = self.new_name_var.get().strip()
        if not name:
            messagebox.showwarning("警告", "名前を入力してください")
            return
        self.conn.execute(
            "INSERT INTO habits (name, description, color, created_at) VALUES (?,?,?,?)",
            (name, self.new_desc_var.get().strip(),
             self.new_color_var.get(),
             date.today().isoformat()))
        self.conn.commit()
        self.new_name_var.set("")
        self.new_desc_var.set("")
        self._refresh_habits()
        self.status_var.set(f"習慣を追加しました: {name}")

    def _refresh_habits(self):
        habits = self.conn.execute(
            "SELECT id, name, description, active FROM habits ORDER BY id").fetchall()
        habit_names = ["すべて"] + [h[1] for h in habits if h[3]]

        # コンボボックス更新
        for cb_attr in ["cal_habit_cb", "anal_habit_cb"]:
            if hasattr(self, cb_attr):
                getattr(self, cb_attr).configure(values=habit_names)

        if hasattr(self, "habit_tree"):
            self.habit_tree.delete(*self.habit_tree.get_children())
            today = date.today()
            for hid, name, desc, active in habits:
                streak = self._get_streak(hid, today.isoformat())
                total = self.conn.execute(
                    "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? AND completed=1",
                    (hid,)).fetchone()[0]
                state = "有効" if active else "無効"
                self.habit_tree.insert("", "end", iid=str(hid),
                                       values=(name, desc or "", f"🔥{streak}日",
                                               f"{total}回", state))

        if hasattr(self, "_check_vars"):
            self._refresh_today()

    def _toggle_habit_active(self):
        sel = self.habit_tree.selection()
        if sel:
            hid = int(sel[0])
            row = self.conn.execute("SELECT active FROM habits WHERE id=?",
                                    (hid,)).fetchone()
            if row:
                new_val = 0 if row[0] else 1
                self.conn.execute("UPDATE habits SET active=? WHERE id=?",
                                  (new_val, hid))
                self.conn.commit()
                self._refresh_habits()

    def _delete_habit(self):
        sel = self.habit_tree.selection()
        if sel and messagebox.askyesno("確認", "習慣とすべてのログを削除しますか?"):
            hid = int(sel[0])
            self.conn.execute("DELETE FROM habit_logs WHERE habit_id=?", (hid,))
            self.conn.execute("DELETE FROM habits WHERE id=?", (hid,))
            self.conn.commit()
            self._refresh_habits()


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

LabelFrameによるセクション分け

ttk.LabelFrame を使うことで、入力エリアと結果エリアを視覚的に分けられます。padding引数でフレーム内の余白を設定し、見やすいレイアウトを実現しています。

import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3
import os
from datetime import datetime, timedelta, date

try:
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    MPL_AVAILABLE = True
except ImportError:
    MPL_AVAILABLE = False


class App42:
    """習慣トラッカー(分析付き)"""

    DB_PATH = os.path.join(os.path.dirname(__file__), "habits.db")

    def __init__(self, root):
        self.root = root
        self.root.title("習慣トラッカー(分析付き)")
        self.root.geometry("1000x680")
        self.root.configure(bg="#f8f9fc")
        self._init_db()
        self._build_ui()
        self._refresh_habits()

    def _init_db(self):
        self.conn = sqlite3.connect(self.DB_PATH)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habits (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                description TEXT,
                color TEXT DEFAULT '#4caf50',
                created_at TEXT,
                active INTEGER DEFAULT 1
            )
        """)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habit_logs (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                habit_id INTEGER,
                log_date TEXT,
                completed INTEGER DEFAULT 1,
                note TEXT,
                UNIQUE(habit_id, log_date),
                FOREIGN KEY(habit_id) REFERENCES habits(id)
            )
        """)
        self.conn.commit()
        # サンプルデータ挿入
        if not self.conn.execute("SELECT 1 FROM habits").fetchone():
            samples = [("早起き", "7時前に起床", "#ff9800"),
                       ("運動", "30分以上の運動", "#2196f3"),
                       ("読書", "30分以上の読書", "#9c27b0"),
                       ("瞑想", "10分以上の瞑想", "#009688"),
                       ("日記", "日記を書く", "#f44336")]
            for name, desc, color in samples:
                self.conn.execute(
                    "INSERT INTO habits (name, description, color, created_at) "
                    "VALUES (?,?,?,?)",
                    (name, desc, color, date.today().isoformat()))
            self.conn.commit()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#388e3c", pady=8)
        header.pack(fill=tk.X)
        tk.Label(header, text="📈 習慣トラッカー",
                 font=("Noto Sans JP", 13, "bold"),
                 bg="#388e3c", fg="white").pack(side=tk.LEFT, padx=12)
        self.today_label = tk.Label(header,
                                     text=f"今日: {date.today().strftime('%Y年%m月%d日')}",
                                     bg="#388e3c", fg="#c8e6c9",
                                     font=("Arial", 10))
        self.today_label.pack(side=tk.RIGHT, padx=12)

        notebook = ttk.Notebook(self.root)
        notebook.pack(fill=tk.BOTH, expand=True, padx=6, pady=6)

        # 今日の記録タブ
        today_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(today_tab, text="📅 今日の記録")
        self._build_today_tab(today_tab)

        # カレンダービュータブ
        cal_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(cal_tab, text="📆 カレンダー")
        self._build_calendar_tab(cal_tab)

        # 分析タブ
        analysis_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(analysis_tab, text="📊 分析")
        self._build_analysis_tab(analysis_tab)

        # 習慣管理タブ
        manage_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(manage_tab, text="⚙ 習慣管理")
        self._build_manage_tab(manage_tab)

        self.status_var = tk.StringVar(value="習慣を記録しましょう!")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#dde", font=("Arial", 9), anchor="w", padx=8
                 ).pack(fill=tk.X, side=tk.BOTTOM)

    # ── 今日の記録タブ ────────────────────────────────────────────

    def _build_today_tab(self, parent):
        # 日付選択
        date_f = tk.Frame(parent, bg="#f8f9fc")
        date_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(date_f, text="日付:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.log_date_var = tk.StringVar(value=date.today().isoformat())
        ttk.Entry(date_f, textvariable=self.log_date_var,
                  width=12).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="今日",
                   command=lambda: (
                       self.log_date_var.set(date.today().isoformat()),
                       self._refresh_today())).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="< 前日",
                   command=self._prev_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="翌日 >",
                   command=self._next_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="🔄 更新",
                   command=self._refresh_today).pack(side=tk.LEFT, padx=4)

        # 習慣チェックリスト
        list_f = ttk.LabelFrame(parent, text="習慣チェックリスト", padding=8)
        list_f.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)

        canvas = tk.Canvas(list_f, bg="#f8f9fc", highlightthickness=0)
        sb = ttk.Scrollbar(list_f, orient=tk.VERTICAL, command=canvas.yview)
        canvas.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        canvas.pack(fill=tk.BOTH, expand=True)

        self.check_frame = tk.Frame(canvas, bg="#f8f9fc")
        canvas.create_window((0, 0), window=self.check_frame, anchor="nw")
        self.check_frame.bind("<Configure>",
                              lambda e: canvas.configure(
                                  scrollregion=canvas.bbox("all")))

        # 今日の進捗
        prog_f = tk.Frame(parent, bg="#f8f9fc")
        prog_f.pack(fill=tk.X, padx=8, pady=4)
        self.progress_label = tk.Label(prog_f, text="",
                                        bg="#f8f9fc", font=("Arial", 11, "bold"))
        self.progress_label.pack(side=tk.LEFT)
        self.day_progress = ttk.Progressbar(prog_f, length=300,
                                             maximum=100, mode="determinate")
        self.day_progress.pack(side=tk.LEFT, padx=8)

        self._check_vars = {}

    def _refresh_today(self):
        log_date = self.log_date_var.get()
        for widget in self.check_frame.winfo_children():
            widget.destroy()
        self._check_vars.clear()

        habits = self.conn.execute(
            "SELECT id, name, description, color FROM habits WHERE active=1 "
            "ORDER BY id").fetchall()

        for habit_id, name, desc, color in habits:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date)).fetchone()
            done = bool(row and row[0]) if row else False

            var = tk.BooleanVar(value=done)
            self._check_vars[habit_id] = var

            card = tk.Frame(self.check_frame, bg="white", relief=tk.FLAT, bd=1)
            card.pack(fill=tk.X, padx=4, pady=3, ipady=4)

            # 色インジケーター
            tk.Frame(card, bg=color, width=6).pack(side=tk.LEFT, fill=tk.Y)

            # チェックボックス
            cb = tk.Checkbutton(card, variable=var, bg="white",
                                 activebackground="white",
                                 command=lambda hid=habit_id, v=var: self._toggle_habit(hid, v))
            cb.pack(side=tk.LEFT, padx=6)

            info_f = tk.Frame(card, bg="white")
            info_f.pack(side=tk.LEFT, fill=tk.X, expand=True)
            tk.Label(info_f, text=name, bg="white",
                     font=("Arial", 12, "bold" if not done else "normal"),
                     fg="#333" if not done else "#999").pack(anchor="w")
            if desc:
                tk.Label(info_f, text=desc, bg="white",
                         font=("Arial", 9), fg="#888").pack(anchor="w")

            # 連続記録
            streak = self._get_streak(habit_id, log_date)
            streak_lbl = tk.Label(card, text=f"🔥 {streak}日",
                                   bg="white", fg="#ff6f00",
                                   font=("Arial", 10, "bold"))
            streak_lbl.pack(side=tk.RIGHT, padx=8)

        self._update_day_progress(len(habits))

    def _toggle_habit(self, habit_id, var):
        log_date = self.log_date_var.get()
        done = var.get()
        if done:
            self.conn.execute(
                "INSERT OR REPLACE INTO habit_logs "
                "(habit_id, log_date, completed) VALUES (?,?,1)",
                (habit_id, log_date))
        else:
            self.conn.execute(
                "DELETE FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date))
        self.conn.commit()
        total = len(self._check_vars)
        self._update_day_progress(total)
        self._refresh_today()

    def _update_day_progress(self, total):
        if not total:
            return
        done = sum(1 for v in self._check_vars.values() if v.get())
        pct = int(done / total * 100)
        self.day_progress["value"] = pct
        self.progress_label.config(
            text=f"{done}/{total} 完了 ({pct}%)",
            fg="#2e7d32" if pct == 100 else "#333")

    def _get_streak(self, habit_id, until_date):
        """連続達成日数を計算"""
        streak = 0
        d = datetime.fromisoformat(until_date).date()
        while True:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, d.isoformat())).fetchone()
            if row and row[0]:
                streak += 1
                d -= timedelta(days=1)
            else:
                break
        return streak

    def _prev_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() - timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    def _next_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() + timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    # ── カレンダータブ ────────────────────────────────────────────

    def _build_calendar_tab(self, parent):
        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)

        self.cal_year_var = tk.IntVar(value=date.today().year)
        self.cal_month_var = tk.IntVar(value=date.today().month)
        ttk.Button(ctrl_f, text="◀",
                   command=self._cal_prev_month).pack(side=tk.LEFT)
        self.cal_title = tk.Label(ctrl_f, text="",
                                   bg="#f8f9fc", font=("Arial", 13, "bold"))
        self.cal_title.pack(side=tk.LEFT, padx=12)
        ttk.Button(ctrl_f, text="▶",
                   command=self._cal_next_month).pack(side=tk.LEFT)

        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.cal_habit_var = tk.StringVar(value="すべて")
        self.cal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.cal_habit_var,
                                          state="readonly", width=14)
        self.cal_habit_cb.pack(side=tk.LEFT)
        self.cal_habit_cb.bind("<<ComboboxSelected>>", lambda e: self._refresh_calendar())

        self.cal_canvas = tk.Canvas(parent, bg="#f8f9fc", highlightthickness=0)
        self.cal_canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self._refresh_calendar()

    def _cal_prev_month(self):
        m = self.cal_month_var.get() - 1
        y = self.cal_year_var.get()
        if m < 1:
            m = 12; y -= 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _cal_next_month(self):
        m = self.cal_month_var.get() + 1
        y = self.cal_year_var.get()
        if m > 12:
            m = 1; y += 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _refresh_calendar(self):
        import calendar
        y = self.cal_year_var.get()
        m = self.cal_month_var.get()
        self.cal_title.config(text=f"{y}年 {m}月")
        self.cal_canvas.delete("all")

        # 月の達成率データ
        habit_name = self.cal_habit_var.get()
        habit_id = None
        if habit_name != "すべて":
            row = self.conn.execute("SELECT id FROM habits WHERE name=?",
                                    (habit_name,)).fetchone()
            if row:
                habit_id = row[0]

        CELL_W, CELL_H = 52, 52
        DAYS = ["月", "火", "水", "木", "金", "土", "日"]
        x0, y0 = 20, 20

        for i, d in enumerate(DAYS):
            color = "#f44336" if d == "日" else "#1976d2" if d == "土" else "#333"
            self.cal_canvas.create_text(
                x0 + i * CELL_W + CELL_W // 2, y0,
                text=d, fill=color, font=("Arial", 11, "bold"))

        cal = calendar.monthcalendar(y, m)
        today = date.today()
        total_habits = self.conn.execute(
            "SELECT COUNT(*) FROM habits WHERE active=1").fetchone()[0]

        for week_idx, week in enumerate(cal):
            for day_idx, day in enumerate(week):
                if day == 0:
                    continue
                dx = x0 + day_idx * CELL_W
                dy = y0 + 30 + week_idx * CELL_H
                d_str = f"{y}-{m:02d}-{day:02d}"
                d_date = date(y, m, day)

                # 達成数を取得
                if habit_id:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                        "AND log_date=? AND completed=1",
                        (habit_id, d_str)).fetchone()[0]
                    total = 1
                else:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs l "
                        "JOIN habits h ON l.habit_id=h.id "
                        "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                        (d_str,)).fetchone()[0]
                    total = total_habits

                # セル色
                if d_date > today:
                    bg = "#f5f5f5"
                elif total > 0 and done == total:
                    bg = "#a5d6a7"  # 全達成
                elif done > 0:
                    bg = "#fff9c4"  # 一部達成
                else:
                    bg = "#ffcdd2"  # 未達成

                self.cal_canvas.create_rectangle(
                    dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                    fill=bg, outline="#ccc")

                day_color = "#f44336" if day_idx == 6 else "#1976d2" if day_idx == 5 else "#333"
                if d_date == today:
                    day_color = "#0d47a1"
                    self.cal_canvas.create_rectangle(
                        dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                        fill=bg, outline="#0d47a1", width=2)
                self.cal_canvas.create_text(
                    dx + CELL_W // 2, dy + 16,
                    text=str(day), fill=day_color, font=("Arial", 10))
                if total > 0:
                    self.cal_canvas.create_text(
                        dx + CELL_W // 2, dy + 34,
                        text=f"{done}/{total}", fill="#555", font=("Arial", 8))

    # ── 分析タブ ──────────────────────────────────────────────────

    def _build_analysis_tab(self, parent):
        if not MPL_AVAILABLE:
            tk.Label(parent,
                     text="⚠ matplotlib が未インストールです (pip install matplotlib)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)
            return

        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.anal_habit_var = tk.StringVar(value="すべて")
        self.anal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.anal_habit_var,
                                           state="readonly", width=14)
        self.anal_habit_cb.pack(side=tk.LEFT, padx=4)
        tk.Label(ctrl_f, text="期間:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT, padx=(8, 2))
        self.period_var = tk.StringVar(value="30日")
        ttk.Combobox(ctrl_f, textvariable=self.period_var,
                     values=["7日", "30日", "90日"],
                     state="readonly", width=6).pack(side=tk.LEFT)
        ttk.Button(ctrl_f, text="🔄 更新",
                   command=self._update_analysis).pack(side=tk.LEFT, padx=8)

        fig = Figure(figsize=(10, 5), facecolor="#f8f9fc")
        self.anal_axes = [fig.add_subplot(1, 2, i + 1) for i in range(2)]
        fig.tight_layout(pad=2.0)
        self.anal_canvas_widget = FigureCanvasTkAgg(fig, master=parent)
        self.anal_canvas_widget.get_tk_widget().pack(fill=tk.BOTH, expand=True)
        self.anal_fig = fig

        # 統計テキスト
        self.anal_stats = tk.Text(parent, height=4, bg="#1e1e1e", fg="#c9d1d9",
                                   font=("Courier New", 10), state=tk.DISABLED)
        self.anal_stats.pack(fill=tk.X, padx=8, pady=4)

    def _update_analysis(self):
        if not MPL_AVAILABLE:
            return
        period_map = {"7日": 7, "30日": 30, "90日": 90}
        days = period_map.get(self.period_var.get(), 30)
        today = date.today()
        start = today - timedelta(days=days - 1)

        habits = self.conn.execute(
            "SELECT id, name, color FROM habits WHERE active=1").fetchall()
        if not habits:
            return

        for ax in self.anal_axes:
            ax.clear()

        # グラフ1: 習慣別達成率
        names = []
        rates = []
        colors = []
        for hid, name, color in habits:
            done = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                "AND log_date >= ? AND completed=1",
                (hid, start.isoformat())).fetchone()[0]
            rate = done / days * 100
            names.append(name[:6])
            rates.append(rate)
            colors.append(color)

        ax1 = self.anal_axes[0]
        ax1.barh(names, rates, color=colors, alpha=0.8)
        ax1.set_xlim(0, 100)
        ax1.set_xlabel("達成率 (%)")
        ax1.set_title(f"習慣別達成率 (過去{days}日間)")
        for i, (r, n) in enumerate(zip(rates, names)):
            ax1.text(r + 1, i, f"{r:.0f}%", va="center", fontsize=9)

        # グラフ2: 日別総達成数(折れ線)
        ax2 = self.anal_axes[1]
        dates_list = []
        totals = []
        for i in range(days):
            d = start + timedelta(days=i)
            cnt = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs l "
                "JOIN habits h ON l.habit_id=h.id "
                "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                (d.isoformat(),)).fetchone()[0]
            dates_list.append(i)
            totals.append(cnt)

        ax2.plot(dates_list, totals, color="#4caf50", linewidth=2)
        ax2.fill_between(dates_list, totals, alpha=0.2, color="#4caf50")
        ax2.set_title(f"日別達成数 (過去{days}日間)")
        ax2.set_ylabel("達成習慣数")
        ax2.set_ylim(0, max(len(habits), 1))

        self.anal_fig.tight_layout(pad=2.0)
        self.anal_canvas_widget.draw()

        # 統計情報
        total_logs = self.conn.execute(
            "SELECT COUNT(*) FROM habit_logs WHERE log_date >= ? AND completed=1",
            (start.isoformat(),)).fetchone()[0]
        avg = total_logs / days if days else 0
        stats = (f"期間: {start} 〜 {today}  |  "
                 f"合計達成: {total_logs} 回  |  "
                 f"1日平均: {avg:.1f} 習慣\n")
        for hid, name, _ in habits:
            streak = self._get_streak(hid, today.isoformat())
            stats += f"  {name}: 現在の連続 {streak}日  "
        self.anal_stats.config(state=tk.NORMAL)
        self.anal_stats.delete("1.0", tk.END)
        self.anal_stats.insert("1.0", stats)
        self.anal_stats.config(state=tk.DISABLED)

    # ── 習慣管理タブ ──────────────────────────────────────────────

    def _build_manage_tab(self, parent):
        # 追加フォーム
        add_f = ttk.LabelFrame(parent, text="習慣を追加", padding=8)
        add_f.pack(fill=tk.X, padx=8, pady=6)
        for lbl, attr in [("名前:", "new_name_var"), ("説明:", "new_desc_var")]:
            row = tk.Frame(add_f, bg=add_f.cget("background"))
            row.pack(fill=tk.X, pady=2)
            tk.Label(row, text=lbl, bg=row.cget("bg"), width=8,
                     anchor="e").pack(side=tk.LEFT)
            var = tk.StringVar()
            setattr(self, attr, var)
            ttk.Entry(row, textvariable=var, width=30).pack(side=tk.LEFT, padx=4)

        row_c = tk.Frame(add_f, bg=add_f.cget("background"))
        row_c.pack(fill=tk.X, pady=2)
        tk.Label(row_c, text="色:", bg=row_c.cget("bg"), width=8,
                 anchor="e").pack(side=tk.LEFT)
        self.new_color_var = tk.StringVar(value="#4caf50")
        for c in ["#4caf50", "#2196f3", "#ff9800", "#f44336",
                  "#9c27b0", "#009688", "#795548"]:
            tk.Button(row_c, bg=c, width=2, relief=tk.FLAT, bd=1,
                      command=lambda cv=c: self.new_color_var.set(cv)
                      ).pack(side=tk.LEFT, padx=2)
        ttk.Button(add_f, text="➕ 追加",
                   command=self._add_habit).pack(pady=4)

        # 習慣リスト
        cols = ("name", "desc", "streak", "total", "active")
        self.habit_tree = ttk.Treeview(parent, columns=cols, show="headings", height=12)
        for c, h, w in [("name", "名前", 120), ("desc", "説明", 200),
                         ("streak", "連続日数", 80), ("total", "合計達成", 80),
                         ("active", "状態", 60)]:
            self.habit_tree.heading(c, text=h)
            self.habit_tree.column(c, width=w, minwidth=40)
        sb = ttk.Scrollbar(parent, command=self.habit_tree.yview)
        self.habit_tree.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        self.habit_tree.pack(fill=tk.BOTH, expand=True, padx=8)

        btn_f = tk.Frame(parent, bg="#f8f9fc")
        btn_f.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_f, text="🔄 更新",
                   command=self._refresh_habits).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="⏸ 無効化/有効化",
                   command=self._toggle_habit_active).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="🗑 削除",
                   command=self._delete_habit).pack(side=tk.LEFT, padx=4)

    def _add_habit(self):
        name = self.new_name_var.get().strip()
        if not name:
            messagebox.showwarning("警告", "名前を入力してください")
            return
        self.conn.execute(
            "INSERT INTO habits (name, description, color, created_at) VALUES (?,?,?,?)",
            (name, self.new_desc_var.get().strip(),
             self.new_color_var.get(),
             date.today().isoformat()))
        self.conn.commit()
        self.new_name_var.set("")
        self.new_desc_var.set("")
        self._refresh_habits()
        self.status_var.set(f"習慣を追加しました: {name}")

    def _refresh_habits(self):
        habits = self.conn.execute(
            "SELECT id, name, description, active FROM habits ORDER BY id").fetchall()
        habit_names = ["すべて"] + [h[1] for h in habits if h[3]]

        # コンボボックス更新
        for cb_attr in ["cal_habit_cb", "anal_habit_cb"]:
            if hasattr(self, cb_attr):
                getattr(self, cb_attr).configure(values=habit_names)

        if hasattr(self, "habit_tree"):
            self.habit_tree.delete(*self.habit_tree.get_children())
            today = date.today()
            for hid, name, desc, active in habits:
                streak = self._get_streak(hid, today.isoformat())
                total = self.conn.execute(
                    "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? AND completed=1",
                    (hid,)).fetchone()[0]
                state = "有効" if active else "無効"
                self.habit_tree.insert("", "end", iid=str(hid),
                                       values=(name, desc or "", f"🔥{streak}日",
                                               f"{total}回", state))

        if hasattr(self, "_check_vars"):
            self._refresh_today()

    def _toggle_habit_active(self):
        sel = self.habit_tree.selection()
        if sel:
            hid = int(sel[0])
            row = self.conn.execute("SELECT active FROM habits WHERE id=?",
                                    (hid,)).fetchone()
            if row:
                new_val = 0 if row[0] else 1
                self.conn.execute("UPDATE habits SET active=? WHERE id=?",
                                  (new_val, hid))
                self.conn.commit()
                self._refresh_habits()

    def _delete_habit(self):
        sel = self.habit_tree.selection()
        if sel and messagebox.askyesno("確認", "習慣とすべてのログを削除しますか?"):
            hid = int(sel[0])
            self.conn.execute("DELETE FROM habit_logs WHERE habit_id=?", (hid,))
            self.conn.execute("DELETE FROM habits WHERE id=?", (hid,))
            self.conn.commit()
            self._refresh_habits()


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

Entryウィジェットとイベントバインド

ttk.Entryで入力フィールドを作成します。bind('', ...)でEnterキー押下時に処理を実行できます。これにより、マウスを使わずキーボードだけで操作できるUXが実現できます。

import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3
import os
from datetime import datetime, timedelta, date

try:
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    MPL_AVAILABLE = True
except ImportError:
    MPL_AVAILABLE = False


class App42:
    """習慣トラッカー(分析付き)"""

    DB_PATH = os.path.join(os.path.dirname(__file__), "habits.db")

    def __init__(self, root):
        self.root = root
        self.root.title("習慣トラッカー(分析付き)")
        self.root.geometry("1000x680")
        self.root.configure(bg="#f8f9fc")
        self._init_db()
        self._build_ui()
        self._refresh_habits()

    def _init_db(self):
        self.conn = sqlite3.connect(self.DB_PATH)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habits (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                description TEXT,
                color TEXT DEFAULT '#4caf50',
                created_at TEXT,
                active INTEGER DEFAULT 1
            )
        """)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habit_logs (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                habit_id INTEGER,
                log_date TEXT,
                completed INTEGER DEFAULT 1,
                note TEXT,
                UNIQUE(habit_id, log_date),
                FOREIGN KEY(habit_id) REFERENCES habits(id)
            )
        """)
        self.conn.commit()
        # サンプルデータ挿入
        if not self.conn.execute("SELECT 1 FROM habits").fetchone():
            samples = [("早起き", "7時前に起床", "#ff9800"),
                       ("運動", "30分以上の運動", "#2196f3"),
                       ("読書", "30分以上の読書", "#9c27b0"),
                       ("瞑想", "10分以上の瞑想", "#009688"),
                       ("日記", "日記を書く", "#f44336")]
            for name, desc, color in samples:
                self.conn.execute(
                    "INSERT INTO habits (name, description, color, created_at) "
                    "VALUES (?,?,?,?)",
                    (name, desc, color, date.today().isoformat()))
            self.conn.commit()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#388e3c", pady=8)
        header.pack(fill=tk.X)
        tk.Label(header, text="📈 習慣トラッカー",
                 font=("Noto Sans JP", 13, "bold"),
                 bg="#388e3c", fg="white").pack(side=tk.LEFT, padx=12)
        self.today_label = tk.Label(header,
                                     text=f"今日: {date.today().strftime('%Y年%m月%d日')}",
                                     bg="#388e3c", fg="#c8e6c9",
                                     font=("Arial", 10))
        self.today_label.pack(side=tk.RIGHT, padx=12)

        notebook = ttk.Notebook(self.root)
        notebook.pack(fill=tk.BOTH, expand=True, padx=6, pady=6)

        # 今日の記録タブ
        today_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(today_tab, text="📅 今日の記録")
        self._build_today_tab(today_tab)

        # カレンダービュータブ
        cal_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(cal_tab, text="📆 カレンダー")
        self._build_calendar_tab(cal_tab)

        # 分析タブ
        analysis_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(analysis_tab, text="📊 分析")
        self._build_analysis_tab(analysis_tab)

        # 習慣管理タブ
        manage_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(manage_tab, text="⚙ 習慣管理")
        self._build_manage_tab(manage_tab)

        self.status_var = tk.StringVar(value="習慣を記録しましょう!")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#dde", font=("Arial", 9), anchor="w", padx=8
                 ).pack(fill=tk.X, side=tk.BOTTOM)

    # ── 今日の記録タブ ────────────────────────────────────────────

    def _build_today_tab(self, parent):
        # 日付選択
        date_f = tk.Frame(parent, bg="#f8f9fc")
        date_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(date_f, text="日付:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.log_date_var = tk.StringVar(value=date.today().isoformat())
        ttk.Entry(date_f, textvariable=self.log_date_var,
                  width=12).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="今日",
                   command=lambda: (
                       self.log_date_var.set(date.today().isoformat()),
                       self._refresh_today())).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="< 前日",
                   command=self._prev_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="翌日 >",
                   command=self._next_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="🔄 更新",
                   command=self._refresh_today).pack(side=tk.LEFT, padx=4)

        # 習慣チェックリスト
        list_f = ttk.LabelFrame(parent, text="習慣チェックリスト", padding=8)
        list_f.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)

        canvas = tk.Canvas(list_f, bg="#f8f9fc", highlightthickness=0)
        sb = ttk.Scrollbar(list_f, orient=tk.VERTICAL, command=canvas.yview)
        canvas.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        canvas.pack(fill=tk.BOTH, expand=True)

        self.check_frame = tk.Frame(canvas, bg="#f8f9fc")
        canvas.create_window((0, 0), window=self.check_frame, anchor="nw")
        self.check_frame.bind("<Configure>",
                              lambda e: canvas.configure(
                                  scrollregion=canvas.bbox("all")))

        # 今日の進捗
        prog_f = tk.Frame(parent, bg="#f8f9fc")
        prog_f.pack(fill=tk.X, padx=8, pady=4)
        self.progress_label = tk.Label(prog_f, text="",
                                        bg="#f8f9fc", font=("Arial", 11, "bold"))
        self.progress_label.pack(side=tk.LEFT)
        self.day_progress = ttk.Progressbar(prog_f, length=300,
                                             maximum=100, mode="determinate")
        self.day_progress.pack(side=tk.LEFT, padx=8)

        self._check_vars = {}

    def _refresh_today(self):
        log_date = self.log_date_var.get()
        for widget in self.check_frame.winfo_children():
            widget.destroy()
        self._check_vars.clear()

        habits = self.conn.execute(
            "SELECT id, name, description, color FROM habits WHERE active=1 "
            "ORDER BY id").fetchall()

        for habit_id, name, desc, color in habits:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date)).fetchone()
            done = bool(row and row[0]) if row else False

            var = tk.BooleanVar(value=done)
            self._check_vars[habit_id] = var

            card = tk.Frame(self.check_frame, bg="white", relief=tk.FLAT, bd=1)
            card.pack(fill=tk.X, padx=4, pady=3, ipady=4)

            # 色インジケーター
            tk.Frame(card, bg=color, width=6).pack(side=tk.LEFT, fill=tk.Y)

            # チェックボックス
            cb = tk.Checkbutton(card, variable=var, bg="white",
                                 activebackground="white",
                                 command=lambda hid=habit_id, v=var: self._toggle_habit(hid, v))
            cb.pack(side=tk.LEFT, padx=6)

            info_f = tk.Frame(card, bg="white")
            info_f.pack(side=tk.LEFT, fill=tk.X, expand=True)
            tk.Label(info_f, text=name, bg="white",
                     font=("Arial", 12, "bold" if not done else "normal"),
                     fg="#333" if not done else "#999").pack(anchor="w")
            if desc:
                tk.Label(info_f, text=desc, bg="white",
                         font=("Arial", 9), fg="#888").pack(anchor="w")

            # 連続記録
            streak = self._get_streak(habit_id, log_date)
            streak_lbl = tk.Label(card, text=f"🔥 {streak}日",
                                   bg="white", fg="#ff6f00",
                                   font=("Arial", 10, "bold"))
            streak_lbl.pack(side=tk.RIGHT, padx=8)

        self._update_day_progress(len(habits))

    def _toggle_habit(self, habit_id, var):
        log_date = self.log_date_var.get()
        done = var.get()
        if done:
            self.conn.execute(
                "INSERT OR REPLACE INTO habit_logs "
                "(habit_id, log_date, completed) VALUES (?,?,1)",
                (habit_id, log_date))
        else:
            self.conn.execute(
                "DELETE FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date))
        self.conn.commit()
        total = len(self._check_vars)
        self._update_day_progress(total)
        self._refresh_today()

    def _update_day_progress(self, total):
        if not total:
            return
        done = sum(1 for v in self._check_vars.values() if v.get())
        pct = int(done / total * 100)
        self.day_progress["value"] = pct
        self.progress_label.config(
            text=f"{done}/{total} 完了 ({pct}%)",
            fg="#2e7d32" if pct == 100 else "#333")

    def _get_streak(self, habit_id, until_date):
        """連続達成日数を計算"""
        streak = 0
        d = datetime.fromisoformat(until_date).date()
        while True:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, d.isoformat())).fetchone()
            if row and row[0]:
                streak += 1
                d -= timedelta(days=1)
            else:
                break
        return streak

    def _prev_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() - timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    def _next_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() + timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    # ── カレンダータブ ────────────────────────────────────────────

    def _build_calendar_tab(self, parent):
        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)

        self.cal_year_var = tk.IntVar(value=date.today().year)
        self.cal_month_var = tk.IntVar(value=date.today().month)
        ttk.Button(ctrl_f, text="◀",
                   command=self._cal_prev_month).pack(side=tk.LEFT)
        self.cal_title = tk.Label(ctrl_f, text="",
                                   bg="#f8f9fc", font=("Arial", 13, "bold"))
        self.cal_title.pack(side=tk.LEFT, padx=12)
        ttk.Button(ctrl_f, text="▶",
                   command=self._cal_next_month).pack(side=tk.LEFT)

        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.cal_habit_var = tk.StringVar(value="すべて")
        self.cal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.cal_habit_var,
                                          state="readonly", width=14)
        self.cal_habit_cb.pack(side=tk.LEFT)
        self.cal_habit_cb.bind("<<ComboboxSelected>>", lambda e: self._refresh_calendar())

        self.cal_canvas = tk.Canvas(parent, bg="#f8f9fc", highlightthickness=0)
        self.cal_canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self._refresh_calendar()

    def _cal_prev_month(self):
        m = self.cal_month_var.get() - 1
        y = self.cal_year_var.get()
        if m < 1:
            m = 12; y -= 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _cal_next_month(self):
        m = self.cal_month_var.get() + 1
        y = self.cal_year_var.get()
        if m > 12:
            m = 1; y += 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _refresh_calendar(self):
        import calendar
        y = self.cal_year_var.get()
        m = self.cal_month_var.get()
        self.cal_title.config(text=f"{y}年 {m}月")
        self.cal_canvas.delete("all")

        # 月の達成率データ
        habit_name = self.cal_habit_var.get()
        habit_id = None
        if habit_name != "すべて":
            row = self.conn.execute("SELECT id FROM habits WHERE name=?",
                                    (habit_name,)).fetchone()
            if row:
                habit_id = row[0]

        CELL_W, CELL_H = 52, 52
        DAYS = ["月", "火", "水", "木", "金", "土", "日"]
        x0, y0 = 20, 20

        for i, d in enumerate(DAYS):
            color = "#f44336" if d == "日" else "#1976d2" if d == "土" else "#333"
            self.cal_canvas.create_text(
                x0 + i * CELL_W + CELL_W // 2, y0,
                text=d, fill=color, font=("Arial", 11, "bold"))

        cal = calendar.monthcalendar(y, m)
        today = date.today()
        total_habits = self.conn.execute(
            "SELECT COUNT(*) FROM habits WHERE active=1").fetchone()[0]

        for week_idx, week in enumerate(cal):
            for day_idx, day in enumerate(week):
                if day == 0:
                    continue
                dx = x0 + day_idx * CELL_W
                dy = y0 + 30 + week_idx * CELL_H
                d_str = f"{y}-{m:02d}-{day:02d}"
                d_date = date(y, m, day)

                # 達成数を取得
                if habit_id:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                        "AND log_date=? AND completed=1",
                        (habit_id, d_str)).fetchone()[0]
                    total = 1
                else:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs l "
                        "JOIN habits h ON l.habit_id=h.id "
                        "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                        (d_str,)).fetchone()[0]
                    total = total_habits

                # セル色
                if d_date > today:
                    bg = "#f5f5f5"
                elif total > 0 and done == total:
                    bg = "#a5d6a7"  # 全達成
                elif done > 0:
                    bg = "#fff9c4"  # 一部達成
                else:
                    bg = "#ffcdd2"  # 未達成

                self.cal_canvas.create_rectangle(
                    dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                    fill=bg, outline="#ccc")

                day_color = "#f44336" if day_idx == 6 else "#1976d2" if day_idx == 5 else "#333"
                if d_date == today:
                    day_color = "#0d47a1"
                    self.cal_canvas.create_rectangle(
                        dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                        fill=bg, outline="#0d47a1", width=2)
                self.cal_canvas.create_text(
                    dx + CELL_W // 2, dy + 16,
                    text=str(day), fill=day_color, font=("Arial", 10))
                if total > 0:
                    self.cal_canvas.create_text(
                        dx + CELL_W // 2, dy + 34,
                        text=f"{done}/{total}", fill="#555", font=("Arial", 8))

    # ── 分析タブ ──────────────────────────────────────────────────

    def _build_analysis_tab(self, parent):
        if not MPL_AVAILABLE:
            tk.Label(parent,
                     text="⚠ matplotlib が未インストールです (pip install matplotlib)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)
            return

        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.anal_habit_var = tk.StringVar(value="すべて")
        self.anal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.anal_habit_var,
                                           state="readonly", width=14)
        self.anal_habit_cb.pack(side=tk.LEFT, padx=4)
        tk.Label(ctrl_f, text="期間:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT, padx=(8, 2))
        self.period_var = tk.StringVar(value="30日")
        ttk.Combobox(ctrl_f, textvariable=self.period_var,
                     values=["7日", "30日", "90日"],
                     state="readonly", width=6).pack(side=tk.LEFT)
        ttk.Button(ctrl_f, text="🔄 更新",
                   command=self._update_analysis).pack(side=tk.LEFT, padx=8)

        fig = Figure(figsize=(10, 5), facecolor="#f8f9fc")
        self.anal_axes = [fig.add_subplot(1, 2, i + 1) for i in range(2)]
        fig.tight_layout(pad=2.0)
        self.anal_canvas_widget = FigureCanvasTkAgg(fig, master=parent)
        self.anal_canvas_widget.get_tk_widget().pack(fill=tk.BOTH, expand=True)
        self.anal_fig = fig

        # 統計テキスト
        self.anal_stats = tk.Text(parent, height=4, bg="#1e1e1e", fg="#c9d1d9",
                                   font=("Courier New", 10), state=tk.DISABLED)
        self.anal_stats.pack(fill=tk.X, padx=8, pady=4)

    def _update_analysis(self):
        if not MPL_AVAILABLE:
            return
        period_map = {"7日": 7, "30日": 30, "90日": 90}
        days = period_map.get(self.period_var.get(), 30)
        today = date.today()
        start = today - timedelta(days=days - 1)

        habits = self.conn.execute(
            "SELECT id, name, color FROM habits WHERE active=1").fetchall()
        if not habits:
            return

        for ax in self.anal_axes:
            ax.clear()

        # グラフ1: 習慣別達成率
        names = []
        rates = []
        colors = []
        for hid, name, color in habits:
            done = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                "AND log_date >= ? AND completed=1",
                (hid, start.isoformat())).fetchone()[0]
            rate = done / days * 100
            names.append(name[:6])
            rates.append(rate)
            colors.append(color)

        ax1 = self.anal_axes[0]
        ax1.barh(names, rates, color=colors, alpha=0.8)
        ax1.set_xlim(0, 100)
        ax1.set_xlabel("達成率 (%)")
        ax1.set_title(f"習慣別達成率 (過去{days}日間)")
        for i, (r, n) in enumerate(zip(rates, names)):
            ax1.text(r + 1, i, f"{r:.0f}%", va="center", fontsize=9)

        # グラフ2: 日別総達成数(折れ線)
        ax2 = self.anal_axes[1]
        dates_list = []
        totals = []
        for i in range(days):
            d = start + timedelta(days=i)
            cnt = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs l "
                "JOIN habits h ON l.habit_id=h.id "
                "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                (d.isoformat(),)).fetchone()[0]
            dates_list.append(i)
            totals.append(cnt)

        ax2.plot(dates_list, totals, color="#4caf50", linewidth=2)
        ax2.fill_between(dates_list, totals, alpha=0.2, color="#4caf50")
        ax2.set_title(f"日別達成数 (過去{days}日間)")
        ax2.set_ylabel("達成習慣数")
        ax2.set_ylim(0, max(len(habits), 1))

        self.anal_fig.tight_layout(pad=2.0)
        self.anal_canvas_widget.draw()

        # 統計情報
        total_logs = self.conn.execute(
            "SELECT COUNT(*) FROM habit_logs WHERE log_date >= ? AND completed=1",
            (start.isoformat(),)).fetchone()[0]
        avg = total_logs / days if days else 0
        stats = (f"期間: {start} 〜 {today}  |  "
                 f"合計達成: {total_logs} 回  |  "
                 f"1日平均: {avg:.1f} 習慣\n")
        for hid, name, _ in habits:
            streak = self._get_streak(hid, today.isoformat())
            stats += f"  {name}: 現在の連続 {streak}日  "
        self.anal_stats.config(state=tk.NORMAL)
        self.anal_stats.delete("1.0", tk.END)
        self.anal_stats.insert("1.0", stats)
        self.anal_stats.config(state=tk.DISABLED)

    # ── 習慣管理タブ ──────────────────────────────────────────────

    def _build_manage_tab(self, parent):
        # 追加フォーム
        add_f = ttk.LabelFrame(parent, text="習慣を追加", padding=8)
        add_f.pack(fill=tk.X, padx=8, pady=6)
        for lbl, attr in [("名前:", "new_name_var"), ("説明:", "new_desc_var")]:
            row = tk.Frame(add_f, bg=add_f.cget("background"))
            row.pack(fill=tk.X, pady=2)
            tk.Label(row, text=lbl, bg=row.cget("bg"), width=8,
                     anchor="e").pack(side=tk.LEFT)
            var = tk.StringVar()
            setattr(self, attr, var)
            ttk.Entry(row, textvariable=var, width=30).pack(side=tk.LEFT, padx=4)

        row_c = tk.Frame(add_f, bg=add_f.cget("background"))
        row_c.pack(fill=tk.X, pady=2)
        tk.Label(row_c, text="色:", bg=row_c.cget("bg"), width=8,
                 anchor="e").pack(side=tk.LEFT)
        self.new_color_var = tk.StringVar(value="#4caf50")
        for c in ["#4caf50", "#2196f3", "#ff9800", "#f44336",
                  "#9c27b0", "#009688", "#795548"]:
            tk.Button(row_c, bg=c, width=2, relief=tk.FLAT, bd=1,
                      command=lambda cv=c: self.new_color_var.set(cv)
                      ).pack(side=tk.LEFT, padx=2)
        ttk.Button(add_f, text="➕ 追加",
                   command=self._add_habit).pack(pady=4)

        # 習慣リスト
        cols = ("name", "desc", "streak", "total", "active")
        self.habit_tree = ttk.Treeview(parent, columns=cols, show="headings", height=12)
        for c, h, w in [("name", "名前", 120), ("desc", "説明", 200),
                         ("streak", "連続日数", 80), ("total", "合計達成", 80),
                         ("active", "状態", 60)]:
            self.habit_tree.heading(c, text=h)
            self.habit_tree.column(c, width=w, minwidth=40)
        sb = ttk.Scrollbar(parent, command=self.habit_tree.yview)
        self.habit_tree.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        self.habit_tree.pack(fill=tk.BOTH, expand=True, padx=8)

        btn_f = tk.Frame(parent, bg="#f8f9fc")
        btn_f.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_f, text="🔄 更新",
                   command=self._refresh_habits).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="⏸ 無効化/有効化",
                   command=self._toggle_habit_active).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="🗑 削除",
                   command=self._delete_habit).pack(side=tk.LEFT, padx=4)

    def _add_habit(self):
        name = self.new_name_var.get().strip()
        if not name:
            messagebox.showwarning("警告", "名前を入力してください")
            return
        self.conn.execute(
            "INSERT INTO habits (name, description, color, created_at) VALUES (?,?,?,?)",
            (name, self.new_desc_var.get().strip(),
             self.new_color_var.get(),
             date.today().isoformat()))
        self.conn.commit()
        self.new_name_var.set("")
        self.new_desc_var.set("")
        self._refresh_habits()
        self.status_var.set(f"習慣を追加しました: {name}")

    def _refresh_habits(self):
        habits = self.conn.execute(
            "SELECT id, name, description, active FROM habits ORDER BY id").fetchall()
        habit_names = ["すべて"] + [h[1] for h in habits if h[3]]

        # コンボボックス更新
        for cb_attr in ["cal_habit_cb", "anal_habit_cb"]:
            if hasattr(self, cb_attr):
                getattr(self, cb_attr).configure(values=habit_names)

        if hasattr(self, "habit_tree"):
            self.habit_tree.delete(*self.habit_tree.get_children())
            today = date.today()
            for hid, name, desc, active in habits:
                streak = self._get_streak(hid, today.isoformat())
                total = self.conn.execute(
                    "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? AND completed=1",
                    (hid,)).fetchone()[0]
                state = "有効" if active else "無効"
                self.habit_tree.insert("", "end", iid=str(hid),
                                       values=(name, desc or "", f"🔥{streak}日",
                                               f"{total}回", state))

        if hasattr(self, "_check_vars"):
            self._refresh_today()

    def _toggle_habit_active(self):
        sel = self.habit_tree.selection()
        if sel:
            hid = int(sel[0])
            row = self.conn.execute("SELECT active FROM habits WHERE id=?",
                                    (hid,)).fetchone()
            if row:
                new_val = 0 if row[0] else 1
                self.conn.execute("UPDATE habits SET active=? WHERE id=?",
                                  (new_val, hid))
                self.conn.commit()
                self._refresh_habits()

    def _delete_habit(self):
        sel = self.habit_tree.selection()
        if sel and messagebox.askyesno("確認", "習慣とすべてのログを削除しますか?"):
            hid = int(sel[0])
            self.conn.execute("DELETE FROM habit_logs WHERE habit_id=?", (hid,))
            self.conn.execute("DELETE FROM habits WHERE id=?", (hid,))
            self.conn.commit()
            self._refresh_habits()


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

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

結果表示にはtk.Textウィジェットを使います。state=tk.DISABLEDでユーザーが直接編集できないようにし、表示前にNORMALに切り替えてからinsert()で内容を更新します。

import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3
import os
from datetime import datetime, timedelta, date

try:
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    MPL_AVAILABLE = True
except ImportError:
    MPL_AVAILABLE = False


class App42:
    """習慣トラッカー(分析付き)"""

    DB_PATH = os.path.join(os.path.dirname(__file__), "habits.db")

    def __init__(self, root):
        self.root = root
        self.root.title("習慣トラッカー(分析付き)")
        self.root.geometry("1000x680")
        self.root.configure(bg="#f8f9fc")
        self._init_db()
        self._build_ui()
        self._refresh_habits()

    def _init_db(self):
        self.conn = sqlite3.connect(self.DB_PATH)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habits (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                description TEXT,
                color TEXT DEFAULT '#4caf50',
                created_at TEXT,
                active INTEGER DEFAULT 1
            )
        """)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habit_logs (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                habit_id INTEGER,
                log_date TEXT,
                completed INTEGER DEFAULT 1,
                note TEXT,
                UNIQUE(habit_id, log_date),
                FOREIGN KEY(habit_id) REFERENCES habits(id)
            )
        """)
        self.conn.commit()
        # サンプルデータ挿入
        if not self.conn.execute("SELECT 1 FROM habits").fetchone():
            samples = [("早起き", "7時前に起床", "#ff9800"),
                       ("運動", "30分以上の運動", "#2196f3"),
                       ("読書", "30分以上の読書", "#9c27b0"),
                       ("瞑想", "10分以上の瞑想", "#009688"),
                       ("日記", "日記を書く", "#f44336")]
            for name, desc, color in samples:
                self.conn.execute(
                    "INSERT INTO habits (name, description, color, created_at) "
                    "VALUES (?,?,?,?)",
                    (name, desc, color, date.today().isoformat()))
            self.conn.commit()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#388e3c", pady=8)
        header.pack(fill=tk.X)
        tk.Label(header, text="📈 習慣トラッカー",
                 font=("Noto Sans JP", 13, "bold"),
                 bg="#388e3c", fg="white").pack(side=tk.LEFT, padx=12)
        self.today_label = tk.Label(header,
                                     text=f"今日: {date.today().strftime('%Y年%m月%d日')}",
                                     bg="#388e3c", fg="#c8e6c9",
                                     font=("Arial", 10))
        self.today_label.pack(side=tk.RIGHT, padx=12)

        notebook = ttk.Notebook(self.root)
        notebook.pack(fill=tk.BOTH, expand=True, padx=6, pady=6)

        # 今日の記録タブ
        today_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(today_tab, text="📅 今日の記録")
        self._build_today_tab(today_tab)

        # カレンダービュータブ
        cal_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(cal_tab, text="📆 カレンダー")
        self._build_calendar_tab(cal_tab)

        # 分析タブ
        analysis_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(analysis_tab, text="📊 分析")
        self._build_analysis_tab(analysis_tab)

        # 習慣管理タブ
        manage_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(manage_tab, text="⚙ 習慣管理")
        self._build_manage_tab(manage_tab)

        self.status_var = tk.StringVar(value="習慣を記録しましょう!")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#dde", font=("Arial", 9), anchor="w", padx=8
                 ).pack(fill=tk.X, side=tk.BOTTOM)

    # ── 今日の記録タブ ────────────────────────────────────────────

    def _build_today_tab(self, parent):
        # 日付選択
        date_f = tk.Frame(parent, bg="#f8f9fc")
        date_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(date_f, text="日付:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.log_date_var = tk.StringVar(value=date.today().isoformat())
        ttk.Entry(date_f, textvariable=self.log_date_var,
                  width=12).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="今日",
                   command=lambda: (
                       self.log_date_var.set(date.today().isoformat()),
                       self._refresh_today())).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="< 前日",
                   command=self._prev_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="翌日 >",
                   command=self._next_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="🔄 更新",
                   command=self._refresh_today).pack(side=tk.LEFT, padx=4)

        # 習慣チェックリスト
        list_f = ttk.LabelFrame(parent, text="習慣チェックリスト", padding=8)
        list_f.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)

        canvas = tk.Canvas(list_f, bg="#f8f9fc", highlightthickness=0)
        sb = ttk.Scrollbar(list_f, orient=tk.VERTICAL, command=canvas.yview)
        canvas.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        canvas.pack(fill=tk.BOTH, expand=True)

        self.check_frame = tk.Frame(canvas, bg="#f8f9fc")
        canvas.create_window((0, 0), window=self.check_frame, anchor="nw")
        self.check_frame.bind("<Configure>",
                              lambda e: canvas.configure(
                                  scrollregion=canvas.bbox("all")))

        # 今日の進捗
        prog_f = tk.Frame(parent, bg="#f8f9fc")
        prog_f.pack(fill=tk.X, padx=8, pady=4)
        self.progress_label = tk.Label(prog_f, text="",
                                        bg="#f8f9fc", font=("Arial", 11, "bold"))
        self.progress_label.pack(side=tk.LEFT)
        self.day_progress = ttk.Progressbar(prog_f, length=300,
                                             maximum=100, mode="determinate")
        self.day_progress.pack(side=tk.LEFT, padx=8)

        self._check_vars = {}

    def _refresh_today(self):
        log_date = self.log_date_var.get()
        for widget in self.check_frame.winfo_children():
            widget.destroy()
        self._check_vars.clear()

        habits = self.conn.execute(
            "SELECT id, name, description, color FROM habits WHERE active=1 "
            "ORDER BY id").fetchall()

        for habit_id, name, desc, color in habits:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date)).fetchone()
            done = bool(row and row[0]) if row else False

            var = tk.BooleanVar(value=done)
            self._check_vars[habit_id] = var

            card = tk.Frame(self.check_frame, bg="white", relief=tk.FLAT, bd=1)
            card.pack(fill=tk.X, padx=4, pady=3, ipady=4)

            # 色インジケーター
            tk.Frame(card, bg=color, width=6).pack(side=tk.LEFT, fill=tk.Y)

            # チェックボックス
            cb = tk.Checkbutton(card, variable=var, bg="white",
                                 activebackground="white",
                                 command=lambda hid=habit_id, v=var: self._toggle_habit(hid, v))
            cb.pack(side=tk.LEFT, padx=6)

            info_f = tk.Frame(card, bg="white")
            info_f.pack(side=tk.LEFT, fill=tk.X, expand=True)
            tk.Label(info_f, text=name, bg="white",
                     font=("Arial", 12, "bold" if not done else "normal"),
                     fg="#333" if not done else "#999").pack(anchor="w")
            if desc:
                tk.Label(info_f, text=desc, bg="white",
                         font=("Arial", 9), fg="#888").pack(anchor="w")

            # 連続記録
            streak = self._get_streak(habit_id, log_date)
            streak_lbl = tk.Label(card, text=f"🔥 {streak}日",
                                   bg="white", fg="#ff6f00",
                                   font=("Arial", 10, "bold"))
            streak_lbl.pack(side=tk.RIGHT, padx=8)

        self._update_day_progress(len(habits))

    def _toggle_habit(self, habit_id, var):
        log_date = self.log_date_var.get()
        done = var.get()
        if done:
            self.conn.execute(
                "INSERT OR REPLACE INTO habit_logs "
                "(habit_id, log_date, completed) VALUES (?,?,1)",
                (habit_id, log_date))
        else:
            self.conn.execute(
                "DELETE FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date))
        self.conn.commit()
        total = len(self._check_vars)
        self._update_day_progress(total)
        self._refresh_today()

    def _update_day_progress(self, total):
        if not total:
            return
        done = sum(1 for v in self._check_vars.values() if v.get())
        pct = int(done / total * 100)
        self.day_progress["value"] = pct
        self.progress_label.config(
            text=f"{done}/{total} 完了 ({pct}%)",
            fg="#2e7d32" if pct == 100 else "#333")

    def _get_streak(self, habit_id, until_date):
        """連続達成日数を計算"""
        streak = 0
        d = datetime.fromisoformat(until_date).date()
        while True:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, d.isoformat())).fetchone()
            if row and row[0]:
                streak += 1
                d -= timedelta(days=1)
            else:
                break
        return streak

    def _prev_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() - timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    def _next_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() + timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    # ── カレンダータブ ────────────────────────────────────────────

    def _build_calendar_tab(self, parent):
        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)

        self.cal_year_var = tk.IntVar(value=date.today().year)
        self.cal_month_var = tk.IntVar(value=date.today().month)
        ttk.Button(ctrl_f, text="◀",
                   command=self._cal_prev_month).pack(side=tk.LEFT)
        self.cal_title = tk.Label(ctrl_f, text="",
                                   bg="#f8f9fc", font=("Arial", 13, "bold"))
        self.cal_title.pack(side=tk.LEFT, padx=12)
        ttk.Button(ctrl_f, text="▶",
                   command=self._cal_next_month).pack(side=tk.LEFT)

        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.cal_habit_var = tk.StringVar(value="すべて")
        self.cal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.cal_habit_var,
                                          state="readonly", width=14)
        self.cal_habit_cb.pack(side=tk.LEFT)
        self.cal_habit_cb.bind("<<ComboboxSelected>>", lambda e: self._refresh_calendar())

        self.cal_canvas = tk.Canvas(parent, bg="#f8f9fc", highlightthickness=0)
        self.cal_canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self._refresh_calendar()

    def _cal_prev_month(self):
        m = self.cal_month_var.get() - 1
        y = self.cal_year_var.get()
        if m < 1:
            m = 12; y -= 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _cal_next_month(self):
        m = self.cal_month_var.get() + 1
        y = self.cal_year_var.get()
        if m > 12:
            m = 1; y += 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _refresh_calendar(self):
        import calendar
        y = self.cal_year_var.get()
        m = self.cal_month_var.get()
        self.cal_title.config(text=f"{y}年 {m}月")
        self.cal_canvas.delete("all")

        # 月の達成率データ
        habit_name = self.cal_habit_var.get()
        habit_id = None
        if habit_name != "すべて":
            row = self.conn.execute("SELECT id FROM habits WHERE name=?",
                                    (habit_name,)).fetchone()
            if row:
                habit_id = row[0]

        CELL_W, CELL_H = 52, 52
        DAYS = ["月", "火", "水", "木", "金", "土", "日"]
        x0, y0 = 20, 20

        for i, d in enumerate(DAYS):
            color = "#f44336" if d == "日" else "#1976d2" if d == "土" else "#333"
            self.cal_canvas.create_text(
                x0 + i * CELL_W + CELL_W // 2, y0,
                text=d, fill=color, font=("Arial", 11, "bold"))

        cal = calendar.monthcalendar(y, m)
        today = date.today()
        total_habits = self.conn.execute(
            "SELECT COUNT(*) FROM habits WHERE active=1").fetchone()[0]

        for week_idx, week in enumerate(cal):
            for day_idx, day in enumerate(week):
                if day == 0:
                    continue
                dx = x0 + day_idx * CELL_W
                dy = y0 + 30 + week_idx * CELL_H
                d_str = f"{y}-{m:02d}-{day:02d}"
                d_date = date(y, m, day)

                # 達成数を取得
                if habit_id:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                        "AND log_date=? AND completed=1",
                        (habit_id, d_str)).fetchone()[0]
                    total = 1
                else:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs l "
                        "JOIN habits h ON l.habit_id=h.id "
                        "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                        (d_str,)).fetchone()[0]
                    total = total_habits

                # セル色
                if d_date > today:
                    bg = "#f5f5f5"
                elif total > 0 and done == total:
                    bg = "#a5d6a7"  # 全達成
                elif done > 0:
                    bg = "#fff9c4"  # 一部達成
                else:
                    bg = "#ffcdd2"  # 未達成

                self.cal_canvas.create_rectangle(
                    dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                    fill=bg, outline="#ccc")

                day_color = "#f44336" if day_idx == 6 else "#1976d2" if day_idx == 5 else "#333"
                if d_date == today:
                    day_color = "#0d47a1"
                    self.cal_canvas.create_rectangle(
                        dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                        fill=bg, outline="#0d47a1", width=2)
                self.cal_canvas.create_text(
                    dx + CELL_W // 2, dy + 16,
                    text=str(day), fill=day_color, font=("Arial", 10))
                if total > 0:
                    self.cal_canvas.create_text(
                        dx + CELL_W // 2, dy + 34,
                        text=f"{done}/{total}", fill="#555", font=("Arial", 8))

    # ── 分析タブ ──────────────────────────────────────────────────

    def _build_analysis_tab(self, parent):
        if not MPL_AVAILABLE:
            tk.Label(parent,
                     text="⚠ matplotlib が未インストールです (pip install matplotlib)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)
            return

        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.anal_habit_var = tk.StringVar(value="すべて")
        self.anal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.anal_habit_var,
                                           state="readonly", width=14)
        self.anal_habit_cb.pack(side=tk.LEFT, padx=4)
        tk.Label(ctrl_f, text="期間:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT, padx=(8, 2))
        self.period_var = tk.StringVar(value="30日")
        ttk.Combobox(ctrl_f, textvariable=self.period_var,
                     values=["7日", "30日", "90日"],
                     state="readonly", width=6).pack(side=tk.LEFT)
        ttk.Button(ctrl_f, text="🔄 更新",
                   command=self._update_analysis).pack(side=tk.LEFT, padx=8)

        fig = Figure(figsize=(10, 5), facecolor="#f8f9fc")
        self.anal_axes = [fig.add_subplot(1, 2, i + 1) for i in range(2)]
        fig.tight_layout(pad=2.0)
        self.anal_canvas_widget = FigureCanvasTkAgg(fig, master=parent)
        self.anal_canvas_widget.get_tk_widget().pack(fill=tk.BOTH, expand=True)
        self.anal_fig = fig

        # 統計テキスト
        self.anal_stats = tk.Text(parent, height=4, bg="#1e1e1e", fg="#c9d1d9",
                                   font=("Courier New", 10), state=tk.DISABLED)
        self.anal_stats.pack(fill=tk.X, padx=8, pady=4)

    def _update_analysis(self):
        if not MPL_AVAILABLE:
            return
        period_map = {"7日": 7, "30日": 30, "90日": 90}
        days = period_map.get(self.period_var.get(), 30)
        today = date.today()
        start = today - timedelta(days=days - 1)

        habits = self.conn.execute(
            "SELECT id, name, color FROM habits WHERE active=1").fetchall()
        if not habits:
            return

        for ax in self.anal_axes:
            ax.clear()

        # グラフ1: 習慣別達成率
        names = []
        rates = []
        colors = []
        for hid, name, color in habits:
            done = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                "AND log_date >= ? AND completed=1",
                (hid, start.isoformat())).fetchone()[0]
            rate = done / days * 100
            names.append(name[:6])
            rates.append(rate)
            colors.append(color)

        ax1 = self.anal_axes[0]
        ax1.barh(names, rates, color=colors, alpha=0.8)
        ax1.set_xlim(0, 100)
        ax1.set_xlabel("達成率 (%)")
        ax1.set_title(f"習慣別達成率 (過去{days}日間)")
        for i, (r, n) in enumerate(zip(rates, names)):
            ax1.text(r + 1, i, f"{r:.0f}%", va="center", fontsize=9)

        # グラフ2: 日別総達成数(折れ線)
        ax2 = self.anal_axes[1]
        dates_list = []
        totals = []
        for i in range(days):
            d = start + timedelta(days=i)
            cnt = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs l "
                "JOIN habits h ON l.habit_id=h.id "
                "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                (d.isoformat(),)).fetchone()[0]
            dates_list.append(i)
            totals.append(cnt)

        ax2.plot(dates_list, totals, color="#4caf50", linewidth=2)
        ax2.fill_between(dates_list, totals, alpha=0.2, color="#4caf50")
        ax2.set_title(f"日別達成数 (過去{days}日間)")
        ax2.set_ylabel("達成習慣数")
        ax2.set_ylim(0, max(len(habits), 1))

        self.anal_fig.tight_layout(pad=2.0)
        self.anal_canvas_widget.draw()

        # 統計情報
        total_logs = self.conn.execute(
            "SELECT COUNT(*) FROM habit_logs WHERE log_date >= ? AND completed=1",
            (start.isoformat(),)).fetchone()[0]
        avg = total_logs / days if days else 0
        stats = (f"期間: {start} 〜 {today}  |  "
                 f"合計達成: {total_logs} 回  |  "
                 f"1日平均: {avg:.1f} 習慣\n")
        for hid, name, _ in habits:
            streak = self._get_streak(hid, today.isoformat())
            stats += f"  {name}: 現在の連続 {streak}日  "
        self.anal_stats.config(state=tk.NORMAL)
        self.anal_stats.delete("1.0", tk.END)
        self.anal_stats.insert("1.0", stats)
        self.anal_stats.config(state=tk.DISABLED)

    # ── 習慣管理タブ ──────────────────────────────────────────────

    def _build_manage_tab(self, parent):
        # 追加フォーム
        add_f = ttk.LabelFrame(parent, text="習慣を追加", padding=8)
        add_f.pack(fill=tk.X, padx=8, pady=6)
        for lbl, attr in [("名前:", "new_name_var"), ("説明:", "new_desc_var")]:
            row = tk.Frame(add_f, bg=add_f.cget("background"))
            row.pack(fill=tk.X, pady=2)
            tk.Label(row, text=lbl, bg=row.cget("bg"), width=8,
                     anchor="e").pack(side=tk.LEFT)
            var = tk.StringVar()
            setattr(self, attr, var)
            ttk.Entry(row, textvariable=var, width=30).pack(side=tk.LEFT, padx=4)

        row_c = tk.Frame(add_f, bg=add_f.cget("background"))
        row_c.pack(fill=tk.X, pady=2)
        tk.Label(row_c, text="色:", bg=row_c.cget("bg"), width=8,
                 anchor="e").pack(side=tk.LEFT)
        self.new_color_var = tk.StringVar(value="#4caf50")
        for c in ["#4caf50", "#2196f3", "#ff9800", "#f44336",
                  "#9c27b0", "#009688", "#795548"]:
            tk.Button(row_c, bg=c, width=2, relief=tk.FLAT, bd=1,
                      command=lambda cv=c: self.new_color_var.set(cv)
                      ).pack(side=tk.LEFT, padx=2)
        ttk.Button(add_f, text="➕ 追加",
                   command=self._add_habit).pack(pady=4)

        # 習慣リスト
        cols = ("name", "desc", "streak", "total", "active")
        self.habit_tree = ttk.Treeview(parent, columns=cols, show="headings", height=12)
        for c, h, w in [("name", "名前", 120), ("desc", "説明", 200),
                         ("streak", "連続日数", 80), ("total", "合計達成", 80),
                         ("active", "状態", 60)]:
            self.habit_tree.heading(c, text=h)
            self.habit_tree.column(c, width=w, minwidth=40)
        sb = ttk.Scrollbar(parent, command=self.habit_tree.yview)
        self.habit_tree.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        self.habit_tree.pack(fill=tk.BOTH, expand=True, padx=8)

        btn_f = tk.Frame(parent, bg="#f8f9fc")
        btn_f.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_f, text="🔄 更新",
                   command=self._refresh_habits).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="⏸ 無効化/有効化",
                   command=self._toggle_habit_active).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="🗑 削除",
                   command=self._delete_habit).pack(side=tk.LEFT, padx=4)

    def _add_habit(self):
        name = self.new_name_var.get().strip()
        if not name:
            messagebox.showwarning("警告", "名前を入力してください")
            return
        self.conn.execute(
            "INSERT INTO habits (name, description, color, created_at) VALUES (?,?,?,?)",
            (name, self.new_desc_var.get().strip(),
             self.new_color_var.get(),
             date.today().isoformat()))
        self.conn.commit()
        self.new_name_var.set("")
        self.new_desc_var.set("")
        self._refresh_habits()
        self.status_var.set(f"習慣を追加しました: {name}")

    def _refresh_habits(self):
        habits = self.conn.execute(
            "SELECT id, name, description, active FROM habits ORDER BY id").fetchall()
        habit_names = ["すべて"] + [h[1] for h in habits if h[3]]

        # コンボボックス更新
        for cb_attr in ["cal_habit_cb", "anal_habit_cb"]:
            if hasattr(self, cb_attr):
                getattr(self, cb_attr).configure(values=habit_names)

        if hasattr(self, "habit_tree"):
            self.habit_tree.delete(*self.habit_tree.get_children())
            today = date.today()
            for hid, name, desc, active in habits:
                streak = self._get_streak(hid, today.isoformat())
                total = self.conn.execute(
                    "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? AND completed=1",
                    (hid,)).fetchone()[0]
                state = "有効" if active else "無効"
                self.habit_tree.insert("", "end", iid=str(hid),
                                       values=(name, desc or "", f"🔥{streak}日",
                                               f"{total}回", state))

        if hasattr(self, "_check_vars"):
            self._refresh_today()

    def _toggle_habit_active(self):
        sel = self.habit_tree.selection()
        if sel:
            hid = int(sel[0])
            row = self.conn.execute("SELECT active FROM habits WHERE id=?",
                                    (hid,)).fetchone()
            if row:
                new_val = 0 if row[0] else 1
                self.conn.execute("UPDATE habits SET active=? WHERE id=?",
                                  (new_val, hid))
                self.conn.commit()
                self._refresh_habits()

    def _delete_habit(self):
        sel = self.habit_tree.selection()
        if sel and messagebox.askyesno("確認", "習慣とすべてのログを削除しますか?"):
            hid = int(sel[0])
            self.conn.execute("DELETE FROM habit_logs WHERE habit_id=?", (hid,))
            self.conn.execute("DELETE FROM habits WHERE id=?", (hid,))
            self.conn.commit()
            self._refresh_habits()


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

例外処理とmessagebox

try-except で ValueError と Exception を捕捉し、messagebox.showerror() でユーザーにわかりやすいエラーメッセージを表示します。入力バリデーションは必ず実装しましょう。

import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3
import os
from datetime import datetime, timedelta, date

try:
    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    MPL_AVAILABLE = True
except ImportError:
    MPL_AVAILABLE = False


class App42:
    """習慣トラッカー(分析付き)"""

    DB_PATH = os.path.join(os.path.dirname(__file__), "habits.db")

    def __init__(self, root):
        self.root = root
        self.root.title("習慣トラッカー(分析付き)")
        self.root.geometry("1000x680")
        self.root.configure(bg="#f8f9fc")
        self._init_db()
        self._build_ui()
        self._refresh_habits()

    def _init_db(self):
        self.conn = sqlite3.connect(self.DB_PATH)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habits (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                description TEXT,
                color TEXT DEFAULT '#4caf50',
                created_at TEXT,
                active INTEGER DEFAULT 1
            )
        """)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS habit_logs (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                habit_id INTEGER,
                log_date TEXT,
                completed INTEGER DEFAULT 1,
                note TEXT,
                UNIQUE(habit_id, log_date),
                FOREIGN KEY(habit_id) REFERENCES habits(id)
            )
        """)
        self.conn.commit()
        # サンプルデータ挿入
        if not self.conn.execute("SELECT 1 FROM habits").fetchone():
            samples = [("早起き", "7時前に起床", "#ff9800"),
                       ("運動", "30分以上の運動", "#2196f3"),
                       ("読書", "30分以上の読書", "#9c27b0"),
                       ("瞑想", "10分以上の瞑想", "#009688"),
                       ("日記", "日記を書く", "#f44336")]
            for name, desc, color in samples:
                self.conn.execute(
                    "INSERT INTO habits (name, description, color, created_at) "
                    "VALUES (?,?,?,?)",
                    (name, desc, color, date.today().isoformat()))
            self.conn.commit()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#388e3c", pady=8)
        header.pack(fill=tk.X)
        tk.Label(header, text="📈 習慣トラッカー",
                 font=("Noto Sans JP", 13, "bold"),
                 bg="#388e3c", fg="white").pack(side=tk.LEFT, padx=12)
        self.today_label = tk.Label(header,
                                     text=f"今日: {date.today().strftime('%Y年%m月%d日')}",
                                     bg="#388e3c", fg="#c8e6c9",
                                     font=("Arial", 10))
        self.today_label.pack(side=tk.RIGHT, padx=12)

        notebook = ttk.Notebook(self.root)
        notebook.pack(fill=tk.BOTH, expand=True, padx=6, pady=6)

        # 今日の記録タブ
        today_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(today_tab, text="📅 今日の記録")
        self._build_today_tab(today_tab)

        # カレンダービュータブ
        cal_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(cal_tab, text="📆 カレンダー")
        self._build_calendar_tab(cal_tab)

        # 分析タブ
        analysis_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(analysis_tab, text="📊 分析")
        self._build_analysis_tab(analysis_tab)

        # 習慣管理タブ
        manage_tab = tk.Frame(notebook, bg="#f8f9fc")
        notebook.add(manage_tab, text="⚙ 習慣管理")
        self._build_manage_tab(manage_tab)

        self.status_var = tk.StringVar(value="習慣を記録しましょう!")
        tk.Label(self.root, textvariable=self.status_var,
                 bg="#dde", font=("Arial", 9), anchor="w", padx=8
                 ).pack(fill=tk.X, side=tk.BOTTOM)

    # ── 今日の記録タブ ────────────────────────────────────────────

    def _build_today_tab(self, parent):
        # 日付選択
        date_f = tk.Frame(parent, bg="#f8f9fc")
        date_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(date_f, text="日付:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.log_date_var = tk.StringVar(value=date.today().isoformat())
        ttk.Entry(date_f, textvariable=self.log_date_var,
                  width=12).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="今日",
                   command=lambda: (
                       self.log_date_var.set(date.today().isoformat()),
                       self._refresh_today())).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="< 前日",
                   command=self._prev_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="翌日 >",
                   command=self._next_day).pack(side=tk.LEFT, padx=4)
        ttk.Button(date_f, text="🔄 更新",
                   command=self._refresh_today).pack(side=tk.LEFT, padx=4)

        # 習慣チェックリスト
        list_f = ttk.LabelFrame(parent, text="習慣チェックリスト", padding=8)
        list_f.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)

        canvas = tk.Canvas(list_f, bg="#f8f9fc", highlightthickness=0)
        sb = ttk.Scrollbar(list_f, orient=tk.VERTICAL, command=canvas.yview)
        canvas.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        canvas.pack(fill=tk.BOTH, expand=True)

        self.check_frame = tk.Frame(canvas, bg="#f8f9fc")
        canvas.create_window((0, 0), window=self.check_frame, anchor="nw")
        self.check_frame.bind("<Configure>",
                              lambda e: canvas.configure(
                                  scrollregion=canvas.bbox("all")))

        # 今日の進捗
        prog_f = tk.Frame(parent, bg="#f8f9fc")
        prog_f.pack(fill=tk.X, padx=8, pady=4)
        self.progress_label = tk.Label(prog_f, text="",
                                        bg="#f8f9fc", font=("Arial", 11, "bold"))
        self.progress_label.pack(side=tk.LEFT)
        self.day_progress = ttk.Progressbar(prog_f, length=300,
                                             maximum=100, mode="determinate")
        self.day_progress.pack(side=tk.LEFT, padx=8)

        self._check_vars = {}

    def _refresh_today(self):
        log_date = self.log_date_var.get()
        for widget in self.check_frame.winfo_children():
            widget.destroy()
        self._check_vars.clear()

        habits = self.conn.execute(
            "SELECT id, name, description, color FROM habits WHERE active=1 "
            "ORDER BY id").fetchall()

        for habit_id, name, desc, color in habits:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date)).fetchone()
            done = bool(row and row[0]) if row else False

            var = tk.BooleanVar(value=done)
            self._check_vars[habit_id] = var

            card = tk.Frame(self.check_frame, bg="white", relief=tk.FLAT, bd=1)
            card.pack(fill=tk.X, padx=4, pady=3, ipady=4)

            # 色インジケーター
            tk.Frame(card, bg=color, width=6).pack(side=tk.LEFT, fill=tk.Y)

            # チェックボックス
            cb = tk.Checkbutton(card, variable=var, bg="white",
                                 activebackground="white",
                                 command=lambda hid=habit_id, v=var: self._toggle_habit(hid, v))
            cb.pack(side=tk.LEFT, padx=6)

            info_f = tk.Frame(card, bg="white")
            info_f.pack(side=tk.LEFT, fill=tk.X, expand=True)
            tk.Label(info_f, text=name, bg="white",
                     font=("Arial", 12, "bold" if not done else "normal"),
                     fg="#333" if not done else "#999").pack(anchor="w")
            if desc:
                tk.Label(info_f, text=desc, bg="white",
                         font=("Arial", 9), fg="#888").pack(anchor="w")

            # 連続記録
            streak = self._get_streak(habit_id, log_date)
            streak_lbl = tk.Label(card, text=f"🔥 {streak}日",
                                   bg="white", fg="#ff6f00",
                                   font=("Arial", 10, "bold"))
            streak_lbl.pack(side=tk.RIGHT, padx=8)

        self._update_day_progress(len(habits))

    def _toggle_habit(self, habit_id, var):
        log_date = self.log_date_var.get()
        done = var.get()
        if done:
            self.conn.execute(
                "INSERT OR REPLACE INTO habit_logs "
                "(habit_id, log_date, completed) VALUES (?,?,1)",
                (habit_id, log_date))
        else:
            self.conn.execute(
                "DELETE FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, log_date))
        self.conn.commit()
        total = len(self._check_vars)
        self._update_day_progress(total)
        self._refresh_today()

    def _update_day_progress(self, total):
        if not total:
            return
        done = sum(1 for v in self._check_vars.values() if v.get())
        pct = int(done / total * 100)
        self.day_progress["value"] = pct
        self.progress_label.config(
            text=f"{done}/{total} 完了 ({pct}%)",
            fg="#2e7d32" if pct == 100 else "#333")

    def _get_streak(self, habit_id, until_date):
        """連続達成日数を計算"""
        streak = 0
        d = datetime.fromisoformat(until_date).date()
        while True:
            row = self.conn.execute(
                "SELECT completed FROM habit_logs WHERE habit_id=? AND log_date=?",
                (habit_id, d.isoformat())).fetchone()
            if row and row[0]:
                streak += 1
                d -= timedelta(days=1)
            else:
                break
        return streak

    def _prev_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() - timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    def _next_day(self):
        d = datetime.fromisoformat(self.log_date_var.get()).date() + timedelta(days=1)
        self.log_date_var.set(d.isoformat())
        self._refresh_today()

    # ── カレンダータブ ────────────────────────────────────────────

    def _build_calendar_tab(self, parent):
        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)

        self.cal_year_var = tk.IntVar(value=date.today().year)
        self.cal_month_var = tk.IntVar(value=date.today().month)
        ttk.Button(ctrl_f, text="◀",
                   command=self._cal_prev_month).pack(side=tk.LEFT)
        self.cal_title = tk.Label(ctrl_f, text="",
                                   bg="#f8f9fc", font=("Arial", 13, "bold"))
        self.cal_title.pack(side=tk.LEFT, padx=12)
        ttk.Button(ctrl_f, text="▶",
                   command=self._cal_next_month).pack(side=tk.LEFT)

        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.cal_habit_var = tk.StringVar(value="すべて")
        self.cal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.cal_habit_var,
                                          state="readonly", width=14)
        self.cal_habit_cb.pack(side=tk.LEFT)
        self.cal_habit_cb.bind("<<ComboboxSelected>>", lambda e: self._refresh_calendar())

        self.cal_canvas = tk.Canvas(parent, bg="#f8f9fc", highlightthickness=0)
        self.cal_canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self._refresh_calendar()

    def _cal_prev_month(self):
        m = self.cal_month_var.get() - 1
        y = self.cal_year_var.get()
        if m < 1:
            m = 12; y -= 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _cal_next_month(self):
        m = self.cal_month_var.get() + 1
        y = self.cal_year_var.get()
        if m > 12:
            m = 1; y += 1
        self.cal_month_var.set(m); self.cal_year_var.set(y)
        self._refresh_calendar()

    def _refresh_calendar(self):
        import calendar
        y = self.cal_year_var.get()
        m = self.cal_month_var.get()
        self.cal_title.config(text=f"{y}年 {m}月")
        self.cal_canvas.delete("all")

        # 月の達成率データ
        habit_name = self.cal_habit_var.get()
        habit_id = None
        if habit_name != "すべて":
            row = self.conn.execute("SELECT id FROM habits WHERE name=?",
                                    (habit_name,)).fetchone()
            if row:
                habit_id = row[0]

        CELL_W, CELL_H = 52, 52
        DAYS = ["月", "火", "水", "木", "金", "土", "日"]
        x0, y0 = 20, 20

        for i, d in enumerate(DAYS):
            color = "#f44336" if d == "日" else "#1976d2" if d == "土" else "#333"
            self.cal_canvas.create_text(
                x0 + i * CELL_W + CELL_W // 2, y0,
                text=d, fill=color, font=("Arial", 11, "bold"))

        cal = calendar.monthcalendar(y, m)
        today = date.today()
        total_habits = self.conn.execute(
            "SELECT COUNT(*) FROM habits WHERE active=1").fetchone()[0]

        for week_idx, week in enumerate(cal):
            for day_idx, day in enumerate(week):
                if day == 0:
                    continue
                dx = x0 + day_idx * CELL_W
                dy = y0 + 30 + week_idx * CELL_H
                d_str = f"{y}-{m:02d}-{day:02d}"
                d_date = date(y, m, day)

                # 達成数を取得
                if habit_id:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                        "AND log_date=? AND completed=1",
                        (habit_id, d_str)).fetchone()[0]
                    total = 1
                else:
                    done = self.conn.execute(
                        "SELECT COUNT(*) FROM habit_logs l "
                        "JOIN habits h ON l.habit_id=h.id "
                        "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                        (d_str,)).fetchone()[0]
                    total = total_habits

                # セル色
                if d_date > today:
                    bg = "#f5f5f5"
                elif total > 0 and done == total:
                    bg = "#a5d6a7"  # 全達成
                elif done > 0:
                    bg = "#fff9c4"  # 一部達成
                else:
                    bg = "#ffcdd2"  # 未達成

                self.cal_canvas.create_rectangle(
                    dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                    fill=bg, outline="#ccc")

                day_color = "#f44336" if day_idx == 6 else "#1976d2" if day_idx == 5 else "#333"
                if d_date == today:
                    day_color = "#0d47a1"
                    self.cal_canvas.create_rectangle(
                        dx + 2, dy + 2, dx + CELL_W - 2, dy + CELL_H - 2,
                        fill=bg, outline="#0d47a1", width=2)
                self.cal_canvas.create_text(
                    dx + CELL_W // 2, dy + 16,
                    text=str(day), fill=day_color, font=("Arial", 10))
                if total > 0:
                    self.cal_canvas.create_text(
                        dx + CELL_W // 2, dy + 34,
                        text=f"{done}/{total}", fill="#555", font=("Arial", 8))

    # ── 分析タブ ──────────────────────────────────────────────────

    def _build_analysis_tab(self, parent):
        if not MPL_AVAILABLE:
            tk.Label(parent,
                     text="⚠ matplotlib が未インストールです (pip install matplotlib)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)
            return

        ctrl_f = tk.Frame(parent, bg="#f8f9fc")
        ctrl_f.pack(fill=tk.X, padx=8, pady=6)
        tk.Label(ctrl_f, text="習慣:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT)
        self.anal_habit_var = tk.StringVar(value="すべて")
        self.anal_habit_cb = ttk.Combobox(ctrl_f, textvariable=self.anal_habit_var,
                                           state="readonly", width=14)
        self.anal_habit_cb.pack(side=tk.LEFT, padx=4)
        tk.Label(ctrl_f, text="期間:", bg="#f8f9fc",
                 font=("Arial", 10)).pack(side=tk.LEFT, padx=(8, 2))
        self.period_var = tk.StringVar(value="30日")
        ttk.Combobox(ctrl_f, textvariable=self.period_var,
                     values=["7日", "30日", "90日"],
                     state="readonly", width=6).pack(side=tk.LEFT)
        ttk.Button(ctrl_f, text="🔄 更新",
                   command=self._update_analysis).pack(side=tk.LEFT, padx=8)

        fig = Figure(figsize=(10, 5), facecolor="#f8f9fc")
        self.anal_axes = [fig.add_subplot(1, 2, i + 1) for i in range(2)]
        fig.tight_layout(pad=2.0)
        self.anal_canvas_widget = FigureCanvasTkAgg(fig, master=parent)
        self.anal_canvas_widget.get_tk_widget().pack(fill=tk.BOTH, expand=True)
        self.anal_fig = fig

        # 統計テキスト
        self.anal_stats = tk.Text(parent, height=4, bg="#1e1e1e", fg="#c9d1d9",
                                   font=("Courier New", 10), state=tk.DISABLED)
        self.anal_stats.pack(fill=tk.X, padx=8, pady=4)

    def _update_analysis(self):
        if not MPL_AVAILABLE:
            return
        period_map = {"7日": 7, "30日": 30, "90日": 90}
        days = period_map.get(self.period_var.get(), 30)
        today = date.today()
        start = today - timedelta(days=days - 1)

        habits = self.conn.execute(
            "SELECT id, name, color FROM habits WHERE active=1").fetchall()
        if not habits:
            return

        for ax in self.anal_axes:
            ax.clear()

        # グラフ1: 習慣別達成率
        names = []
        rates = []
        colors = []
        for hid, name, color in habits:
            done = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? "
                "AND log_date >= ? AND completed=1",
                (hid, start.isoformat())).fetchone()[0]
            rate = done / days * 100
            names.append(name[:6])
            rates.append(rate)
            colors.append(color)

        ax1 = self.anal_axes[0]
        ax1.barh(names, rates, color=colors, alpha=0.8)
        ax1.set_xlim(0, 100)
        ax1.set_xlabel("達成率 (%)")
        ax1.set_title(f"習慣別達成率 (過去{days}日間)")
        for i, (r, n) in enumerate(zip(rates, names)):
            ax1.text(r + 1, i, f"{r:.0f}%", va="center", fontsize=9)

        # グラフ2: 日別総達成数(折れ線)
        ax2 = self.anal_axes[1]
        dates_list = []
        totals = []
        for i in range(days):
            d = start + timedelta(days=i)
            cnt = self.conn.execute(
                "SELECT COUNT(*) FROM habit_logs l "
                "JOIN habits h ON l.habit_id=h.id "
                "WHERE l.log_date=? AND l.completed=1 AND h.active=1",
                (d.isoformat(),)).fetchone()[0]
            dates_list.append(i)
            totals.append(cnt)

        ax2.plot(dates_list, totals, color="#4caf50", linewidth=2)
        ax2.fill_between(dates_list, totals, alpha=0.2, color="#4caf50")
        ax2.set_title(f"日別達成数 (過去{days}日間)")
        ax2.set_ylabel("達成習慣数")
        ax2.set_ylim(0, max(len(habits), 1))

        self.anal_fig.tight_layout(pad=2.0)
        self.anal_canvas_widget.draw()

        # 統計情報
        total_logs = self.conn.execute(
            "SELECT COUNT(*) FROM habit_logs WHERE log_date >= ? AND completed=1",
            (start.isoformat(),)).fetchone()[0]
        avg = total_logs / days if days else 0
        stats = (f"期間: {start} 〜 {today}  |  "
                 f"合計達成: {total_logs} 回  |  "
                 f"1日平均: {avg:.1f} 習慣\n")
        for hid, name, _ in habits:
            streak = self._get_streak(hid, today.isoformat())
            stats += f"  {name}: 現在の連続 {streak}日  "
        self.anal_stats.config(state=tk.NORMAL)
        self.anal_stats.delete("1.0", tk.END)
        self.anal_stats.insert("1.0", stats)
        self.anal_stats.config(state=tk.DISABLED)

    # ── 習慣管理タブ ──────────────────────────────────────────────

    def _build_manage_tab(self, parent):
        # 追加フォーム
        add_f = ttk.LabelFrame(parent, text="習慣を追加", padding=8)
        add_f.pack(fill=tk.X, padx=8, pady=6)
        for lbl, attr in [("名前:", "new_name_var"), ("説明:", "new_desc_var")]:
            row = tk.Frame(add_f, bg=add_f.cget("background"))
            row.pack(fill=tk.X, pady=2)
            tk.Label(row, text=lbl, bg=row.cget("bg"), width=8,
                     anchor="e").pack(side=tk.LEFT)
            var = tk.StringVar()
            setattr(self, attr, var)
            ttk.Entry(row, textvariable=var, width=30).pack(side=tk.LEFT, padx=4)

        row_c = tk.Frame(add_f, bg=add_f.cget("background"))
        row_c.pack(fill=tk.X, pady=2)
        tk.Label(row_c, text="色:", bg=row_c.cget("bg"), width=8,
                 anchor="e").pack(side=tk.LEFT)
        self.new_color_var = tk.StringVar(value="#4caf50")
        for c in ["#4caf50", "#2196f3", "#ff9800", "#f44336",
                  "#9c27b0", "#009688", "#795548"]:
            tk.Button(row_c, bg=c, width=2, relief=tk.FLAT, bd=1,
                      command=lambda cv=c: self.new_color_var.set(cv)
                      ).pack(side=tk.LEFT, padx=2)
        ttk.Button(add_f, text="➕ 追加",
                   command=self._add_habit).pack(pady=4)

        # 習慣リスト
        cols = ("name", "desc", "streak", "total", "active")
        self.habit_tree = ttk.Treeview(parent, columns=cols, show="headings", height=12)
        for c, h, w in [("name", "名前", 120), ("desc", "説明", 200),
                         ("streak", "連続日数", 80), ("total", "合計達成", 80),
                         ("active", "状態", 60)]:
            self.habit_tree.heading(c, text=h)
            self.habit_tree.column(c, width=w, minwidth=40)
        sb = ttk.Scrollbar(parent, command=self.habit_tree.yview)
        self.habit_tree.configure(yscrollcommand=sb.set)
        sb.pack(side=tk.RIGHT, fill=tk.Y)
        self.habit_tree.pack(fill=tk.BOTH, expand=True, padx=8)

        btn_f = tk.Frame(parent, bg="#f8f9fc")
        btn_f.pack(fill=tk.X, padx=8, pady=4)
        ttk.Button(btn_f, text="🔄 更新",
                   command=self._refresh_habits).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="⏸ 無効化/有効化",
                   command=self._toggle_habit_active).pack(side=tk.LEFT, padx=4)
        ttk.Button(btn_f, text="🗑 削除",
                   command=self._delete_habit).pack(side=tk.LEFT, padx=4)

    def _add_habit(self):
        name = self.new_name_var.get().strip()
        if not name:
            messagebox.showwarning("警告", "名前を入力してください")
            return
        self.conn.execute(
            "INSERT INTO habits (name, description, color, created_at) VALUES (?,?,?,?)",
            (name, self.new_desc_var.get().strip(),
             self.new_color_var.get(),
             date.today().isoformat()))
        self.conn.commit()
        self.new_name_var.set("")
        self.new_desc_var.set("")
        self._refresh_habits()
        self.status_var.set(f"習慣を追加しました: {name}")

    def _refresh_habits(self):
        habits = self.conn.execute(
            "SELECT id, name, description, active FROM habits ORDER BY id").fetchall()
        habit_names = ["すべて"] + [h[1] for h in habits if h[3]]

        # コンボボックス更新
        for cb_attr in ["cal_habit_cb", "anal_habit_cb"]:
            if hasattr(self, cb_attr):
                getattr(self, cb_attr).configure(values=habit_names)

        if hasattr(self, "habit_tree"):
            self.habit_tree.delete(*self.habit_tree.get_children())
            today = date.today()
            for hid, name, desc, active in habits:
                streak = self._get_streak(hid, today.isoformat())
                total = self.conn.execute(
                    "SELECT COUNT(*) FROM habit_logs WHERE habit_id=? AND completed=1",
                    (hid,)).fetchone()[0]
                state = "有効" if active else "無効"
                self.habit_tree.insert("", "end", iid=str(hid),
                                       values=(name, desc or "", f"🔥{streak}日",
                                               f"{total}回", state))

        if hasattr(self, "_check_vars"):
            self._refresh_today()

    def _toggle_habit_active(self):
        sel = self.habit_tree.selection()
        if sel:
            hid = int(sel[0])
            row = self.conn.execute("SELECT active FROM habits WHERE id=?",
                                    (hid,)).fetchone()
            if row:
                new_val = 0 if row[0] else 1
                self.conn.execute("UPDATE habits SET active=? WHERE id=?",
                                  (new_val, hid))
                self.conn.commit()
                self._refresh_habits()

    def _delete_habit(self):
        sel = self.habit_tree.selection()
        if sel and messagebox.askyesno("確認", "習慣とすべてのログを削除しますか?"):
            hid = int(sel[0])
            self.conn.execute("DELETE FROM habit_logs WHERE habit_id=?", (hid,))
            self.conn.execute("DELETE FROM habits WHERE id=?", (hid,))
            self.conn.commit()
            self._refresh_habits()


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

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

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

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

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

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

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

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

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

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

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

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

    _calculate()メソッドに計算・処理ロジックを実装します。

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

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

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

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

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

基本機能を習得したら、以下のカスタマイズに挑戦してみましょう。少しずつ機能を追加することで、Pythonのスキルが飛躍的に向上します。

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

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

💡 データのエクスポート機能

計算結果をCSV・TXTファイルに保存するエクスポート機能を追加しましょう。filedialog.asksaveasfilename()でファイル保存ダイアログが使えます。

💡 入力履歴機能

以前の入力値を覚えておいてComboboxのドロップダウンで再選択できる履歴機能を追加しましょう。

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

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

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

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

❌ ウィンドウのサイズが変更できない

原因:resizable(False, False)が設定されています。

解決法:resizable(True, True)に変更してください。

9. 練習問題

アプリの理解を深めるための練習問題です。難易度順に挑戦してみてください。

  1. 課題1:機能拡張

    習慣トラッカー(分析付き)に新しい機能を1つ追加してみましょう。どんな機能があると便利か考えてから実装してください。

  2. 課題2:UIの改善

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

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

    入力値や計算結果をファイルに保存する機能を追加しましょう。jsonやcsvモジュールを使います。

🚀
次に挑戦するアプリ

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