中級者向け No.46

動画プレイヤー UI

動画ファイルを再生・一時停止・シーク・音量調整できるプレイヤーUI。opencv-pythonとPillowの組み合わせを学びます。

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

1. アプリ概要

動画ファイルを再生・一時停止・シーク・音量調整できるプレイヤーUI。opencv-pythonとPillowの組み合わせを学びます。

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

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

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

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

2. 機能一覧

  • 動画プレイヤー UIのメイン機能
  • 直感的なGUIインターフェース
  • 入力値のバリデーション
  • エラーハンドリング
  • 結果の見やすい表示
  • キーボードショートカット対応

3. 事前準備・環境

ℹ️
動作確認環境

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

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

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

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

pip install pillow

4. 完全なソースコード

💡
コードのコピー方法

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

app46.py
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
import threading
import time

try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    CV2_AVAILABLE = False

try:
    from PIL import Image, ImageTk
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False


class App46:
    """動画プレイヤー UI"""

    def __init__(self, root):
        self.root = root
        self.root.title("動画プレイヤー UI")
        self.root.geometry("860x640")
        self.root.configure(bg="#1a1a2e")
        self._cap = None
        self._playing = False
        self._total_frames = 0
        self._current_frame = 0
        self._fps = 30
        self._volume = 0.5
        self._after_id = None
        self._build_ui()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#16213e", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="🎬 動画プレイヤー UI",
                 font=("Noto Sans JP", 12, "bold"),
                 bg="#16213e", fg="#e94560").pack(side=tk.LEFT, padx=12)
        tk.Button(header, text="📂 ファイルを開く",
                  command=self._open_file,
                  bg="#e94560", fg="white", relief=tk.FLAT,
                  font=("Arial", 10), padx=10, pady=3,
                  activebackground="#c23152", bd=0).pack(side=tk.RIGHT, padx=8)

        if not CV2_AVAILABLE:
            tk.Label(self.root,
                     text="⚠ opencv-python が未インストールです "
                          "(pip install opencv-python Pillow)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)

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

        # ビデオ表示キャンバス
        self.canvas = tk.Canvas(self.root, bg="black", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self.canvas_img = None

        # シークバー
        seek_f = tk.Frame(self.root, bg="#1a1a2e")
        seek_f.pack(fill=tk.X, padx=8, pady=2)
        self.position_var = tk.DoubleVar(value=0)
        self.seek_bar = ttk.Scale(seek_f, from_=0, to=100,
                                   variable=self.position_var,
                                   orient=tk.HORIZONTAL,
                                   command=self._on_seek)
        self.seek_bar.pack(fill=tk.X)

        # 時間表示
        time_f = tk.Frame(self.root, bg="#1a1a2e")
        time_f.pack(fill=tk.X, padx=8)
        self.time_label = tk.Label(time_f, text="00:00 / 00:00",
                                    bg="#1a1a2e", fg="#ccc",
                                    font=("Courier New", 10))
        self.time_label.pack(side=tk.LEFT)
        self.frame_label = tk.Label(time_f, text="フレーム: 0/0",
                                     bg="#1a1a2e", fg="#888",
                                     font=("Arial", 9))
        self.frame_label.pack(side=tk.RIGHT)

        # コントロールパネル
        ctrl_f = tk.Frame(self.root, bg="#16213e", pady=8)
        ctrl_f.pack(fill=tk.X)

        btn_s = {"bg": "#0f3460", "fg": "white", "relief": tk.FLAT,
                 "font": ("Arial", 12), "padx": 12, "pady=4",
                 "activebackground": "#1a4a80", "bd": 0}

        self.rewind_btn = tk.Button(ctrl_f, text="⏪",
                                     command=lambda: self._skip(-10),
                                     bg="#0f3460", fg="white", relief=tk.FLAT,
                                     font=("Arial", 14), padx=10, pady=4,
                                     activebackground="#1a4a80", bd=0)
        self.rewind_btn.pack(side=tk.LEFT, padx=4)

        self.play_btn = tk.Button(ctrl_f, text="▶",
                                   command=self._toggle_play,
                                   bg="#e94560", fg="white", relief=tk.FLAT,
                                   font=("Arial", 16), padx=14, pady=4,
                                   activebackground="#c23152", bd=0)
        self.play_btn.pack(side=tk.LEFT, padx=4)

        self.forward_btn = tk.Button(ctrl_f, text="⏩",
                                      command=lambda: self._skip(10),
                                      bg="#0f3460", fg="white", relief=tk.FLAT,
                                      font=("Arial", 14), padx=10, pady=4,
                                      activebackground="#1a4a80", bd=0)
        self.forward_btn.pack(side=tk.LEFT, padx=4)

        tk.Button(ctrl_f, text="⏹",
                  command=self._stop,
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 14), padx=10, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.LEFT, padx=4)

        # 再生速度
        tk.Label(ctrl_f, text="速度:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.speed_var = tk.DoubleVar(value=1.0)
        self.speed_cb = ttk.Combobox(ctrl_f, textvariable=self.speed_var,
                                      values=[0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
                                      state="readonly", width=5)
        self.speed_cb.pack(side=tk.LEFT)
        self.speed_cb.set("1.0")

        # 音量(ダミー UIとして表示)
        tk.Label(ctrl_f, text="音量:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.vol_var = tk.IntVar(value=50)
        ttk.Scale(ctrl_f, from_=0, to=100, variable=self.vol_var,
                   orient=tk.HORIZONTAL, length=80).pack(side=tk.LEFT)

        # フレームステップ
        tk.Button(ctrl_f, text="◀ 1f",
                  command=lambda: self._step_frame(-1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Button(ctrl_f, text="1f ▶",
                  command=lambda: self._step_frame(1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Label(ctrl_f, text="フレーム:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.RIGHT, padx=4)

        # ファイル情報
        info_f = tk.Frame(self.root, bg="#1a1a2e")
        info_f.pack(fill=tk.X, padx=8)
        self.info_var = tk.StringVar(value="ファイルを開いてください")
        tk.Label(info_f, textvariable=self.info_var,
                 bg="#1a1a2e", fg="#888", font=("Arial", 9)).pack(anchor="w")

        self.root.protocol("WM_DELETE_WINDOW", self._on_close)

    def _open_file(self):
        path = filedialog.askopenfilename(
            filetypes=[("動画ファイル", "*.mp4 *.avi *.mov *.mkv *.wmv *.flv"),
                       ("すべて", "*.*")])
        if path:
            self._load_video(path)

    def _load_video(self, path):
        if not CV2_AVAILABLE or not PIL_AVAILABLE:
            messagebox.showerror("エラー",
                                 "opencv-python と Pillow が必要です:\n"
                                 "pip install opencv-python Pillow")
            return
        self._stop()
        if self._cap:
            self._cap.release()
        cap = cv2.VideoCapture(path)
        if not cap.isOpened():
            messagebox.showerror("エラー", "動画ファイルを開けませんでした")
            return
        self._cap = cap
        self._fps = cap.get(cv2.CAP_PROP_FPS) or 30
        self._total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        self._current_frame = 0
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = self._total_frames / self._fps
        self.seek_bar.configure(to=self._total_frames)
        self.root.title(f"動画プレイヤー — {os.path.basename(path)}")
        self.info_var.set(
            f"{os.path.basename(path)}  |  {w}×{h}  |  "
            f"{self._fps:.1f}fps  |  {self._fmt_time(duration)}")
        # 最初のフレームを表示
        self._show_frame(0)

    def _show_frame(self, frame_idx):
        if not self._cap:
            return
        frame_idx = max(0, min(frame_idx, self._total_frames - 1))
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
        ret, frame = self._cap.read()
        if not ret:
            return
        self._current_frame = frame_idx
        # BGR → RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame_rgb)
        # キャンバスサイズに合わせてリサイズ
        cw = self.canvas.winfo_width() or 640
        ch = self.canvas.winfo_height() or 480
        if cw > 1 and ch > 1:
            img.thumbnail((cw, ch), Image.LANCZOS)
        self.canvas_img = ImageTk.PhotoImage(img)
        self.canvas.delete("all")
        self.canvas.create_image(cw // 2, ch // 2, image=self.canvas_img, anchor="center")
        # UI更新
        self.position_var.set(frame_idx)
        elapsed = frame_idx / self._fps
        total = self._total_frames / self._fps
        self.time_label.config(
            text=f"{self._fmt_time(elapsed)} / {self._fmt_time(total)}")
        self.frame_label.config(
            text=f"フレーム: {frame_idx}/{self._total_frames}")

    def _toggle_play(self):
        if not self._cap:
            messagebox.showinfo("情報", "動画ファイルを開いてください")
            return
        if self._playing:
            self._playing = False
            self.play_btn.config(text="▶")
            if self._after_id:
                self.root.after_cancel(self._after_id)
        else:
            self._playing = True
            self.play_btn.config(text="⏸")
            self._play_loop()

    def _play_loop(self):
        if not self._playing or not self._cap:
            return
        speed = float(self.speed_cb.get())
        delay_ms = max(1, int(1000 / (self._fps * speed)))

        if self._current_frame >= self._total_frames - 1:
            # 末尾に達したら停止
            self._playing = False
            self.play_btn.config(text="▶")
            return

        self._show_frame(self._current_frame + 1)
        self._after_id = self.root.after(delay_ms, self._play_loop)

    def _on_seek(self, value):
        if self._cap:
            frame = int(float(value))
            self._show_frame(frame)

    def _skip(self, seconds):
        if self._cap:
            frames = int(seconds * self._fps)
            new_frame = max(0, min(self._current_frame + frames, self._total_frames - 1))
            self._show_frame(new_frame)

    def _step_frame(self, delta):
        if self._cap:
            self._show_frame(self._current_frame + delta)

    def _stop(self):
        self._playing = False
        self.play_btn.config(text="▶")
        if self._after_id:
            self.root.after_cancel(self._after_id)
            self._after_id = None
        if self._cap:
            self._show_frame(0)

    def _fmt_time(self, seconds):
        m = int(seconds // 60)
        s = int(seconds % 60)
        return f"{m:02d}:{s:02d}"

    def _on_close(self):
        self._playing = False
        if self._after_id:
            self.root.after_cancel(self._after_id)
        if self._cap:
            self._cap.release()
        self.root.destroy()


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

5. コード解説

動画プレイヤー UIのコードを詳しく解説します。クラスベースの設計で各機能を整理して実装しています。

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

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

import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
import threading
import time

try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    CV2_AVAILABLE = False

try:
    from PIL import Image, ImageTk
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False


class App46:
    """動画プレイヤー UI"""

    def __init__(self, root):
        self.root = root
        self.root.title("動画プレイヤー UI")
        self.root.geometry("860x640")
        self.root.configure(bg="#1a1a2e")
        self._cap = None
        self._playing = False
        self._total_frames = 0
        self._current_frame = 0
        self._fps = 30
        self._volume = 0.5
        self._after_id = None
        self._build_ui()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#16213e", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="🎬 動画プレイヤー UI",
                 font=("Noto Sans JP", 12, "bold"),
                 bg="#16213e", fg="#e94560").pack(side=tk.LEFT, padx=12)
        tk.Button(header, text="📂 ファイルを開く",
                  command=self._open_file,
                  bg="#e94560", fg="white", relief=tk.FLAT,
                  font=("Arial", 10), padx=10, pady=3,
                  activebackground="#c23152", bd=0).pack(side=tk.RIGHT, padx=8)

        if not CV2_AVAILABLE:
            tk.Label(self.root,
                     text="⚠ opencv-python が未インストールです "
                          "(pip install opencv-python Pillow)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)

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

        # ビデオ表示キャンバス
        self.canvas = tk.Canvas(self.root, bg="black", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self.canvas_img = None

        # シークバー
        seek_f = tk.Frame(self.root, bg="#1a1a2e")
        seek_f.pack(fill=tk.X, padx=8, pady=2)
        self.position_var = tk.DoubleVar(value=0)
        self.seek_bar = ttk.Scale(seek_f, from_=0, to=100,
                                   variable=self.position_var,
                                   orient=tk.HORIZONTAL,
                                   command=self._on_seek)
        self.seek_bar.pack(fill=tk.X)

        # 時間表示
        time_f = tk.Frame(self.root, bg="#1a1a2e")
        time_f.pack(fill=tk.X, padx=8)
        self.time_label = tk.Label(time_f, text="00:00 / 00:00",
                                    bg="#1a1a2e", fg="#ccc",
                                    font=("Courier New", 10))
        self.time_label.pack(side=tk.LEFT)
        self.frame_label = tk.Label(time_f, text="フレーム: 0/0",
                                     bg="#1a1a2e", fg="#888",
                                     font=("Arial", 9))
        self.frame_label.pack(side=tk.RIGHT)

        # コントロールパネル
        ctrl_f = tk.Frame(self.root, bg="#16213e", pady=8)
        ctrl_f.pack(fill=tk.X)

        btn_s = {"bg": "#0f3460", "fg": "white", "relief": tk.FLAT,
                 "font": ("Arial", 12), "padx": 12, "pady=4",
                 "activebackground": "#1a4a80", "bd": 0}

        self.rewind_btn = tk.Button(ctrl_f, text="⏪",
                                     command=lambda: self._skip(-10),
                                     bg="#0f3460", fg="white", relief=tk.FLAT,
                                     font=("Arial", 14), padx=10, pady=4,
                                     activebackground="#1a4a80", bd=0)
        self.rewind_btn.pack(side=tk.LEFT, padx=4)

        self.play_btn = tk.Button(ctrl_f, text="▶",
                                   command=self._toggle_play,
                                   bg="#e94560", fg="white", relief=tk.FLAT,
                                   font=("Arial", 16), padx=14, pady=4,
                                   activebackground="#c23152", bd=0)
        self.play_btn.pack(side=tk.LEFT, padx=4)

        self.forward_btn = tk.Button(ctrl_f, text="⏩",
                                      command=lambda: self._skip(10),
                                      bg="#0f3460", fg="white", relief=tk.FLAT,
                                      font=("Arial", 14), padx=10, pady=4,
                                      activebackground="#1a4a80", bd=0)
        self.forward_btn.pack(side=tk.LEFT, padx=4)

        tk.Button(ctrl_f, text="⏹",
                  command=self._stop,
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 14), padx=10, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.LEFT, padx=4)

        # 再生速度
        tk.Label(ctrl_f, text="速度:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.speed_var = tk.DoubleVar(value=1.0)
        self.speed_cb = ttk.Combobox(ctrl_f, textvariable=self.speed_var,
                                      values=[0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
                                      state="readonly", width=5)
        self.speed_cb.pack(side=tk.LEFT)
        self.speed_cb.set("1.0")

        # 音量(ダミー UIとして表示)
        tk.Label(ctrl_f, text="音量:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.vol_var = tk.IntVar(value=50)
        ttk.Scale(ctrl_f, from_=0, to=100, variable=self.vol_var,
                   orient=tk.HORIZONTAL, length=80).pack(side=tk.LEFT)

        # フレームステップ
        tk.Button(ctrl_f, text="◀ 1f",
                  command=lambda: self._step_frame(-1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Button(ctrl_f, text="1f ▶",
                  command=lambda: self._step_frame(1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Label(ctrl_f, text="フレーム:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.RIGHT, padx=4)

        # ファイル情報
        info_f = tk.Frame(self.root, bg="#1a1a2e")
        info_f.pack(fill=tk.X, padx=8)
        self.info_var = tk.StringVar(value="ファイルを開いてください")
        tk.Label(info_f, textvariable=self.info_var,
                 bg="#1a1a2e", fg="#888", font=("Arial", 9)).pack(anchor="w")

        self.root.protocol("WM_DELETE_WINDOW", self._on_close)

    def _open_file(self):
        path = filedialog.askopenfilename(
            filetypes=[("動画ファイル", "*.mp4 *.avi *.mov *.mkv *.wmv *.flv"),
                       ("すべて", "*.*")])
        if path:
            self._load_video(path)

    def _load_video(self, path):
        if not CV2_AVAILABLE or not PIL_AVAILABLE:
            messagebox.showerror("エラー",
                                 "opencv-python と Pillow が必要です:\n"
                                 "pip install opencv-python Pillow")
            return
        self._stop()
        if self._cap:
            self._cap.release()
        cap = cv2.VideoCapture(path)
        if not cap.isOpened():
            messagebox.showerror("エラー", "動画ファイルを開けませんでした")
            return
        self._cap = cap
        self._fps = cap.get(cv2.CAP_PROP_FPS) or 30
        self._total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        self._current_frame = 0
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = self._total_frames / self._fps
        self.seek_bar.configure(to=self._total_frames)
        self.root.title(f"動画プレイヤー — {os.path.basename(path)}")
        self.info_var.set(
            f"{os.path.basename(path)}  |  {w}×{h}  |  "
            f"{self._fps:.1f}fps  |  {self._fmt_time(duration)}")
        # 最初のフレームを表示
        self._show_frame(0)

    def _show_frame(self, frame_idx):
        if not self._cap:
            return
        frame_idx = max(0, min(frame_idx, self._total_frames - 1))
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
        ret, frame = self._cap.read()
        if not ret:
            return
        self._current_frame = frame_idx
        # BGR → RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame_rgb)
        # キャンバスサイズに合わせてリサイズ
        cw = self.canvas.winfo_width() or 640
        ch = self.canvas.winfo_height() or 480
        if cw > 1 and ch > 1:
            img.thumbnail((cw, ch), Image.LANCZOS)
        self.canvas_img = ImageTk.PhotoImage(img)
        self.canvas.delete("all")
        self.canvas.create_image(cw // 2, ch // 2, image=self.canvas_img, anchor="center")
        # UI更新
        self.position_var.set(frame_idx)
        elapsed = frame_idx / self._fps
        total = self._total_frames / self._fps
        self.time_label.config(
            text=f"{self._fmt_time(elapsed)} / {self._fmt_time(total)}")
        self.frame_label.config(
            text=f"フレーム: {frame_idx}/{self._total_frames}")

    def _toggle_play(self):
        if not self._cap:
            messagebox.showinfo("情報", "動画ファイルを開いてください")
            return
        if self._playing:
            self._playing = False
            self.play_btn.config(text="▶")
            if self._after_id:
                self.root.after_cancel(self._after_id)
        else:
            self._playing = True
            self.play_btn.config(text="⏸")
            self._play_loop()

    def _play_loop(self):
        if not self._playing or not self._cap:
            return
        speed = float(self.speed_cb.get())
        delay_ms = max(1, int(1000 / (self._fps * speed)))

        if self._current_frame >= self._total_frames - 1:
            # 末尾に達したら停止
            self._playing = False
            self.play_btn.config(text="▶")
            return

        self._show_frame(self._current_frame + 1)
        self._after_id = self.root.after(delay_ms, self._play_loop)

    def _on_seek(self, value):
        if self._cap:
            frame = int(float(value))
            self._show_frame(frame)

    def _skip(self, seconds):
        if self._cap:
            frames = int(seconds * self._fps)
            new_frame = max(0, min(self._current_frame + frames, self._total_frames - 1))
            self._show_frame(new_frame)

    def _step_frame(self, delta):
        if self._cap:
            self._show_frame(self._current_frame + delta)

    def _stop(self):
        self._playing = False
        self.play_btn.config(text="▶")
        if self._after_id:
            self.root.after_cancel(self._after_id)
            self._after_id = None
        if self._cap:
            self._show_frame(0)

    def _fmt_time(self, seconds):
        m = int(seconds // 60)
        s = int(seconds % 60)
        return f"{m:02d}:{s:02d}"

    def _on_close(self):
        self._playing = False
        if self._after_id:
            self.root.after_cancel(self._after_id)
        if self._cap:
            self._cap.release()
        self.root.destroy()


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

LabelFrameによるセクション分け

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

import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
import threading
import time

try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    CV2_AVAILABLE = False

try:
    from PIL import Image, ImageTk
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False


class App46:
    """動画プレイヤー UI"""

    def __init__(self, root):
        self.root = root
        self.root.title("動画プレイヤー UI")
        self.root.geometry("860x640")
        self.root.configure(bg="#1a1a2e")
        self._cap = None
        self._playing = False
        self._total_frames = 0
        self._current_frame = 0
        self._fps = 30
        self._volume = 0.5
        self._after_id = None
        self._build_ui()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#16213e", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="🎬 動画プレイヤー UI",
                 font=("Noto Sans JP", 12, "bold"),
                 bg="#16213e", fg="#e94560").pack(side=tk.LEFT, padx=12)
        tk.Button(header, text="📂 ファイルを開く",
                  command=self._open_file,
                  bg="#e94560", fg="white", relief=tk.FLAT,
                  font=("Arial", 10), padx=10, pady=3,
                  activebackground="#c23152", bd=0).pack(side=tk.RIGHT, padx=8)

        if not CV2_AVAILABLE:
            tk.Label(self.root,
                     text="⚠ opencv-python が未インストールです "
                          "(pip install opencv-python Pillow)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)

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

        # ビデオ表示キャンバス
        self.canvas = tk.Canvas(self.root, bg="black", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self.canvas_img = None

        # シークバー
        seek_f = tk.Frame(self.root, bg="#1a1a2e")
        seek_f.pack(fill=tk.X, padx=8, pady=2)
        self.position_var = tk.DoubleVar(value=0)
        self.seek_bar = ttk.Scale(seek_f, from_=0, to=100,
                                   variable=self.position_var,
                                   orient=tk.HORIZONTAL,
                                   command=self._on_seek)
        self.seek_bar.pack(fill=tk.X)

        # 時間表示
        time_f = tk.Frame(self.root, bg="#1a1a2e")
        time_f.pack(fill=tk.X, padx=8)
        self.time_label = tk.Label(time_f, text="00:00 / 00:00",
                                    bg="#1a1a2e", fg="#ccc",
                                    font=("Courier New", 10))
        self.time_label.pack(side=tk.LEFT)
        self.frame_label = tk.Label(time_f, text="フレーム: 0/0",
                                     bg="#1a1a2e", fg="#888",
                                     font=("Arial", 9))
        self.frame_label.pack(side=tk.RIGHT)

        # コントロールパネル
        ctrl_f = tk.Frame(self.root, bg="#16213e", pady=8)
        ctrl_f.pack(fill=tk.X)

        btn_s = {"bg": "#0f3460", "fg": "white", "relief": tk.FLAT,
                 "font": ("Arial", 12), "padx": 12, "pady=4",
                 "activebackground": "#1a4a80", "bd": 0}

        self.rewind_btn = tk.Button(ctrl_f, text="⏪",
                                     command=lambda: self._skip(-10),
                                     bg="#0f3460", fg="white", relief=tk.FLAT,
                                     font=("Arial", 14), padx=10, pady=4,
                                     activebackground="#1a4a80", bd=0)
        self.rewind_btn.pack(side=tk.LEFT, padx=4)

        self.play_btn = tk.Button(ctrl_f, text="▶",
                                   command=self._toggle_play,
                                   bg="#e94560", fg="white", relief=tk.FLAT,
                                   font=("Arial", 16), padx=14, pady=4,
                                   activebackground="#c23152", bd=0)
        self.play_btn.pack(side=tk.LEFT, padx=4)

        self.forward_btn = tk.Button(ctrl_f, text="⏩",
                                      command=lambda: self._skip(10),
                                      bg="#0f3460", fg="white", relief=tk.FLAT,
                                      font=("Arial", 14), padx=10, pady=4,
                                      activebackground="#1a4a80", bd=0)
        self.forward_btn.pack(side=tk.LEFT, padx=4)

        tk.Button(ctrl_f, text="⏹",
                  command=self._stop,
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 14), padx=10, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.LEFT, padx=4)

        # 再生速度
        tk.Label(ctrl_f, text="速度:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.speed_var = tk.DoubleVar(value=1.0)
        self.speed_cb = ttk.Combobox(ctrl_f, textvariable=self.speed_var,
                                      values=[0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
                                      state="readonly", width=5)
        self.speed_cb.pack(side=tk.LEFT)
        self.speed_cb.set("1.0")

        # 音量(ダミー UIとして表示)
        tk.Label(ctrl_f, text="音量:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.vol_var = tk.IntVar(value=50)
        ttk.Scale(ctrl_f, from_=0, to=100, variable=self.vol_var,
                   orient=tk.HORIZONTAL, length=80).pack(side=tk.LEFT)

        # フレームステップ
        tk.Button(ctrl_f, text="◀ 1f",
                  command=lambda: self._step_frame(-1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Button(ctrl_f, text="1f ▶",
                  command=lambda: self._step_frame(1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Label(ctrl_f, text="フレーム:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.RIGHT, padx=4)

        # ファイル情報
        info_f = tk.Frame(self.root, bg="#1a1a2e")
        info_f.pack(fill=tk.X, padx=8)
        self.info_var = tk.StringVar(value="ファイルを開いてください")
        tk.Label(info_f, textvariable=self.info_var,
                 bg="#1a1a2e", fg="#888", font=("Arial", 9)).pack(anchor="w")

        self.root.protocol("WM_DELETE_WINDOW", self._on_close)

    def _open_file(self):
        path = filedialog.askopenfilename(
            filetypes=[("動画ファイル", "*.mp4 *.avi *.mov *.mkv *.wmv *.flv"),
                       ("すべて", "*.*")])
        if path:
            self._load_video(path)

    def _load_video(self, path):
        if not CV2_AVAILABLE or not PIL_AVAILABLE:
            messagebox.showerror("エラー",
                                 "opencv-python と Pillow が必要です:\n"
                                 "pip install opencv-python Pillow")
            return
        self._stop()
        if self._cap:
            self._cap.release()
        cap = cv2.VideoCapture(path)
        if not cap.isOpened():
            messagebox.showerror("エラー", "動画ファイルを開けませんでした")
            return
        self._cap = cap
        self._fps = cap.get(cv2.CAP_PROP_FPS) or 30
        self._total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        self._current_frame = 0
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = self._total_frames / self._fps
        self.seek_bar.configure(to=self._total_frames)
        self.root.title(f"動画プレイヤー — {os.path.basename(path)}")
        self.info_var.set(
            f"{os.path.basename(path)}  |  {w}×{h}  |  "
            f"{self._fps:.1f}fps  |  {self._fmt_time(duration)}")
        # 最初のフレームを表示
        self._show_frame(0)

    def _show_frame(self, frame_idx):
        if not self._cap:
            return
        frame_idx = max(0, min(frame_idx, self._total_frames - 1))
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
        ret, frame = self._cap.read()
        if not ret:
            return
        self._current_frame = frame_idx
        # BGR → RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame_rgb)
        # キャンバスサイズに合わせてリサイズ
        cw = self.canvas.winfo_width() or 640
        ch = self.canvas.winfo_height() or 480
        if cw > 1 and ch > 1:
            img.thumbnail((cw, ch), Image.LANCZOS)
        self.canvas_img = ImageTk.PhotoImage(img)
        self.canvas.delete("all")
        self.canvas.create_image(cw // 2, ch // 2, image=self.canvas_img, anchor="center")
        # UI更新
        self.position_var.set(frame_idx)
        elapsed = frame_idx / self._fps
        total = self._total_frames / self._fps
        self.time_label.config(
            text=f"{self._fmt_time(elapsed)} / {self._fmt_time(total)}")
        self.frame_label.config(
            text=f"フレーム: {frame_idx}/{self._total_frames}")

    def _toggle_play(self):
        if not self._cap:
            messagebox.showinfo("情報", "動画ファイルを開いてください")
            return
        if self._playing:
            self._playing = False
            self.play_btn.config(text="▶")
            if self._after_id:
                self.root.after_cancel(self._after_id)
        else:
            self._playing = True
            self.play_btn.config(text="⏸")
            self._play_loop()

    def _play_loop(self):
        if not self._playing or not self._cap:
            return
        speed = float(self.speed_cb.get())
        delay_ms = max(1, int(1000 / (self._fps * speed)))

        if self._current_frame >= self._total_frames - 1:
            # 末尾に達したら停止
            self._playing = False
            self.play_btn.config(text="▶")
            return

        self._show_frame(self._current_frame + 1)
        self._after_id = self.root.after(delay_ms, self._play_loop)

    def _on_seek(self, value):
        if self._cap:
            frame = int(float(value))
            self._show_frame(frame)

    def _skip(self, seconds):
        if self._cap:
            frames = int(seconds * self._fps)
            new_frame = max(0, min(self._current_frame + frames, self._total_frames - 1))
            self._show_frame(new_frame)

    def _step_frame(self, delta):
        if self._cap:
            self._show_frame(self._current_frame + delta)

    def _stop(self):
        self._playing = False
        self.play_btn.config(text="▶")
        if self._after_id:
            self.root.after_cancel(self._after_id)
            self._after_id = None
        if self._cap:
            self._show_frame(0)

    def _fmt_time(self, seconds):
        m = int(seconds // 60)
        s = int(seconds % 60)
        return f"{m:02d}:{s:02d}"

    def _on_close(self):
        self._playing = False
        if self._after_id:
            self.root.after_cancel(self._after_id)
        if self._cap:
            self._cap.release()
        self.root.destroy()


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

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

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

import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
import threading
import time

try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    CV2_AVAILABLE = False

try:
    from PIL import Image, ImageTk
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False


class App46:
    """動画プレイヤー UI"""

    def __init__(self, root):
        self.root = root
        self.root.title("動画プレイヤー UI")
        self.root.geometry("860x640")
        self.root.configure(bg="#1a1a2e")
        self._cap = None
        self._playing = False
        self._total_frames = 0
        self._current_frame = 0
        self._fps = 30
        self._volume = 0.5
        self._after_id = None
        self._build_ui()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#16213e", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="🎬 動画プレイヤー UI",
                 font=("Noto Sans JP", 12, "bold"),
                 bg="#16213e", fg="#e94560").pack(side=tk.LEFT, padx=12)
        tk.Button(header, text="📂 ファイルを開く",
                  command=self._open_file,
                  bg="#e94560", fg="white", relief=tk.FLAT,
                  font=("Arial", 10), padx=10, pady=3,
                  activebackground="#c23152", bd=0).pack(side=tk.RIGHT, padx=8)

        if not CV2_AVAILABLE:
            tk.Label(self.root,
                     text="⚠ opencv-python が未インストールです "
                          "(pip install opencv-python Pillow)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)

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

        # ビデオ表示キャンバス
        self.canvas = tk.Canvas(self.root, bg="black", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self.canvas_img = None

        # シークバー
        seek_f = tk.Frame(self.root, bg="#1a1a2e")
        seek_f.pack(fill=tk.X, padx=8, pady=2)
        self.position_var = tk.DoubleVar(value=0)
        self.seek_bar = ttk.Scale(seek_f, from_=0, to=100,
                                   variable=self.position_var,
                                   orient=tk.HORIZONTAL,
                                   command=self._on_seek)
        self.seek_bar.pack(fill=tk.X)

        # 時間表示
        time_f = tk.Frame(self.root, bg="#1a1a2e")
        time_f.pack(fill=tk.X, padx=8)
        self.time_label = tk.Label(time_f, text="00:00 / 00:00",
                                    bg="#1a1a2e", fg="#ccc",
                                    font=("Courier New", 10))
        self.time_label.pack(side=tk.LEFT)
        self.frame_label = tk.Label(time_f, text="フレーム: 0/0",
                                     bg="#1a1a2e", fg="#888",
                                     font=("Arial", 9))
        self.frame_label.pack(side=tk.RIGHT)

        # コントロールパネル
        ctrl_f = tk.Frame(self.root, bg="#16213e", pady=8)
        ctrl_f.pack(fill=tk.X)

        btn_s = {"bg": "#0f3460", "fg": "white", "relief": tk.FLAT,
                 "font": ("Arial", 12), "padx": 12, "pady=4",
                 "activebackground": "#1a4a80", "bd": 0}

        self.rewind_btn = tk.Button(ctrl_f, text="⏪",
                                     command=lambda: self._skip(-10),
                                     bg="#0f3460", fg="white", relief=tk.FLAT,
                                     font=("Arial", 14), padx=10, pady=4,
                                     activebackground="#1a4a80", bd=0)
        self.rewind_btn.pack(side=tk.LEFT, padx=4)

        self.play_btn = tk.Button(ctrl_f, text="▶",
                                   command=self._toggle_play,
                                   bg="#e94560", fg="white", relief=tk.FLAT,
                                   font=("Arial", 16), padx=14, pady=4,
                                   activebackground="#c23152", bd=0)
        self.play_btn.pack(side=tk.LEFT, padx=4)

        self.forward_btn = tk.Button(ctrl_f, text="⏩",
                                      command=lambda: self._skip(10),
                                      bg="#0f3460", fg="white", relief=tk.FLAT,
                                      font=("Arial", 14), padx=10, pady=4,
                                      activebackground="#1a4a80", bd=0)
        self.forward_btn.pack(side=tk.LEFT, padx=4)

        tk.Button(ctrl_f, text="⏹",
                  command=self._stop,
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 14), padx=10, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.LEFT, padx=4)

        # 再生速度
        tk.Label(ctrl_f, text="速度:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.speed_var = tk.DoubleVar(value=1.0)
        self.speed_cb = ttk.Combobox(ctrl_f, textvariable=self.speed_var,
                                      values=[0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
                                      state="readonly", width=5)
        self.speed_cb.pack(side=tk.LEFT)
        self.speed_cb.set("1.0")

        # 音量(ダミー UIとして表示)
        tk.Label(ctrl_f, text="音量:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.vol_var = tk.IntVar(value=50)
        ttk.Scale(ctrl_f, from_=0, to=100, variable=self.vol_var,
                   orient=tk.HORIZONTAL, length=80).pack(side=tk.LEFT)

        # フレームステップ
        tk.Button(ctrl_f, text="◀ 1f",
                  command=lambda: self._step_frame(-1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Button(ctrl_f, text="1f ▶",
                  command=lambda: self._step_frame(1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Label(ctrl_f, text="フレーム:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.RIGHT, padx=4)

        # ファイル情報
        info_f = tk.Frame(self.root, bg="#1a1a2e")
        info_f.pack(fill=tk.X, padx=8)
        self.info_var = tk.StringVar(value="ファイルを開いてください")
        tk.Label(info_f, textvariable=self.info_var,
                 bg="#1a1a2e", fg="#888", font=("Arial", 9)).pack(anchor="w")

        self.root.protocol("WM_DELETE_WINDOW", self._on_close)

    def _open_file(self):
        path = filedialog.askopenfilename(
            filetypes=[("動画ファイル", "*.mp4 *.avi *.mov *.mkv *.wmv *.flv"),
                       ("すべて", "*.*")])
        if path:
            self._load_video(path)

    def _load_video(self, path):
        if not CV2_AVAILABLE or not PIL_AVAILABLE:
            messagebox.showerror("エラー",
                                 "opencv-python と Pillow が必要です:\n"
                                 "pip install opencv-python Pillow")
            return
        self._stop()
        if self._cap:
            self._cap.release()
        cap = cv2.VideoCapture(path)
        if not cap.isOpened():
            messagebox.showerror("エラー", "動画ファイルを開けませんでした")
            return
        self._cap = cap
        self._fps = cap.get(cv2.CAP_PROP_FPS) or 30
        self._total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        self._current_frame = 0
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = self._total_frames / self._fps
        self.seek_bar.configure(to=self._total_frames)
        self.root.title(f"動画プレイヤー — {os.path.basename(path)}")
        self.info_var.set(
            f"{os.path.basename(path)}  |  {w}×{h}  |  "
            f"{self._fps:.1f}fps  |  {self._fmt_time(duration)}")
        # 最初のフレームを表示
        self._show_frame(0)

    def _show_frame(self, frame_idx):
        if not self._cap:
            return
        frame_idx = max(0, min(frame_idx, self._total_frames - 1))
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
        ret, frame = self._cap.read()
        if not ret:
            return
        self._current_frame = frame_idx
        # BGR → RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame_rgb)
        # キャンバスサイズに合わせてリサイズ
        cw = self.canvas.winfo_width() or 640
        ch = self.canvas.winfo_height() or 480
        if cw > 1 and ch > 1:
            img.thumbnail((cw, ch), Image.LANCZOS)
        self.canvas_img = ImageTk.PhotoImage(img)
        self.canvas.delete("all")
        self.canvas.create_image(cw // 2, ch // 2, image=self.canvas_img, anchor="center")
        # UI更新
        self.position_var.set(frame_idx)
        elapsed = frame_idx / self._fps
        total = self._total_frames / self._fps
        self.time_label.config(
            text=f"{self._fmt_time(elapsed)} / {self._fmt_time(total)}")
        self.frame_label.config(
            text=f"フレーム: {frame_idx}/{self._total_frames}")

    def _toggle_play(self):
        if not self._cap:
            messagebox.showinfo("情報", "動画ファイルを開いてください")
            return
        if self._playing:
            self._playing = False
            self.play_btn.config(text="▶")
            if self._after_id:
                self.root.after_cancel(self._after_id)
        else:
            self._playing = True
            self.play_btn.config(text="⏸")
            self._play_loop()

    def _play_loop(self):
        if not self._playing or not self._cap:
            return
        speed = float(self.speed_cb.get())
        delay_ms = max(1, int(1000 / (self._fps * speed)))

        if self._current_frame >= self._total_frames - 1:
            # 末尾に達したら停止
            self._playing = False
            self.play_btn.config(text="▶")
            return

        self._show_frame(self._current_frame + 1)
        self._after_id = self.root.after(delay_ms, self._play_loop)

    def _on_seek(self, value):
        if self._cap:
            frame = int(float(value))
            self._show_frame(frame)

    def _skip(self, seconds):
        if self._cap:
            frames = int(seconds * self._fps)
            new_frame = max(0, min(self._current_frame + frames, self._total_frames - 1))
            self._show_frame(new_frame)

    def _step_frame(self, delta):
        if self._cap:
            self._show_frame(self._current_frame + delta)

    def _stop(self):
        self._playing = False
        self.play_btn.config(text="▶")
        if self._after_id:
            self.root.after_cancel(self._after_id)
            self._after_id = None
        if self._cap:
            self._show_frame(0)

    def _fmt_time(self, seconds):
        m = int(seconds // 60)
        s = int(seconds % 60)
        return f"{m:02d}:{s:02d}"

    def _on_close(self):
        self._playing = False
        if self._after_id:
            self.root.after_cancel(self._after_id)
        if self._cap:
            self._cap.release()
        self.root.destroy()


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

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

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

import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
import threading
import time

try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    CV2_AVAILABLE = False

try:
    from PIL import Image, ImageTk
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False


class App46:
    """動画プレイヤー UI"""

    def __init__(self, root):
        self.root = root
        self.root.title("動画プレイヤー UI")
        self.root.geometry("860x640")
        self.root.configure(bg="#1a1a2e")
        self._cap = None
        self._playing = False
        self._total_frames = 0
        self._current_frame = 0
        self._fps = 30
        self._volume = 0.5
        self._after_id = None
        self._build_ui()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#16213e", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="🎬 動画プレイヤー UI",
                 font=("Noto Sans JP", 12, "bold"),
                 bg="#16213e", fg="#e94560").pack(side=tk.LEFT, padx=12)
        tk.Button(header, text="📂 ファイルを開く",
                  command=self._open_file,
                  bg="#e94560", fg="white", relief=tk.FLAT,
                  font=("Arial", 10), padx=10, pady=3,
                  activebackground="#c23152", bd=0).pack(side=tk.RIGHT, padx=8)

        if not CV2_AVAILABLE:
            tk.Label(self.root,
                     text="⚠ opencv-python が未インストールです "
                          "(pip install opencv-python Pillow)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)

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

        # ビデオ表示キャンバス
        self.canvas = tk.Canvas(self.root, bg="black", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self.canvas_img = None

        # シークバー
        seek_f = tk.Frame(self.root, bg="#1a1a2e")
        seek_f.pack(fill=tk.X, padx=8, pady=2)
        self.position_var = tk.DoubleVar(value=0)
        self.seek_bar = ttk.Scale(seek_f, from_=0, to=100,
                                   variable=self.position_var,
                                   orient=tk.HORIZONTAL,
                                   command=self._on_seek)
        self.seek_bar.pack(fill=tk.X)

        # 時間表示
        time_f = tk.Frame(self.root, bg="#1a1a2e")
        time_f.pack(fill=tk.X, padx=8)
        self.time_label = tk.Label(time_f, text="00:00 / 00:00",
                                    bg="#1a1a2e", fg="#ccc",
                                    font=("Courier New", 10))
        self.time_label.pack(side=tk.LEFT)
        self.frame_label = tk.Label(time_f, text="フレーム: 0/0",
                                     bg="#1a1a2e", fg="#888",
                                     font=("Arial", 9))
        self.frame_label.pack(side=tk.RIGHT)

        # コントロールパネル
        ctrl_f = tk.Frame(self.root, bg="#16213e", pady=8)
        ctrl_f.pack(fill=tk.X)

        btn_s = {"bg": "#0f3460", "fg": "white", "relief": tk.FLAT,
                 "font": ("Arial", 12), "padx": 12, "pady=4",
                 "activebackground": "#1a4a80", "bd": 0}

        self.rewind_btn = tk.Button(ctrl_f, text="⏪",
                                     command=lambda: self._skip(-10),
                                     bg="#0f3460", fg="white", relief=tk.FLAT,
                                     font=("Arial", 14), padx=10, pady=4,
                                     activebackground="#1a4a80", bd=0)
        self.rewind_btn.pack(side=tk.LEFT, padx=4)

        self.play_btn = tk.Button(ctrl_f, text="▶",
                                   command=self._toggle_play,
                                   bg="#e94560", fg="white", relief=tk.FLAT,
                                   font=("Arial", 16), padx=14, pady=4,
                                   activebackground="#c23152", bd=0)
        self.play_btn.pack(side=tk.LEFT, padx=4)

        self.forward_btn = tk.Button(ctrl_f, text="⏩",
                                      command=lambda: self._skip(10),
                                      bg="#0f3460", fg="white", relief=tk.FLAT,
                                      font=("Arial", 14), padx=10, pady=4,
                                      activebackground="#1a4a80", bd=0)
        self.forward_btn.pack(side=tk.LEFT, padx=4)

        tk.Button(ctrl_f, text="⏹",
                  command=self._stop,
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 14), padx=10, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.LEFT, padx=4)

        # 再生速度
        tk.Label(ctrl_f, text="速度:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.speed_var = tk.DoubleVar(value=1.0)
        self.speed_cb = ttk.Combobox(ctrl_f, textvariable=self.speed_var,
                                      values=[0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
                                      state="readonly", width=5)
        self.speed_cb.pack(side=tk.LEFT)
        self.speed_cb.set("1.0")

        # 音量(ダミー UIとして表示)
        tk.Label(ctrl_f, text="音量:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.vol_var = tk.IntVar(value=50)
        ttk.Scale(ctrl_f, from_=0, to=100, variable=self.vol_var,
                   orient=tk.HORIZONTAL, length=80).pack(side=tk.LEFT)

        # フレームステップ
        tk.Button(ctrl_f, text="◀ 1f",
                  command=lambda: self._step_frame(-1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Button(ctrl_f, text="1f ▶",
                  command=lambda: self._step_frame(1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Label(ctrl_f, text="フレーム:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.RIGHT, padx=4)

        # ファイル情報
        info_f = tk.Frame(self.root, bg="#1a1a2e")
        info_f.pack(fill=tk.X, padx=8)
        self.info_var = tk.StringVar(value="ファイルを開いてください")
        tk.Label(info_f, textvariable=self.info_var,
                 bg="#1a1a2e", fg="#888", font=("Arial", 9)).pack(anchor="w")

        self.root.protocol("WM_DELETE_WINDOW", self._on_close)

    def _open_file(self):
        path = filedialog.askopenfilename(
            filetypes=[("動画ファイル", "*.mp4 *.avi *.mov *.mkv *.wmv *.flv"),
                       ("すべて", "*.*")])
        if path:
            self._load_video(path)

    def _load_video(self, path):
        if not CV2_AVAILABLE or not PIL_AVAILABLE:
            messagebox.showerror("エラー",
                                 "opencv-python と Pillow が必要です:\n"
                                 "pip install opencv-python Pillow")
            return
        self._stop()
        if self._cap:
            self._cap.release()
        cap = cv2.VideoCapture(path)
        if not cap.isOpened():
            messagebox.showerror("エラー", "動画ファイルを開けませんでした")
            return
        self._cap = cap
        self._fps = cap.get(cv2.CAP_PROP_FPS) or 30
        self._total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        self._current_frame = 0
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = self._total_frames / self._fps
        self.seek_bar.configure(to=self._total_frames)
        self.root.title(f"動画プレイヤー — {os.path.basename(path)}")
        self.info_var.set(
            f"{os.path.basename(path)}  |  {w}×{h}  |  "
            f"{self._fps:.1f}fps  |  {self._fmt_time(duration)}")
        # 最初のフレームを表示
        self._show_frame(0)

    def _show_frame(self, frame_idx):
        if not self._cap:
            return
        frame_idx = max(0, min(frame_idx, self._total_frames - 1))
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
        ret, frame = self._cap.read()
        if not ret:
            return
        self._current_frame = frame_idx
        # BGR → RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame_rgb)
        # キャンバスサイズに合わせてリサイズ
        cw = self.canvas.winfo_width() or 640
        ch = self.canvas.winfo_height() or 480
        if cw > 1 and ch > 1:
            img.thumbnail((cw, ch), Image.LANCZOS)
        self.canvas_img = ImageTk.PhotoImage(img)
        self.canvas.delete("all")
        self.canvas.create_image(cw // 2, ch // 2, image=self.canvas_img, anchor="center")
        # UI更新
        self.position_var.set(frame_idx)
        elapsed = frame_idx / self._fps
        total = self._total_frames / self._fps
        self.time_label.config(
            text=f"{self._fmt_time(elapsed)} / {self._fmt_time(total)}")
        self.frame_label.config(
            text=f"フレーム: {frame_idx}/{self._total_frames}")

    def _toggle_play(self):
        if not self._cap:
            messagebox.showinfo("情報", "動画ファイルを開いてください")
            return
        if self._playing:
            self._playing = False
            self.play_btn.config(text="▶")
            if self._after_id:
                self.root.after_cancel(self._after_id)
        else:
            self._playing = True
            self.play_btn.config(text="⏸")
            self._play_loop()

    def _play_loop(self):
        if not self._playing or not self._cap:
            return
        speed = float(self.speed_cb.get())
        delay_ms = max(1, int(1000 / (self._fps * speed)))

        if self._current_frame >= self._total_frames - 1:
            # 末尾に達したら停止
            self._playing = False
            self.play_btn.config(text="▶")
            return

        self._show_frame(self._current_frame + 1)
        self._after_id = self.root.after(delay_ms, self._play_loop)

    def _on_seek(self, value):
        if self._cap:
            frame = int(float(value))
            self._show_frame(frame)

    def _skip(self, seconds):
        if self._cap:
            frames = int(seconds * self._fps)
            new_frame = max(0, min(self._current_frame + frames, self._total_frames - 1))
            self._show_frame(new_frame)

    def _step_frame(self, delta):
        if self._cap:
            self._show_frame(self._current_frame + delta)

    def _stop(self):
        self._playing = False
        self.play_btn.config(text="▶")
        if self._after_id:
            self.root.after_cancel(self._after_id)
            self._after_id = None
        if self._cap:
            self._show_frame(0)

    def _fmt_time(self, seconds):
        m = int(seconds // 60)
        s = int(seconds % 60)
        return f"{m:02d}:{s:02d}"

    def _on_close(self):
        self._playing = False
        if self._after_id:
            self.root.after_cancel(self._after_id)
        if self._cap:
            self._cap.release()
        self.root.destroy()


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

例外処理とmessagebox

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

import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
import threading
import time

try:
    import cv2
    CV2_AVAILABLE = True
except ImportError:
    CV2_AVAILABLE = False

try:
    from PIL import Image, ImageTk
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False


class App46:
    """動画プレイヤー UI"""

    def __init__(self, root):
        self.root = root
        self.root.title("動画プレイヤー UI")
        self.root.geometry("860x640")
        self.root.configure(bg="#1a1a2e")
        self._cap = None
        self._playing = False
        self._total_frames = 0
        self._current_frame = 0
        self._fps = 30
        self._volume = 0.5
        self._after_id = None
        self._build_ui()

    def _build_ui(self):
        # ヘッダー
        header = tk.Frame(self.root, bg="#16213e", pady=6)
        header.pack(fill=tk.X)
        tk.Label(header, text="🎬 動画プレイヤー UI",
                 font=("Noto Sans JP", 12, "bold"),
                 bg="#16213e", fg="#e94560").pack(side=tk.LEFT, padx=12)
        tk.Button(header, text="📂 ファイルを開く",
                  command=self._open_file,
                  bg="#e94560", fg="white", relief=tk.FLAT,
                  font=("Arial", 10), padx=10, pady=3,
                  activebackground="#c23152", bd=0).pack(side=tk.RIGHT, padx=8)

        if not CV2_AVAILABLE:
            tk.Label(self.root,
                     text="⚠ opencv-python が未インストールです "
                          "(pip install opencv-python Pillow)。",
                     bg="#fff3cd", fg="#856404", font=("Arial", 9),
                     anchor="w", padx=8).pack(fill=tk.X)

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

        # ビデオ表示キャンバス
        self.canvas = tk.Canvas(self.root, bg="black", highlightthickness=0)
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=8, pady=4)
        self.canvas_img = None

        # シークバー
        seek_f = tk.Frame(self.root, bg="#1a1a2e")
        seek_f.pack(fill=tk.X, padx=8, pady=2)
        self.position_var = tk.DoubleVar(value=0)
        self.seek_bar = ttk.Scale(seek_f, from_=0, to=100,
                                   variable=self.position_var,
                                   orient=tk.HORIZONTAL,
                                   command=self._on_seek)
        self.seek_bar.pack(fill=tk.X)

        # 時間表示
        time_f = tk.Frame(self.root, bg="#1a1a2e")
        time_f.pack(fill=tk.X, padx=8)
        self.time_label = tk.Label(time_f, text="00:00 / 00:00",
                                    bg="#1a1a2e", fg="#ccc",
                                    font=("Courier New", 10))
        self.time_label.pack(side=tk.LEFT)
        self.frame_label = tk.Label(time_f, text="フレーム: 0/0",
                                     bg="#1a1a2e", fg="#888",
                                     font=("Arial", 9))
        self.frame_label.pack(side=tk.RIGHT)

        # コントロールパネル
        ctrl_f = tk.Frame(self.root, bg="#16213e", pady=8)
        ctrl_f.pack(fill=tk.X)

        btn_s = {"bg": "#0f3460", "fg": "white", "relief": tk.FLAT,
                 "font": ("Arial", 12), "padx": 12, "pady=4",
                 "activebackground": "#1a4a80", "bd": 0}

        self.rewind_btn = tk.Button(ctrl_f, text="⏪",
                                     command=lambda: self._skip(-10),
                                     bg="#0f3460", fg="white", relief=tk.FLAT,
                                     font=("Arial", 14), padx=10, pady=4,
                                     activebackground="#1a4a80", bd=0)
        self.rewind_btn.pack(side=tk.LEFT, padx=4)

        self.play_btn = tk.Button(ctrl_f, text="▶",
                                   command=self._toggle_play,
                                   bg="#e94560", fg="white", relief=tk.FLAT,
                                   font=("Arial", 16), padx=14, pady=4,
                                   activebackground="#c23152", bd=0)
        self.play_btn.pack(side=tk.LEFT, padx=4)

        self.forward_btn = tk.Button(ctrl_f, text="⏩",
                                      command=lambda: self._skip(10),
                                      bg="#0f3460", fg="white", relief=tk.FLAT,
                                      font=("Arial", 14), padx=10, pady=4,
                                      activebackground="#1a4a80", bd=0)
        self.forward_btn.pack(side=tk.LEFT, padx=4)

        tk.Button(ctrl_f, text="⏹",
                  command=self._stop,
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 14), padx=10, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.LEFT, padx=4)

        # 再生速度
        tk.Label(ctrl_f, text="速度:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.speed_var = tk.DoubleVar(value=1.0)
        self.speed_cb = ttk.Combobox(ctrl_f, textvariable=self.speed_var,
                                      values=[0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
                                      state="readonly", width=5)
        self.speed_cb.pack(side=tk.LEFT)
        self.speed_cb.set("1.0")

        # 音量(ダミー UIとして表示)
        tk.Label(ctrl_f, text="音量:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.LEFT, padx=(16, 4))
        self.vol_var = tk.IntVar(value=50)
        ttk.Scale(ctrl_f, from_=0, to=100, variable=self.vol_var,
                   orient=tk.HORIZONTAL, length=80).pack(side=tk.LEFT)

        # フレームステップ
        tk.Button(ctrl_f, text="◀ 1f",
                  command=lambda: self._step_frame(-1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Button(ctrl_f, text="1f ▶",
                  command=lambda: self._step_frame(1),
                  bg="#0f3460", fg="white", relief=tk.FLAT,
                  font=("Arial", 9), padx=6, pady=4,
                  activebackground="#1a4a80", bd=0).pack(side=tk.RIGHT, padx=2)
        tk.Label(ctrl_f, text="フレーム:", bg="#16213e", fg="#ccc",
                 font=("Arial", 9)).pack(side=tk.RIGHT, padx=4)

        # ファイル情報
        info_f = tk.Frame(self.root, bg="#1a1a2e")
        info_f.pack(fill=tk.X, padx=8)
        self.info_var = tk.StringVar(value="ファイルを開いてください")
        tk.Label(info_f, textvariable=self.info_var,
                 bg="#1a1a2e", fg="#888", font=("Arial", 9)).pack(anchor="w")

        self.root.protocol("WM_DELETE_WINDOW", self._on_close)

    def _open_file(self):
        path = filedialog.askopenfilename(
            filetypes=[("動画ファイル", "*.mp4 *.avi *.mov *.mkv *.wmv *.flv"),
                       ("すべて", "*.*")])
        if path:
            self._load_video(path)

    def _load_video(self, path):
        if not CV2_AVAILABLE or not PIL_AVAILABLE:
            messagebox.showerror("エラー",
                                 "opencv-python と Pillow が必要です:\n"
                                 "pip install opencv-python Pillow")
            return
        self._stop()
        if self._cap:
            self._cap.release()
        cap = cv2.VideoCapture(path)
        if not cap.isOpened():
            messagebox.showerror("エラー", "動画ファイルを開けませんでした")
            return
        self._cap = cap
        self._fps = cap.get(cv2.CAP_PROP_FPS) or 30
        self._total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        self._current_frame = 0
        w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        duration = self._total_frames / self._fps
        self.seek_bar.configure(to=self._total_frames)
        self.root.title(f"動画プレイヤー — {os.path.basename(path)}")
        self.info_var.set(
            f"{os.path.basename(path)}  |  {w}×{h}  |  "
            f"{self._fps:.1f}fps  |  {self._fmt_time(duration)}")
        # 最初のフレームを表示
        self._show_frame(0)

    def _show_frame(self, frame_idx):
        if not self._cap:
            return
        frame_idx = max(0, min(frame_idx, self._total_frames - 1))
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
        ret, frame = self._cap.read()
        if not ret:
            return
        self._current_frame = frame_idx
        # BGR → RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame_rgb)
        # キャンバスサイズに合わせてリサイズ
        cw = self.canvas.winfo_width() or 640
        ch = self.canvas.winfo_height() or 480
        if cw > 1 and ch > 1:
            img.thumbnail((cw, ch), Image.LANCZOS)
        self.canvas_img = ImageTk.PhotoImage(img)
        self.canvas.delete("all")
        self.canvas.create_image(cw // 2, ch // 2, image=self.canvas_img, anchor="center")
        # UI更新
        self.position_var.set(frame_idx)
        elapsed = frame_idx / self._fps
        total = self._total_frames / self._fps
        self.time_label.config(
            text=f"{self._fmt_time(elapsed)} / {self._fmt_time(total)}")
        self.frame_label.config(
            text=f"フレーム: {frame_idx}/{self._total_frames}")

    def _toggle_play(self):
        if not self._cap:
            messagebox.showinfo("情報", "動画ファイルを開いてください")
            return
        if self._playing:
            self._playing = False
            self.play_btn.config(text="▶")
            if self._after_id:
                self.root.after_cancel(self._after_id)
        else:
            self._playing = True
            self.play_btn.config(text="⏸")
            self._play_loop()

    def _play_loop(self):
        if not self._playing or not self._cap:
            return
        speed = float(self.speed_cb.get())
        delay_ms = max(1, int(1000 / (self._fps * speed)))

        if self._current_frame >= self._total_frames - 1:
            # 末尾に達したら停止
            self._playing = False
            self.play_btn.config(text="▶")
            return

        self._show_frame(self._current_frame + 1)
        self._after_id = self.root.after(delay_ms, self._play_loop)

    def _on_seek(self, value):
        if self._cap:
            frame = int(float(value))
            self._show_frame(frame)

    def _skip(self, seconds):
        if self._cap:
            frames = int(seconds * self._fps)
            new_frame = max(0, min(self._current_frame + frames, self._total_frames - 1))
            self._show_frame(new_frame)

    def _step_frame(self, delta):
        if self._cap:
            self._show_frame(self._current_frame + delta)

    def _stop(self):
        self._playing = False
        self.play_btn.config(text="▶")
        if self._after_id:
            self.root.after_cancel(self._after_id)
            self._after_id = None
        if self._cap:
            self._show_frame(0)

    def _fmt_time(self, seconds):
        m = int(seconds // 60)
        s = int(seconds % 60)
        return f"{m:02d}:{s:02d}"

    def _on_close(self):
        self._playing = False
        if self._after_id:
            self.root.after_cancel(self._after_id)
        if self._cap:
            self._cap.release()
        self.root.destroy()


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

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

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

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

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

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

    App46クラスを定義し、__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:機能拡張

    動画プレイヤー UIに新しい機能を1つ追加してみましょう。どんな機能があると便利か考えてから実装してください。

  2. 課題2:UIの改善

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

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

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

🚀
次に挑戦するアプリ

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