高度なウィジェットセレクトター
import tkinter as tk
from tkinter import ttk
def on_select():
for child in container.winfo_children():
child.destroy()
name = combo.get()
if name == "キャンバス (Canvas)":
# 自由描画エリア。円や四角を表示
w = tk.Canvas(container, width=200, height=150, bg="white")
w.create_oval(50, 25, 150, 125, fill="orange") # 円
w.create_text(100, 75, text="Canvas")
elif name == "リストボックス (Listbox)":
# 複数項目を一覧表示
w = tk.Listbox(container, height=5)
for item in ["Apple", "Banana", "Cherry", "Dragonfruit"]:
w.insert(tk.END, item)
elif name == "メッセージ (Message)":
# 長いテキストを幅に合わせて折り返す
w = tk.Message(container, text="これはMessageウィジェットです。Labelとは違い、長い文章を自動で折り返して表示する機能を持っています。", width=200)
elif name == "ラベル付きフレーム (Labelframe)":
# 枠線にタイトルがつく
w = tk.LabelFrame(container, text="設定グループ", padx=10, pady=10)
tk.Label(w, text="フレームの中のラベル").pack()
tk.Button(w, text="フレームの中のボタン").pack()
elif name == "セパレータ (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 == "ノートブック (Notebook)":
# タブ切り替え
w = ttk.Notebook(container)
f1 = tk.Frame(w); f2 = tk.Frame(w)
w.add(f1, text="Tab 1"); w.add(f2, text="Tab 2")
tk.Label(f1, text="1枚目のページ").pack(pady=20)
tk.Label(f2, text="2枚目のページ").pack(pady=20)
else:
# 以前紹介したものはシンプルに表示(一部抜粋)
w = tk.Label(container, text="(以前のウィジェットもここに追加可能です)")
w.pack(pady=20, padx=20)
container.update()
# --- メイン画面の設定 ---
root = tk.Tk()
root.title("Advanced Widget Selector")
root.geometry("400x600")
widget_list = [
"キャンバス (Canvas)",
"リストボックス (Listbox)",
"メッセージ (Message)",
"ラベル付きフレーム (Labelframe)",
"セパレータ (Separator)",
"ノートブック (Notebook)"
]
combo = ttk.Combobox(root, values=widget_list, state="readonly", font=("Arial", 12))
combo.set("-- 高度なウィジェットを選択 --")
combo.pack(pady=20)
tk.Button(root, text="表示する", command=on_select, bg="#2196F3", fg="white").pack()
container = tk.Frame(root, bd=1, relief="ridge", width=350, height=300)
container.pack(pady=20)
container.pack_propagate(False)
root.mainloop()
コメント
コメントを投稿