GUIコンポーネント・ウィジェット100本ノック3本目
import tkinter as tk
from tkinter import ttk
def show_knock():
for child in container.winfo_children():
child.destroy()
name = combo.get()
# --- 100本ノック 第3セット メインロジック ---
if name == "21. Listbox (複数選択可能)":
# selectmode="multiple" で複数選択を許可
w = tk.Listbox(container, selectmode="multiple", height=5)
for i in range(5): w.insert(tk.END, f"項目 {i+1}")
elif name == "22. Listbox (スクロール連動)":
f = tk.Frame(container)
sb = tk.Scrollbar(f, orient="vertical")
lb = tk.Listbox(f, yscrollcommand=sb.set)
sb.config(command=lb.yview)
for i in range(50): lb.insert(tk.END, f"大量データ {i}")
lb.pack(side="left"); sb.pack(side="right", fill="y")
w = f
elif name == "23. Treeview (シンプル表)":
w = ttk.Treeview(container, columns=("Age", "Work"), show="headings", height=4)
w.heading("Age", text="年齢"); w.heading("Work", text="職業")
w.insert("", "end", values=("25", "エンジニア"))
w.insert("", "end", values=("32", "デザイナー"))
elif name == "24. Treeview (階層構造)":
w = ttk.Treeview(container, height=5)
root_node = w.insert("", "end", text="プロジェクトA")
w.insert(root_node, "end", text="設計図.pdf")
w.insert(root_node, "end", text="ソースコード.py")
elif name == "25. Combobox (入力不可設定)":
# state="readonly" でユーザーの直接入力を禁止
w = ttk.Combobox(container, values=("Apple", "Banana", "Cherry"), state="readonly")
elif name == "26. Progressbar (確定モード)":
# 0から100まで進捗を表示
w = ttk.Progressbar(container, length=200, mode='determinate', value=75)
elif name == "27. Progressbar (非確定モード)":
# 左右にバーが動く「準備中」の表示
w = ttk.Progressbar(container, length=200, mode='indeterminate')
w.start(10)
elif name == "28. Separator (水平区切り線)":
w = tk.Frame(container)
tk.Label(w, text="上のエリア").pack()
ttk.Separator(w, orient="horizontal").pack(fill="x", pady=10)
tk.Label(w, text="下のエリア").pack()
elif name == "29. Notebook (タブの追加)":
w = ttk.Notebook(container)
tab1 = tk.Frame(w); tab2 = tk.Frame(w)
w.add(tab1, text="設定"); w.add(tab2, text="ログ")
tk.Label(tab1, text="設定画面です").pack(pady=10)
tk.Label(tab2, text="ログを表示中...").pack(pady=10)
elif name == "30. PanedWindow (サイズ調整バー)":
w = tk.PanedWindow(container, orient="horizontal", bg="gray", sashrelief="raised")
left = tk.Label(w, text="左", bg="lightpink")
right = tk.Label(w, text="右", bg="lightyellow")
w.add(left); w.add(right)
w.pack(pady=10, padx=10, fill="both", expand=True)
container.update()
# --- ベース画面 ---
root = tk.Tk()
root.title("Tkinter 100本ノック 第3章")
root.geometry("400x550")
knock_list = [
"21. Listbox (複数選択可能)", "22. Listbox (スクロール連動)", "23. Treeview (シンプル表)",
"24. Treeview (階層構造)", "25. Combobox (入力不可設定)", "26. Progressbar (確定モード)",
"27. Progressbar (非確定モード)", "28. Separator (水平区切り線)", "29. Notebook (タブの追加)",
"30. PanedWindow (サイズ調整バー)"
]
combo = ttk.Combobox(root, values=knock_list, state="readonly", width=30)
combo.set("ノックを選択してください")
combo.pack(pady=20)
tk.Button(root, text="ノック実行!", command=show_knock, bg="lightgreen").pack()
container = tk.Frame(root, bd=2, relief="sunken", width=350, height=250)
container.pack(pady=20)
container.pack_propagate(False)
root.mainloop()
コメント
コメントを投稿