tkinterの使い方メモ

初めのひな形
from tkinter import *
from tkinter import ttk

root = Tk()

root.mainloop()
ウィンドウの大きさ座標の設定
# ウィンドウの大きさ座標の設定
root.geometry( "400x300+500+100") # 幅x高さ+ウィンドウのx座標+ウィンドウのy座標
ボタンを作る
#text : ボタンに表示されるテキスト
#commmand : ボタンがクリックされたときに呼び出す関数を指定
btn1 = ttk.Button(root,text="End", command = btn1_clicked)

# side : 表示位置指定 top buttom left right
btn1.pack(side = "bottom")
変数操作
#変数の宣言
# StringVar() 文字列
# IntVar() 整数
# DoubleVar() 少数
# BooleanVar() 真偽
var1 = StringVar()

#変数に代入
var1.set("OK")

#変数を取得
print(var1.get())
ラベルを作る
# ラベルを宣言する
# テキストを書き換える場合は textvariable = ""
# テキストを書き換えない場合は text = ""
label1 = ttk.Label(root , textvariable= "OK")
# side : 表示位置指定 top buttom left right
label1.pack(side = "bottom")
ラベルを書き換える
#変数の宣言
# StringVar() 文字列
# IntVar() 整数
# DoubleVar() 少数
# BooleanVar() 真偽
var1 = StringVar()

#変数に代入
var1.set("OK")
# ラベルを宣言する
label1 = ttk.Label(root , textvariable= var1)

# side : 表示位置指定 top buttom left right
label1.pack(side = "bottom")
ボタンが押された時にラベルを書き換えるサンプルコード
from tkinter import *
from tkinter import ttk


def btn1_clicked():
    var1.set("NO")
    print("ボタンが押されました。")

root = Tk()

# ウィンドウの大きさ座標の設定
root.geometry( "400x300+500+100") # 幅x高さ+ウィンドウのx座標+ウィンドウのy座標

# ボタンを宣言する
#text : ボタンに表示されるテキスト
#commmand : ボタンがクリックされたときに呼び出す関数を指定
btn1 = ttk.Button(root,text="End", command = btn1_clicked)

# ラベルを宣言する
# テキストを書き換える場合は textvariable = ""
# テキストを書き換えない場合は text = ""
label1 = ttk.Label(root , textvariable= var1)

#変数の宣言
# StringVar() 文字列
# IntVar() 整数
# DoubleVar() 少数
# BooleanVar() 真偽
var1 = StringVar()

#変数に代入
var1.set("OK")
# ラベルを宣言する
label1 = ttk.Label(root , textvariable= var1)

# side : 表示位置指定 top buttom left right
label1.pack(side = "bottom")

# ボタンを設置する
# side : 表示位置指定 top buttom left right
btn1.pack(side = "bottom")


root.mainloop()
Entry (1行の文字列入力)
# Entryを宣言する
# テキストを書き換える場合は textvariable = ""
# テキストを書き換えない場合は text = ""
entry1 = ttk.Entry(root,textvariable = var1)

# Entryを設置する
# side : 表示位置指定 top buttom left right
entry1.pack(side = "bottom")
Entryに入力された文字をボタンをクリックするとラベルに表示するサンプルコード
from tkinter import *
from tkinter import ttk

# ボタンがクリックされたら呼ばれる関数
def btn1_clicked():
    #label1にEntryに入力された文字を表示
    labelVar.set(entryVer.get())
    print("ボタンが押されました。")

root = Tk()

# ウィンドウの大きさ座標の設定
root.geometry( "400x300+500+100") # 幅x高さ+ウィンドウのx座標+ウィンドウのy座標

# ボタンを宣言する
#text : ボタンに表示されるテキスト
#commmand : ボタンがクリックされたときに呼び出す関数を指定
btn1 = ttk.Button(root,text="End", command = btn1_clicked)

#変数の宣言
# StringVar() 文字列
# IntVar() 整数
# DoubleVar() 少数
# BooleanVar() 真偽
labelVar = StringVar()
entryVer = StringVar()

# ラベルを宣言する
# テキストを書き換える場合は textvariable = ""
# テキストを書き換えない場合は text = ""
label1 = ttk.Label(root , textvariable = labelVar)

#変数に代入
labelVar.set("OK")

# Entryを宣言する
# テキストを書き換える場合は textvariable = ""
# テキストを書き換えない場合は text = ""
entry1 = ttk.Entry(root,textvariable = entryVer)

# side : 表示位置指定 top buttom left right
label1.pack(side = "bottom")

# ボタンを設置する
# side : 表示位置指定 top buttom left right
btn1.pack(side = "bottom")

# Entryを設置する
# side : 表示位置指定 top buttom left right
entry1.pack(side = "bottom")

root.mainloop()

text(複数行の文字列の入力)

text1 = Text(root)
text1.insert(INSERT,"あいうえお\n")

text1.pack(side = "top")

import tkinter