Tkinter(Tキンター)とは?
Pythonの標準ライブラリです。
Tkオブジェクト
トップレベルの全体の画面ウィジェット(画面パーツ)になります。
サンプルコード
1 2 3 4 5 6 |
import tkinter root = tkinter.Tk() root.title('アプリタイトル') root.geometry('300x200') # 横の長さx縦の長さ root.mainloop() # アプリ起動 |
実行
以下のようなデスクトップアプリが起動します。

widget
Frame(フレーム)クラス
部品を一つにまとめるためのクラスです。
Button
ボタン
1 |
submit_btn=tkinter.Button(self,text='実行',command=self.input_handler) # ボタンのクラス |
Entry
単数行のテキストボックスのオブジェクト作成時に使います。
入力
1 2 3 |
self.text_box=tkinter.Entry(self) self.text_box['width']=10 self.text_box.pack() |
取得
1 |
text=self.text_box.get() |
Text
複数行入力ができるテキストボックス
入力
1 |
self.text=tkinter.Text(self,width=20,height=10) |
取得
1 2 |
t = self.text.get(1.0,tkinter.END + '-1c') # 開始行と終了行を指定します。 print(t) |
Radiobutton
ラジオボタン
1 2 3 4 5 6 7 |
self.selected_radio = tkinter.StringVar() # どのラジオボタンが押されたか知るためのもの radio_1 = tkinter.Radiobutton(self,text='選択肢1',value="選択肢1",variable=self.selected_radio) radio_2 = tkinter.Radiobutton(self,text='選択肢2',value="選択肢2",variable=self.selected_radio) radio_3 = tkinter.Radiobutton(self,text='選択肢3',value="選択肢3",variable=self.selected_radio) radio_1.pack() radio_2.pack() radio_3.pack() |
取得
どの選択肢が選択されたか取得できます。
1 |
self.selected_radio.get() |
Message
メッセージを出力できます。
1 2 |
self.message = tkinter.Message(self,width=200) self.message['text'] = 'メッセージ内容' |
Label
Messageと似ていますがこちらは見出しを表示させます。
1 2 |
self.label = tkinter.Label(self,text='見出し名') self.label.pack() |
Checkbutton
チェックボックス
1 2 3 |
self.is_teacher = tkinter.BooleanVar() # チェックされているか知るためのオブジェクト self.chk_btn = tkinter.Checkbutton(self,text='先生',variable=self.is_teacher) self.chk_btn.pack() |
チェックされているか知る。
上記サンプルでいえば以下のように確認できます。False/Trueで返ってきます。(チェックされていればTrueが返る)
1 |
self.is_teacher.get() |
ListBox
1 2 3 4 |
self.items = ['JavaScript','Java','Python'] list_items = tkinter.StringVar(value=self.items) self.list_box = tkinter.Listbox(self,listvariable=list_items) self.list_box.pack() |
取得
以下の構文で選択した値を取得できます。
selected_index = self.list_box.curselection()[0] # 選択したListの番地を取得できます。
print(self.items[selected_index]) # 選択値を出力できる。
SpinBox
作成時は、readonlyにしないとアプリケーションを操作する人が選択肢が操作できてしまうので必ず指定するようにしましょう。
1 2 3 |
items = ['JavaScript','Java','Python'] self.spin_box = tkinter.Spinbox(self,state='readonly',values=items) self.spin_box.pack() |
取得
選ばれた値を取得できます。
1 |
self.spin_box.get() |
Menu
1 2 3 4 5 |
menu = tkinter.Menu(self.root) menu1 = tkinter.Menu(menu,tearoff=False) menu1.add_command(label='画面設定',command=self.screen_setting) menu.add_cascade(label="設定",menu=menu1) self.root.config(menu=menu) |
「画面設定」が押されたときに呼ぶ関数
1 2 |
def screen_setting(self): print("画面設定が完了しました。") |
画面の左上に「設定」というメニューを定義できます。クリックすれば「画面設定」という選択肢が出てきます。(押せば「screen_setting」が実行される。)

filedialog
ファイルダイアログ
閉じるボタンを配置するサンプル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import tkinter # tkinterのFrameクラスを継承 class Application(tkinter.Frame): def __init__(self,root): # Frameクラスのinitializeを呼ぶ。 # root(tKオブジェクトを呼ぶ)を渡す。 super().__init__(root, width=380, height=280, borderwidth=4, # フレームの境界線の太さ relief='groove') # フレームの境界線の種類 self.root=root # クラスの別メソッドで使えるようにTkオブジェクトはインスタンス変数に格納しておく。 self.pack() # 1番上に配置 self.pack_propagate(0) # サイズ調整 self.create_close_button() def create_close_button(self): quit_btn=tkinter.Button(self) # ボタンのクラス quit_btn['text']='閉じる' # ボタンに表示させる文字 quit_btn['command']=self.root.destroy quit_btn.pack(side='bottom') root = tkinter.Tk() root.title('アプリタイトル') root.geometry('400x300') # 横の長さx縦の長さ app = Application(root=root) root.mainloop() # アプリ起動 |
実行結果
以下のように閉じるボタンができて押すとアプリ自体が終了します。

テキストボックスに入力して表示させるサンプル
複数のウィジェットを配置する場合は以下のように記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import tkinter # tkinterのFrameクラスを継承 class Application(tkinter.Frame): def __init__(self,root): super().__init__(root, width=380, height=280, borderwidth=4, relief='groove') self.root=root self.pack() # 1番上に配置 self.pack_propagate(0) # サイズ調整 self.create_widgets() # ウィジェットを作成 def create_widgets(self): # テキストボックス self.text_box=tkinter.Entry(self) self.text_box['width']=10 self.text_box.pack() # 実行ボタン submit_btn=tkinter.Button(self) # ボタンのクラス submit_btn['text']='実行' submit_btn['command']=self.input_handler submit_btn.pack() # メッセージ出力 self.message = tkinter.Message(self) self.message.pack() # インプットハンドラー def input_handler(self): text=self.text_box.get() # 値を取得できる。 self.message['text'] = 'messe:' + text root = tkinter.Tk() root.title('アプリタイトル') root.geometry('400x300') # 横の長さx縦の長さ app = Application(root=root) root.mainloop() # アプリ起動 |
実行
テキストボックスで入力した値をボタンを押せば表示することができます。

この記事へのコメントはありません。