実行サンプル
tcl/tkウィジェット一覧と動作例
サンプルコード
https://github.com/sakurataiko1/python_sample/tree/main/sample_tcltk/tcl_GUI_study
実行環境
windows10
python3.9.5
pythonモジュール tcl/tk
実行手順
ソースコードを作成する
windowsコマンドプロンプトでプログラム作成したフォルダに移動し、下記実行する
python プログラム名.py
ソースコード
import tkinter as tk
import tkinter.messagebox as tmb
from tkinter import ttk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master.title("GUI_tck_tk")
self.master.geometry("900x800")
#-------------------------
# Labelframe and Label 1, 2
# 部品定義
self.label1 = tk.Label(self.master, text="1, 2: Labelframe \nand Label")
self.labelframe = tk.LabelFrame(self.master, text="LabelFrame")
self.label2 = tk.Label(self.labelframe, text="Label")
self.g_count_labelChange = 0
self.button_labelChange = tk.Button(self.labelframe, text="ラベル書き換え", command=self.func_1_1_labelChange)
# 部品配置
self.label1.grid(row=0, sticky="e")
self.labelframe.grid(row=0, column=1, padx=10, pady=10, sticky='ew')
self.label2.pack()
self.button_labelChange.pack()
# -------------------------
# Button 3
# 部品定義
self.label3 = tk.Label(self.master, text="3: Button")
self.button3 = tk.Button(self.master, text="Button", command=lambda: tmb.showinfo("メッセージ", "clicked"))
# 部品配置
self.label3.grid(row=1, sticky="e")
self.button3.grid(row=1, column=1, padx=10, pady=10, sticky='w')
# -------------------------
# Entry 4
# 部品定義
self.label4 = tk.Label(self.master, text="4: Entry")
self.frame4 = tk.Frame(self.master)
self.entry4 = tk.Entry(self.frame4)
self.button4_1 = tk.Button(self.frame4, text="Input1", command=self.func_4_1)
self.button4_2 = tk.Button(self.frame4, text="Input2", command=self.func_4_2)
self.button4_3 = tk.Button(self.frame4, text="output", command=self.func_4_3)
# 部品配置
self.label4.grid(row=2, sticky="e")
self.frame4.grid(row=2, column=1, padx=10, pady=10, sticky='ew')
self.entry4.pack(side='left')
self.button4_1.pack(side='left')
self.button4_2.pack(side='left')
self.button4_3.pack(side='left')
# -----------------------------------------------------
# Checkbutton 5
# 部品定義
self.label5 = tk.Label(self.master, text="5: CheckButton")
self.frame5 = tk.Frame(self.master)
self.entry5 = tk.Entry(self.frame5)
self.value_check5 = tk.BooleanVar(value=False)
self.checkButton5 = tk.Checkbutton(self.frame5, text="A", command=self.func_5_check_clicked, variable=self.value_check5)
# 部品配置
self.label5.grid(row=3, sticky="e")
self.frame5.grid(row=3, column=1, padx=10, pady=10, sticky='ew')
self.checkButton5.pack(side='left')
self.entry5.pack(side='left')
self.value_check5.set(True)
# -----------------------------------------------------
# radioButton 6
# 部品定義
self.label6 = tk.Label(self.master, text="6: RadioButton")
self.frame_for_radio = tk.Frame(self.master)
self.iv1 = tk.IntVar()
self.iv1.set(1)
self.radioButton6_1 = tk.Radiobutton(self.frame_for_radio, text="a", value=1, variable=self.iv1)
self.radioButton6_2 = tk.Radiobutton(self.frame_for_radio, text="b", value=2, variable=self.iv1)
self.radioButton6_3 = tk.Radiobutton(self.frame_for_radio, text="c", value=3, variable=self.iv1)
self.button6 = tk.Button(self.frame_for_radio, text="output", command=self.func_6_button6_clicked)
self.entry6 = tk.Entry(self.frame_for_radio)
# 部品配置
self.label6.grid(row=4, sticky='e')
self.frame_for_radio.grid(row=4, column=1, padx=10, pady=10, sticky='ew')
self.radioButton6_1.pack(side='left')
self.radioButton6_2.pack(side='left')
self.radioButton6_3.pack(side='left')
self.button6.pack(side='left')
self.entry6.pack(side='left')
# -----------------------------------------------------
# OptionMenu 7
# 部品定義
self.label7 = tk.Label(root, text="7: OptionMenu & comboBox").grid(row=5, column=0, sticky="en")
self.frame7 = tk.Frame(self.master)
self.frame7_1 = tk.Frame(self.frame7)
self.frame7_2_1 = tk.Frame(self.frame7)
self.frame7_2_2 = tk.Frame(self.frame7)
self.label7_1 = tk.Label(self.frame7_1, text="OptionMenu")
self.value_option7 = tk.StringVar()
# self.OptionMenu7 = tk.OptionMenu(self.frame7, self.sv1, 'オプション1', 'オプション2')
self.menu_list = ['オプション1', 'オプション2', 'オプション3']
self.value_option7.set('オプション1') # 選択肢のデフォルト値
self.OptionMenu7 = tk.OptionMenu(self.frame7_1, self.value_option7, *self.menu_list)
self.button7 = tk.Button(self.frame7_1, text="output", command=self.func_7_button7_clicked)
self.entry7 = tk.Entry(self.frame7_1)
#
self.label7_2_1 = ttk.Label(self.frame7_2_1, text='ComboBox')
self.GUIlist_combobox1 = ["アイテム0", "アイテム1", "アイテム2"]
self.GUIvalue_combobox1 = tk.StringVar()
self.combobox7_2 = ttk.Combobox(self.frame7_2_1, textvariable=self.GUIvalue_combobox1, values=self.GUIlist_combobox1, state="readonly") #style="office.TCombobox")
self.combobox7_2.bind('<<ComboboxSelected>>', self.func_7_2_comboBox_selected)
self.button7_2_0 = ttk.Button(self.frame7_2_1, text='0', width=2, command=self.func_7_2_button_7_2_0_clicked)
self.button7_2_1 = ttk.Button(self.frame7_2_1, text='1', width=2, command=self.func_7_2_button_7_2_1_clicked)
self.button7_2_2 = ttk.Button(self.frame7_2_1, text='2', width=2, command=self.func_7_2_button_7_2_2_clicked)
self.label7_2_2 = ttk.Label(self.frame7_2_2, text='message')
# 部品配置
self.frame7.grid(row=5, column=1, padx=10, pady=10, sticky='ew')
self.frame7_1.pack(side='top', anchor='w') # anchor=w 左寄せ
self.frame7_2_1.pack(side='top', anchor='w') # anchor=w 左寄せ
self.frame7_2_2.pack(side='top', anchor='w') # anchor=w 左寄せ
# 部品配置 7_1
self.label7_1.pack(side='left')
self.OptionMenu7.pack(side='left')
self.button7.pack(side='left')
self.entry7.pack(side='left')
# 部品配置 7_2
self.label7_2_1.pack(side='left', padx=2, pady=5)
self.combobox7_2.pack(side='left', padx=2, pady=5)
self.button7_2_0.pack(side='left', padx=2, pady=5)
self.button7_2_1.pack(side='left', padx=2, pady=5)
self.button7_2_2.pack(side='left', padx=2, pady=5)
self.label7_2_2.pack(side='left', padx=2, pady=5)
#
# -----------------------------------------------------
# Listbox 8
# 部品定義
self.label8 = tk.Label(root, text="8: Listbox")
self.frame8 = tk.Frame(root)
self.frame8_1 = tk.Frame(self.frame8)
self.frame8_2 = tk.Frame(self.frame8)
self.frame8_1_listScroll = tk.Frame(self.frame8_1)
# # 部品定義 リストボックス
list_value = tk.StringVar()
list_value.set(["Aさん", "Bさん", "Cさん", "Dさん", "Eさん"])
# selectmodeの種類(single:1つだけ選択できる、multiple:複数選択できる、extended:複数選択可能+ドラッグでも選択可能)
#self.listbox8 = tk.Listbox(root, height=4)
self.listbox8 = tk.Listbox(self.frame8_1_listScroll, height=8, listvariable=list_value, selectmode="single")
#for line in ["選択肢1", "選択肢2", "選択肢3", "選択肢4", "選択肢5"]:
# listbox8.insert(tk.END, line)
self.listbox8.select_set(1)
#
# # 部品定義 スクロールバー
self.scroll_bar8 = tk.Scrollbar(self.frame8_1_listScroll, command=self.listbox8.yview)
# # 部品定義 ボタン
self.button8_result = tk.Button(self.frame8_2, text="判定", command=self.func_8_check)
self.button8_allselect = tk.Button(self.frame8_2, text="全選択", command=self.func_8_all_select)
self.button8_allclear = tk.Button(self.frame8_2, text="全クリア", command=self.func_8_all_clear)
# # 部品定義 ラベル 結果表示用
self.var_label8 = tk.StringVar()
self.var_label8.set("ボタンを押して下さい")
self.label8_result = tk.Label(self.frame8_2, textvariable=self.var_label8)
#
# 部品配置
self.label8.grid(row=6, sticky="e")
self.frame8.grid(row=6, column=1, padx=10, pady=10, sticky='ew')
self.frame8_1_listScroll.grid(row=0, column=0, padx=10, pady=10, sticky='ew')
self.frame8_2.grid(row=0, column=1, padx=10, pady=10, sticky='nsew')
#
self.scroll_bar8.pack(side=tk.RIGHT, fill=tk.Y)
self.listbox8.pack(side='left')
#
self.label8_result.pack(side='top')
self.button8_result.pack(side='left')
self.button8_allselect.pack(side='left')
self.button8_allclear.pack(side='left')
# -----------------------------------------------------
# Spinbox 9
# 部品定義
self.label9 = tk.Label(self.master, text="9: Spinbox")
self.frame9 = tk.Frame(self.master)
#self.var_spinbox9 = tk.IntVar(self.master)
self.var_spinbox9 = tk.DoubleVar(self.master)
self.var_spinbox9.set(0) # 初期値セット
# 値が整数の場合 # self.spinbox9 = tk.Spinbox( self.frame9, textvariable=self.var_spinbox9, from_=-10, to=10, increment=1)
self.spinbox9 = tk.Spinbox( self.frame9, textvariable=self.var_spinbox9, from_=-10, to=10, increment=0.1, format="%.1f")
self.label9_result = tk.Label(self.frame9, textvariable=self.var_spinbox9)
#self.entry9 = tk.Entry(self.frame9, ) # 結果表示用
# 部品配置
self.label9.grid(row=0, column=2, sticky="e")
self.frame9.grid(row=0, column=3, padx=10, pady=10)
self.spinbox9.pack(side='left')
self.label9_result.pack(side='left')
#self.entry9.pack(side='left')
# -----------------------------------------------------
# Scale 10
# 部品定義
self.label10 = tk.Label(self.master, text="10: Scale")
self.frame10 = tk.Frame(self.master)
# 値が整数の場合 # self.var_scale10 = tk.IntVar(self.master)
self.var_scale10 = tk.DoubleVar(self.master)
# 値が整数の場合 # self.scale10 = tk.Scale(self.frame10, from_=10, to=80, label='Scale', orient=tk.HORIZONTAL, variable=self.var_scale10)
self.scale10 = tk.Scale(self.frame10, from_=-100, to=100, label='Scale', orient=tk.HORIZONTAL, resolution=0.1, variable=self.var_scale10)
self.entry10 = tk.Entry(self.frame10, textvariable=self.var_scale10)
# 部品配置
self.label10.grid(row=1, column=2, sticky="e")
self.frame10.grid(row=1, column=3, padx=10, pady=10)
self.scale10.pack(side='left')
self.entry10.pack(side='left')
# -----------------------------------------------------
# Message 11
# 部品定義
label11 = tk.Label(root, text="11: Message")
message11 = tk.Message(root, text="私の名前は中村です")
# 部品配置
label11.grid(row=2, column=2, sticky="e")
message11.grid(row=2, column=3, padx=10, pady=10)
# -----------------------------------------------------
# Text 12
# 部品定義
label12 = tk.Label(root, text="12: Text")
text12 = tk.Text(root, width=20, height=6)
text12.insert(tk.END, "sample text\n1\n2\n3")
# 部品配置
label12.grid(row=3, column=2, sticky="e")
text12.grid(row=3, column=3, padx=10, pady=10)
# -----------------------------------------------------
# ScrollBar with Listbox 13
# 部品定義
label13 = tk.Label(root, text="13: Listbox \n& ScrollBar")
scrollbar_frame = tk.Frame(root)
scrollbar_frame.grid(row=4, column=3, padx=10, pady=10)
listbox13 = tk.Listbox(scrollbar_frame)
for i in range(1000):
listbox13.insert(tk.END, i)
scroll_bar = tk.Scrollbar(scrollbar_frame, command=listbox13.yview)
listbox13.config(yscrollcommand=scroll_bar.set)
# 部品配置
label13.grid(row=4, column=2, sticky="e")
listbox13.pack(side=tk.LEFT)
scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
# -----------------------------------------------------
# Canvas 14
# 部品定義
label14 = tk.Label(root, text="14: Canvas")
canvas = tk.Canvas(root, bg='white', width=200, height=100)
canvas.create_oval(25, 15, 180, 60, fill='red')
canvas.create_oval(25, 45, 180, 85, fill='blue')
canvas.create_text(100, 90, text='Canvasウィジェット', fill="green")
# 部品配置
label14.grid(row=5, column=2, sticky="e")
canvas.grid(row=5, column=3, padx=10, pady=10)
# PanedWindow 15
label15 = tk.Label(root, text="15: PanedFrame").grid(row=6, column=2, sticky="e")
panedwindow_frame = tk.Frame(root)
panedwindow_frame.grid(row=6, column=3, padx=10, pady=10)
panedwindow1 = tk.PanedWindow(panedwindow_frame)
text1 = tk.Text(panedwindow1, height=6, width=15)
text1.insert(tk.END, "中村拓男")
panedwindow1.add(text1)
panedwindow1.pack(fill=tk.BOTH, expand=2)
panedwindow2 = tk.PanedWindow(panedwindow1)
text2 = tk.Text(panedwindow1, height=6, width=15)
text2.insert(tk.END, "中村香織")
panedwindow2.add(text2)
panedwindow1.add(panedwindow2)
# ------------------------
# 関数
def func_1_1_labelChange(self):
self.g_count_labelChange += 1
self.label2['text'] = str(self.g_count_labelChange)
def func_4_1(self):
self.entry4.delete(0, tk.END) # テキストボックスクリア
self.entry4.insert(tk.END, u'test input1')
def func_4_2(self):
self.entry4.delete(0, tk.END) # テキストボックスクリア
self.entry4.insert(tk.END, u'test input2')
def func_4_3(self):
tmb.showinfo("title_dialog", self.entry4.get())
# ------------------------
def func_5_check_clicked(self):
tmp_value = self.value_check5.get()
# print(f"チェックの状態は {self.value_check5} です")
self.entry5.delete(0, tk.END)
self.entry5.insert(tk.END, f"チェックの状態は {tmp_value} です")
# ------------------------
def func_6_button6_clicked(self):
tmp_value = self.iv1.get()
self.entry6.delete(0, tk.END)
self.entry6.insert(tk.END, f"ラジオボタン{tmp_value}が選択されました")
# ------------------------
def func_7_button7_clicked(self):
tmp_value = self.value_option7.get()
self.entry7.delete(0, tk.END)
self.entry7.insert(tk.END, f"{tmp_value}が選択されました")
def func_7_2_comboBox_selected(self, in_selectStr):
msg = " comboBox7_2 selected text=" + self.combobox7_2.get()
msg += "\n comboBox7_2 selected index=" + str(self.combobox7_2.current())
#print(msg)
self.label7_2_2['text'] = msg
tk.messagebox.showinfo("info", msg)
def func_7_2_button_7_2_0_clicked(self):
self.combobox7_2.current(0) # インデックス0の値にする
def func_7_2_button_7_2_1_clicked(self):
self.combobox7_2.set("アイテム1") # コンボ値を選択
def func_7_2_button_7_2_2_clicked(self):
self.combobox7_2.set("アイテム2") # コンボ値を選択
# ------------------------
def func_8_check(self):
# ボタンを押したら選択している人を表示する
text_check=self.listbox8.curselection()
self.var_label8.set(self.listbox8.get(text_check))
def func_8_all_select(self):
# 全選択する
self.listbox8.select_set(0,tk.END)
def func_8_all_clear(self):
# 全クリアする
self.listbox8.select_clear(0,tk.END)
# ------------------------
if __name__ == "__main__":
root = tk.Tk()
app = Application(master=root)
app.mainloop()