このコードは、Python の GUI ライブラリ「Tkinter」を使って作成された 簡単な TODO リストアプリです。各部分を丁寧に解説します。
✅ 概要
このアプリは次の機能を持っています:
- タスクの表示
- タスクの追加
- タスクの削除
- GUI による操作
📦 インポート部分
import tkinter as tk
from tkinter import messagebox
tkinter
:Python 標準の GUI ライブラリ。messagebox
:警告や情報などのポップアップメッセージを表示するためのサブモジュール。
🏗️ クラス定義:TodoApp
class TodoApp:
GUI アプリケーションの本体を TodoApp
クラスとして定義しています。
🔧 __init__ メソッド(初期設定)
def __init__(self, root):
- GUI ウィンドウを初期化します。
root
はtk.Tk()
によって作成されるウィンドウ本体。
1. ウィンドウ設定
self.root.title("TODOリストアプリ")
self.root.geometry("300x400")
- タイトルとサイズの設定。
2. タイトルラベル
self.title_label = tk.Label(root, text="TODOリスト", font=("Arial", 16))
self.title_label.pack(pady=10)
- 「TODOリスト」と表示するラベル。
3. タスクリスト表示部(Listbox)
self.task_listbox = tk.Listbox(root, width=40, height=10)
self.task_listbox.pack(pady=10)
- タスクの一覧を表示する領域。
4. タスク入力欄(Entry)
self.task_entry = tk.Entry(root, width=30)
self.task_entry.pack(pady=5)
- 新しいタスクを入力するテキストボックス。
5. ボタン(追加・削除)
self.add_button = tk.Button(root, text="追加", width=10, command=self.add_task)
- 「追加」ボタン。押すと
add_task
メソッドが呼ばれます。
self.delete_button = tk.Button(root, text="削除", width=10, command=self.delete_task)
- 「削除」ボタン。押すと
delete_task
メソッドが呼ばれます。
➕ タスク追加機能:add_task
def add_task(self):
task = self.task_entry.get().strip()
if task:
self.task_listbox.insert(tk.END, task)
self.task_entry.delete(0, tk.END)
else:
messagebox.showwarning("警告", "タスクを入力してください")
task_entry.get()
:テキスト入力から文字列を取得。- 空文字でなければ、
Listbox
に追加。 - 入力欄をクリア。
- 何も入力されていない場合は警告を表示。
🗑️ タスク削除機能:delete_task
def delete_task(self):
selected = self.task_listbox.curselection()
if selected:
self.task_listbox.delete(selected[0])
else:
messagebox.showwarning("警告", "削除するタスクを選択してください")
curselection()
:選択されている項目のインデックスを取得。- 選択があれば削除、なければ警告。
🏁 実行部分
if __name__ == "__main__":
root = tk.Tk()
app = TodoApp(root)
root.mainloop()
- プログラムが直接実行されたときにアプリが起動。
root.mainloop()
で GUI イベントループを開始。
📌 まとめ
import tkinter as tk
from tkinter import messagebox
class TodoApp:
def __init__(self, root):
self.root = root
self.root.title("TODOリストアプリ")
self.root.geometry("300x400")
# タイトル
self.title_label = tk.Label(root, text="TODOリスト", font=("Arial", 16))
self.title_label.pack(pady=10)
# タスクリスト表示用
self.task_listbox = tk.Listbox(root, width=40, height=10)
self.task_listbox.pack(pady=10)
# タスク入力
self.task_entry = tk.Entry(root, width=30)
self.task_entry.pack(pady=5)
# ボタン(追加・削除)
self.add_button = tk.Button(root, text="追加", width=10, command=self.add_task)
self.add_button.pack(pady=5)
self.delete_button = tk.Button(root, text="削除", width=10, command=self.delete_task)
self.delete_button.pack(pady=5)
def add_task(self):
task = self.task_entry.get().strip()
if task:
self.task_listbox.insert(tk.END, task)
self.task_entry.delete(0, tk.END)
else:
messagebox.showwarning("警告", "タスクを入力してください")
def delete_task(self):
selected = self.task_listbox.curselection()
if selected:
self.task_listbox.delete(selected[0])
else:
messagebox.showwarning("警告", "削除するタスクを選択してください")
if __name__ == "__main__":
root = tk.Tk()
app = TodoApp(root)
root.mainloop()
このコードで学べるポイント:
- Tkinter の基本構造(ウィジェットの配置、イベント処理)
- クラスで GUI を整理する方法
Listbox
,Entry
,Button
の使い方messagebox
でユーザーへの警告表示
もし改良案や機能追加(保存機能・編集機能など)に興味があれば、お手伝いできます!