Tag: python Tag: gui Tag: プログラミング

Tkinter

  • GeminiやChatGPTに問い合わせして簡単なサンプルを集めてみよう!

参考URL


サンプル集

ポップアップ窓

すべて開くすべて閉じる
 
 
 
 
-
!
 
-
!
 
 
 
-
!
 
 
-
!
import tkinter as tk
from tkinter import messagebox  # ポップアップ表示用のモジュールをインポート
 
def show_popup():
    # メッセージボックス(情報ダイアログ)を表示
    messagebox.showinfo("お知らせ", "Tkinter")
 
# メインウィンドウの作成
root = tk.Tk()
root.title("ポップアップサンプル")
root.geometry("300x150")
 
# ボタンを作成し、クリック時に show_popup 関数を実行するように設定
button = tk.Button(root, text="クリックしてね", command=show_popup)
button.pack(pady=50)
 
# アプリの実行
root.mainloop()

確認ダイアログ

すべて開くすべて閉じる
 
 
 
 
-
|
!
 
-
!
 
 
 
 
-
!
 
 
 
-
!
 
 
-
|
!
 
 
-
!
import tkinter as tk
from tkinter import messagebox  # ポップアップ用のモジュールをインポート
 
def ask_confirm():
    # 「はい/いいえ」を確認するダイアログを表示
    # Yes のときは True、No のときは False が返ってきます
    result = messagebox.askyesno("確認", "よろしいですか。")
    
    # 選択結果に応じてラベルのテキストを更新
    if result:
        status_label.config(text="結果:Yes が押されました")
    else:
        status_label.config(text="結果:No が押されました")
 
# メインウィンドウの作成
root = tk.Tk()
root.title("選択確認サンプル")
root.geometry("350x200")
 
# メインのボタン(中央に配置)
button = tk.Button(root, text="確認ダイアログを開く", command=ask_confirm)
button.pack(pady=60)
 
# 結果を表示するラベル(左下に配置するための設定)
# anchor="w" でテキストを左寄せにし、pack の side="bottom", fill="x" で最下部に横いっぱいに配置します
status_label = tk.Label(root, text="結果:未選択", anchor="w", padx=10, pady=5)
status_label.pack(side="bottom", fill="x")
 
# アプリの実行
root.mainloop()

用意した情報を元に入力欄を10個表示

すべて開くすべて閉じる
 
 
 
-
!
 
 
-
!
-
!
-
!
 
-
!
 
-
!
 
-
!
 
 
 
-
|
!
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
-
!
-
!
 
 
-
!
 
-
!
 
-
!
 
-
!
 
 
-
!
import tkinter as tk
 
def focus_next(event):
    # event.widget は現在イベントが発生した(Enterが押された)部品を指します
    current_widget = event.widget
    
    try:
        # 現在の入力欄がリストの何番目にあるかインデックスを取得
        current_index = entries.index(current_widget)
        # 次のインデックスを計算(最後の入力欄の場合は最初に戻る)
        next_index = (current_index + 1) % len(entries)
        # 次の入力欄にフォーカスを移動
        entries[next_index].focus_set()
    except ValueError:
        # リスト外の部品でEnterが押された場合は何もしない
        pass
    
    # Enterキー本来の挙動(ビープ音など)を防止するために "break" を返す
    return "break"
 
# メインウィンドウの作成
root = tk.Tk()
root.title("エントリー一括生成 & フォーカス移動")
root.geometry("450x450")
 
# 1. 配列(リスト)に「表示位置(x, y)」と「入力欄の幅(文字数)」を定義
# 10個の入力欄データをまとめて用意します
layout_data = [
    {"x": 30,  "y": 20,  "width": 10},
    {"x": 150, "y": 20,  "width": 30},
    {"x": 30,  "y": 60,  "width": 20},
    {"x": 220, "y": 60,  "width": 20},
    {"x": 30,  "y": 100, "width": 45},
    {"x": 30,  "y": 140, "width": 15},
    {"x": 180, "y": 140, "width": 15},
    {"x": 30,  "y": 180, "width": 45},
    {"x": 30,  "y": 220, "width": 25},
    {"x": 250, "y": 220, "width": 20},
]
 
# 作成した入力欄オブジェクトを保持しておくためのリスト
entries = []
 
# 2. 定義データに基づいてループ処理で入力欄を生成・配置
for i, data in enumerate(layout_data):
    # どの欄か分かりやすいように仮の初期テキストを入れておきます
    entry = tk.Entry(root, width=data["width"])
    entry.insert(0, f"入力欄 {i + 1}")
    
    # place(プレース)を使って、指定された絶対座標 (x, y) に配置
    entry.place(x=data["x"], y=data["y"])
    
    # Enterキー(Returnキー)が押されたときに、上で定義した focus_next 関数を実行するように紐付け(バインド)
    entry.bind("<Return>", focus_next)
    
    # 後からインデックス検索できるようにリストに追加
    entries.append(entry)
 
# 最初の入力欄にあらかじめフォーカスを当てておく
if entries:
    entries[0].focus_set()
 
# アプリの実行
root.mainloop()