ウィジェットサンプル表示

 import tkinter as tk

from tkinter import ttk


def on_select():

    # 古いウィジェットを削除

    for child in container.winfo_children():

        child.destroy()

    

    # プルダウンで選択された値を取得

    name = combo.get()

    

    # --- ウィジェットの生成ロジック ---

    if name == "ボタン (Button)":

        w = tk.Button(container, text="クリックできます", command=lambda: print("Clicked!"))

        

    elif name == "ラベル (Label)":

        w = tk.Label(container, text="これはテキストを表示する部品です", fg="darkgreen")

        

    elif name == "入力ボックス (Entry)":

        w = tk.Entry(container)

        w.insert(0, "ここに入力")

        

    elif name == "チェックボックス (Check)":

        w = tk.Checkbutton(container, text="承諾する")

        

    elif name == "ラジオボタン (Radio)":

        w = tk.Frame(container)

        tk.Radiobutton(w, text="男性", value=1).pack(side="left")

        tk.Radiobutton(w, text="女性", value=2).pack(side="left")

        

    elif name == "スライダー (Slider)":

        w = tk.Scale(container, from_=0, to=100, orient=tk.HORIZONTAL, length=200)

        

    elif name == "プログレスバー (Progress)":

        w = ttk.Progressbar(container, length=200, mode='indeterminate')

        w.start(10)

        

    elif name == "スピンボックス (Spin)":

        w = tk.Spinbox(container, from_=0, to=99)

        

    elif name == "複数行テキスト (Text)":

        w = tk.Text(container, width=25, height=5)

        w.insert("1.0", "ここにメモが書けます。")


    else:

        return


    # 画面に配置

    w.pack(pady=40)

    container.update()


# --- メイン画面の設定 ---

root = tk.Tk()

root.title("Pydroid Widget Selector")

root.geometry("400x550")


# タイトル

tk.Label(root, text="表示するウィジェットを選んでください", font=("Arial", 11, "bold")).pack(pady=15)


# プルダウンメニューのリスト

widget_list = [

    "ボタン (Button)",

    "ラベル (Label)",

    "入力ボックス (Entry)",

    "チェックボックス (Check)",

    "ラジオボタン (Radio)",

    "スライダー (Slider)",

    "プログレスバー (Progress)",

    "スピンボックス (Spin)",

    "複数行テキスト (Text)"

]


# プルダウン(Combobox)の作成

combo = ttk.Combobox(root, values=widget_list, state="readonly", font=("Arial", 12))

combo.set("-- 選択してください --") # 初期値

combo.pack(pady=10)


# 表示実行ボタン

btn = tk.Button(root, text="表示!", command=on_select, bg="#4CAF50", fg="white", width=15, height=2)

btn.pack(pady=5)


# 表示エリア

container = tk.Frame(root, bd=1, relief="solid", width=350, he

ight=250)

container.pack(pady=20)

container.pack_propagate(False)


root.mainloop()

コメント