リスト : Packer のリサイズ import tkinter as tk root = tk.Tk() tk.Button(root, text = 'button 0').pack(expand = True, fill = 'both') tk.Button(root, text = 'button 1').pack(expand = True, fill = 'both') root.mainloop()
Packer によるボタンの配置
ウィンドウを拡大する
window.grid_columnconfigure(column_index, options) window.grid_rowconfigure(row_index, options)
| minsize | 最小の幅/高さを数値で指定する |
| weight | 余白を配分するときの割合を数値で指定する |
| pad | 詰め物を数値で指定する |
リスト : Gridder のリサイズ
import tkinter as tk
root = tk.Tk()
column_data = (0, 0, 1, 1)
row_data = (0, 1, 0, 1)
for x in range(4):
b = tk.Button(root, text = f'button {x}')
b.grid(column = column_data[x], row = row_data[x], sticky = 'nsew')
root.mainloop()
Gridder によるボタンの配置
root.grid_columnconfigure(0, weight = 1)
button 0, 1 は横方向に伸びる
root.grid_columnconfigure(1, weight = 2)
4 つのボタンが横方向に伸びる
root.grid_rowconfigure(0, weight = 1) root.grid_rowconfigure(1, weight = 2)
4 つのボタンが縦横方向に伸びる
リスト : キャンバスウィジェットのリサイズ (1) import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, bg = 'green', width = 200, height = 200) c0.create_rectangle(20, 20, 180, 180, fill = 'red') c0.pack(fill = tk.BOTH, expand = True) root.mainloop()
キャンバスウィジェットを配置
ウィンドウを縮小
ウィンドウを拡大
root.bind('<Configure>', change_size)
リスト : キャンバスウィジェットのリサイズ (2)
import tkinter as tk
# メインウィンドウの設定
root = tk.Tk()
root.minsize(100, 100)
root.maxsize(400, 400)
# キャンバスの設定
c0 = tk.Canvas(root, bg = 'darkgreen', width = 200, height = 200)
id = c0.create_rectangle(20, 20, 180, 180, fill = 'red')
c0.pack(fill = tk.BOTH, expand = True)
# 図形の大きさを変更
def change_size(event):
w = c0.winfo_width()
h = c0.winfo_height()
c0.coords(id, 20, 20, w - 20, h - 20)
# バインディングの設定
root.bind('<Configure>', change_size)
root.mainloop()
ウィンドウを縮小
ウィンドウを拡大
リスト : フレームの表示
import tkinter as tk
root = tk.Tk()
for r in ('raised', 'sunken', 'flat', 'groove', 'ridge'):
f = tk.Frame(root, width = 60, height = 40,
relief = r, borderwidth = 4, bg = 'lightgray')
f.pack(padx = 5, pady = 5, side= 'left')
root.mainloop()
フレームの形状
リスト : フレームにボタンを配置する import tkinter as tk root = tk.Tk() # フレームの生成 f0 = tk.Frame(root) f1 = tk.Frame(root) # f0 にボタンを配置する tk.Button(f0, text = 'button 00').pack(side = tk.LEFT) tk.Button(f0, text = 'button 01').pack(side = tk.LEFT) tk.Button(f0, text = 'button 02').pack(side = tk.LEFT) # f1 にボタンを配置する tk.Button(root, text = 'button 10').pack(in_ = f1, fill = tk.BOTH) tk.Button(root, text = 'button 11').pack(in_ = f1, fill = tk.BOTH) tk.Button(root, text = 'button 12').pack(in_ = f1, fill = tk.BOTH) # フレームの配置 f0.pack() f1.pack(fill = tk.BOTH) root.mainloop()
フレームを使ってボタンを配置する
widget.raise(widget1) widget.lower(widget1)
リスト : フレームの下にボタンを隠す
import tkinter as tk
root = tk.Tk()
buttons = []
# ボタンを隠す
def hide(n):
return lambda : buttons[n].lower()
# フレーム
f = tk.Frame(root)
# ボタン
a = tk.Button(root, text = 'Show', command = lambda : f.lower())
a.pack(in_ = f, fill = tk.X)
for x in range(4):
b = tk.Button(text = 'button {}'.format(x), command = hide(x))
b.pack(in_ = f, fill = tk.X)
buttons.append(b)
f.pack(fill = tk.X)
root.mainloop()
button 1 と 3 を押した状態
Show を押した状態
リスト : ラベルフレーム (1)
import tkinter as tk
root = tk.Tk()
for r in (tk.NW, tk.N, tk.NE, tk.SW, tk.S, tk.SE):
f = tk.LabelFrame(root, text = 'label',
width = 80, height = 60, labelanchor = r)
f.pack(padx = 5, pady = 5, side = tk.LEFT)
root.mainloop()
ラベルフレーム(1)
リスト : ラベルフレーム (2)
import tkinter as tk
root = tk.Tk()
for r in ('raised', 'sunken', 'flat', 'groove', 'ridge'):
f = tk.LabelFrame(root, text = r, width = 80, height = 60, relief = r)
f.pack(padx = 5, pady = 5, side= tk.LEFT)
root.mainloop()
ラベルフレーム(2)
リスト : ラベルフレーム (3)
import tkinter as tk
root = tk.Tk()
v = tk.IntVar()
v.set(0)
f0 = tk.LabelFrame(root, text = 'Group1')
f1 = tk.LabelFrame(root, text = 'Group2')
for x in (0, 1, 2):
tk.Radiobutton(f0, text = 'radiobutton {}'.format(x), value = x, variable = v).pack()
tk.Checkbutton(f1, text = 'checkbutton {}'.format(x)).pack()
f0.pack(padx = 5, pady = 5, side = tk.LEFT)
f1.pack(padx = 5, pady = 5, side = tk.LEFT)
root.mainloop()
ラベルフレーム(3)
リスト : ラベルフレーム (4)
import tkinter as tk
root = tk.Tk()
root.option_add('*font', ('', 14))
# 値を格納するオブジェクト
flag = tk.BooleanVar()
flag.set(False)
v = tk.IntVar()
v.set(0)
# ラジオボタンを格納
buttons = []
# ボタンの状態を変更
def change_state():
if flag.get():
new_state = 'normal'
else:
new_state = 'disabled'
for b in buttons:
b.configure(state = new_state)
# チェックボタン
cb = tk.Checkbutton(root, text = 'use button', variable = flag, command = change_state)
# ラベルフレーム
f = tk.LabelFrame(root, labelwidget = cb)
# ラジオボタン
for x in (0, 1, 2):
b = tk.Radiobutton(f, text = 'radiobutton {}'.format(x),
value = x, variable = v, state = 'disabled')
b.pack()
buttons.append(b)
# フレームの配置
f.pack(padx = 5, pady = 5)
root.mainloop()
disabled
normal
リスト:ペインドウィンドウ (1)
import tkinter as tk
root = tk.Tk()
root.option_add('*font', ('', 14))
# ペインドウィンドウの生成
pw = tk.PanedWindow(root, sashwidth = 4)
pw.pack(expand = True, fill = tk.BOTH)
# ラベルの生成
a = tk.Label(pw, text = 'panedwindow\ntest1', bg = 'white')
b = tk.Label(pw, text = 'panedwindow\ntest2', bg = 'yellow')
# ペインドウィンドウに追加
pw.add(a)
pw.add(b)
root.mainloop()
1.
2.
3.
リスト : ペインドウィンドウ (2)
import tkinter as tk
root = tk.Tk()
root.option_add('*font', ('', 14))
# ペインドウィンドウの生成
pw = tk.PanedWindow(root, orient = 'vertical', sashwidth = 4)
pw.pack(expand = True, fill = tk.BOTH)
# ラベルの生成
a = tk.Label(pw, text = 'panedwindow\ntest1', bg = 'white')
b = tk.Label(pw, text = 'panedwindow\ntest2', bg = 'yellow')
c = tk.Label(pw, text = 'panedwindow\ntest3', bg = 'cyan')
# ペインドウィンドウに配置
pw.add(a)
pw.add(b)
pw.add(c)
root.mainloop()
1.
2.
3.
リスト : ペインドウィンドウ (3)
import tkinter as tk
root = tk.Tk()
root.option_add('*font', ('', 14))
# グローバル変数
la = []
var = []
# ペインドウィンドウ
pw = tk.PanedWindow(root, orient = 'vertical', showhandle = True, sashwidth = 4)
pw.pack(expand = True, fill = tk.BOTH)
# フレーム
f = tk.Frame(pw)
pw.add(f)
# ラベルの表示切り替え
def change_label(n):
def _change():
if var[n].get():
pw.add(la[n])
else:
pw.forget(la[n])
return _change
# チェックボタン
for x in range(4):
v = tk.BooleanVar()
v.set(True)
var.append(v)
tk.Checkbutton(f, text = 'display label {}'.format(x),
variable = v, command = change_label(x)).pack()
# ラベル
for x, y in enumerate(('white', 'yellow', 'cyan', 'pink')):
a = tk.Label(pw, text = 'panedwindow\ntest{}'.format(x), bg = y)
la.append(a)
pw.add(a)
root.mainloop()
1.
2.
3.
リスト:ペインドウィンドウ (4)
import tkinter as tk
root = tk.Tk()
root.option_add('*font', ('', 14))
# ペインドウィンドウ 1
pw1 = tk.PanedWindow(root)
pw1.pack(expand = True, fill = tk.BOTH)
# ラベル 1
a = tk.Label(pw1, text = 'panedwindow\ntest1', bg = 'yellow')
# ペインドウィンドウ 2
pw2 = tk.PanedWindow(pw1, orient = 'vertical')
# ラベル 2, 3
b = tk.Label(pw2, text = 'panedwindow\ntest2', bg = 'cyan')
c = tk.Label(pw2, text = 'panedwindow\ntest3', bg = 'pink')
# ペインドウィンドウに配置
pw1.add(a)
pw1.add(pw2)
pw2.add(b)
pw2.add(c)
root.mainloop()
1.
2.
3.