リスト : messagebox のサンプル import tkinter as tk import tkinter.messagebox as msg root = tk.Tk() n = tk.IntVar() n.set(0) func_table = (msg.showinfo, msg.showwarning, msg.showerror, msg.askquestion, msg.askokcancel, msg.askyesno, msg.askretrycancel) name_table = ('showinfo', 'showwarning', 'showerror', 'askquestion', 'askokcancel', 'askyesno', 'askretrycancel') def message_box(): func = func_table[n.get()] func(title = 'about', message = 'message box のサンプルです') for m, name in enumerate(name_table): tk.Radiobutton(root, text = name, value = m, variable = n).pack(anchor='w') tk.Button(root, text = "Open message box", command = message_box).pack() root.mainloop()
メインウィンドウの画像
メッセージボックス showinfo()の画像
メッセージボックス askretrycancel() の画像
リスト : キャンバスウィジェット import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 150, height = 150) c0.pack() root.mainloop()
空のキャンバス
create_line() | 直線(折れ線) |
create_oval() | 楕円 |
create_arc() | 円弧(楕円の円周の一部) |
create_rectangle() | 矩形 |
create_polygon() | 多角形 |
create_image() | イメージ |
create_bitmap() | ビットマップ |
create_text() | 文字列 |
create_window() | 任意のウィジェット |
リスト : 楕円の描画 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 150, height = 150) id = c0.create_oval(10, 10, 140, 140) # c0.itemconfigure(id, fill = 'red') c0.pack() root.mainloop()
楕円の描画
楕円の描画 (塗りつぶし)
リスト : 矩形の描画 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 150, height = 150) c0.create_rectangle(10, 10, 140, 140, fill = 'green', stipple = 'gray25') c0.pack() root.mainloop()
矩形の描画 (stipple なし)
矩形の描画 (stipple あり)
リスト : 直線の描画 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 150, height = 150) c0.create_line(10, 10, 140, 10, 10, 140, 140, 140) c0.pack() root.mainloop()
直線の描画
直線の色と太さを変更
直線の描画(smooth = True)
リスト : 多角形の描画 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 150, height = 150) c0.create_polygon(75, 10, 140, 70, 110, 140, 40, 140, 10, 70) c0.pack() root.mainloop()
多角形の描画
多角形の描画 (smooth = True)
リスト : 円弧の描画 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 400, height = 200) r = 0 for c in ['red', 'blue', 'green', 'pink', 'cyan', 'yellow', 'magenta', 'brown', 'orange', 'white', 'gray', 'black']: c0.create_arc(10, 10, 390, 190, start = r, extent = 30, fill = c) r += 30 c0.pack() root.mainloop()
円弧の描画
リスト : イメージの表示 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 400, height = 300) c0.pack() image_data = tk.PhotoImage(file = 'earth.gif') c0.create_image(200, 150, image = image_data) root.mainloop()
c0.create_image(x, y, オプション, ... ) c0.create_bitmap(x, y, オプション, ... )
イメージの描画
リスト : テキストの表示 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 150, height = 150) c0.create_text(75, 75, text = 'hello, world!', font = ('', 14)) c0.pack() root.mainloop()
c0.create_text(x, y, オプション, ...)
テキストの描画
リスト : ラベルウィジェットの表示 import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 150, height = 150) a0 = tk.Label(root, text = 'hello, world!', bg = 'green', font = ('', 14)) c0.create_window(75, 75, window = a0) c0.pack() root.mainloop()
c0.create_window(x, y, オプション, ...)
ラベルの描画
type(ID) | 図形の種別を返す |
bbox(ID, ...) | 指定した図形を囲む領域 (矩形) をリストにして返す |
coords(ID, x0, y0, ...) | 図形の座標の設定や問い合わせ |
delete(ID, ...) | 図形の削除 |
move(ID, dx, dt) | 図形の移動 |
tag_lower(ID1, ID2) | 重なり順を低くする |
tag_raise(ID1, ID2) | 重なり順を高くする |
tag_bind(ID, eventsequence, callback) | バインディングの設定 |
リスト : バインディングの設定 (1) import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 200, height = 150) c0.pack() id = c0.create_rectangle(10, 10, 20, 20, fill = 'brown') # 移動 def move_rect(event): x = event.x y = event.y c0.coords(id, x - 5, y - 5, x + 5, y + 5) # バインディング c0.tag_bind(id, '<Button1-Motion>', move_rect) root.mainloop()
初期状態
矩形をドラッグで移動する
リスト : バインディングの設定 (2) import tkinter as tk root = tk.Tk() c0 = tk.Canvas(root, width = 200, height = 150) c0.pack() c0.create_rectangle(10, 10, 20, 20, fill = 'brown', tags = 'brown') c0.create_rectangle(20, 10, 30, 20, fill = 'brown', tags = 'brown') c0.create_rectangle(30, 10, 40, 20, fill = 'brown', tags = 'brown') # 移動 def move_rect(event): x = event.x y = event.y c0.coords('current', x - 5, y - 5, x + 5, y + 5) # バインディング c0.tag_bind('brown', "<Button1-Motion>", move_rect) root.mainloop()
co.itemconfigure('brown', fill = 'green')
co.delete('brown')
初期状態
矩形をドラッグで移動する
sub_win = tk.Toplevel()
リスト : ウィンドウの生成 import tkinter as tk root = tk.Tk() root.option_add('*font', ('', 14)) # メッセージの表示 def message_window(): sub_win = tk.Toplevel() tk.Message(sub_win, text = 'message のサンプルプログラムです').pack() # メニューの設定 m = tk.Menu(root) root.configure(menu = m) m.add_command(label = 'About', under = 0, command = message_window) # ラベルの設定 tk.Label(root, text = u'メニュー About を選んでね').pack() root.mainloop()
メインウィンドウ
About をクリックしてサブウィンドウを表示
widget.winfo_geometry() | ウィジェットの位置を文字列 (幅x高さ+x+y) で返す |
widget.winfo_width() | ウィジェットの幅を返す |
widget.winfo_height() | ウィジェットの高さを返す |
widget.winfo_x() | 親ウィンドウ内での x 座標を返す |
widget.winfo_y() | 親ウィンドウ内での y 座標を返す |
widget.winfo_rootx() | ディスプレイ上での x 座標を返す |
widget.winfo_rooty() | ディスプレイ上での y 座標を返す |
widget.winfo_exists() | ウィジェットが存在するか |
window.withdraw() | ウィンドウを画面から取り除く |
window.deiconify() | ウィンドウを見える状態に戻す |
window.iconify() | ウィンドウをアイコン化する |
window.geometry(string) | ウィンドウを表示する位置を文字列で (幅x高さ+x+y) で指定する |
window.maxsize(幅, 高さ) | ウィンドウの最大値を指定 |
window.minsize(幅, 高さ) | ウィンドウの最小値を指定 |
window.title(タイトル名) | ウィンドウのタイトルを指定 |
リスト : ウィンドウの生成 (改良版) import tkinter as tk root = tk.Tk() root.title('Main') root.option_add('*font', ('', 14)) sub_win = None # メッセージの表示 def message_window(): global sub_win if sub_win is None or not sub_win.winfo_exists(): sub_win = tk.Toplevel() sub_win.title('About') tk.Message(sub_win, aspect = 200, text = 'message のサンプルプログラムです').pack() # メニューの設定 m = tk.Menu(root) root.configure(menu = m) m.add_command(label = 'About', under = 0, command = message_window) # ラベルの設定 tk.Label(root, text = u'メニュー About を選んでね').pack() root.mainloop()
メインウィンドウ (改良版)
サブウィンドウ (改良版)
normal | 通常の状態 |
active | アクティブな状態 |
disabled | 無効な状態 |
activeforeground | アクティブ時の色を指定 |
activebackground | アクティブ時の背景色を指定 |
disabledforeground | 無効時の色を指定 |
リスト : ボタンの状態を変更する import tkinter as tk root = tk.Tk() root.option_add('*font', ('', 14)) var = tk.StringVar() var.set('normal') # ボタン b = tk.Button(root, text = 'button', activeforeground = 'green', disabledforeground = 'red') b.pack(fill = tk.X) # 状態の変更 def change_state(): b.configure(state = var.get()) # ラジオボタンの設定 for x in ('normal', 'active', 'disabled'): tk.Radiobutton(root, text = x, value = x, variable = var, command = change_state).pack(anchor = tk.W) root.mainloop()
ボタンの状態を変更
entryconfigure(項目, option, value)
N | 数値で指定 (先頭の項目が 0 番目となる) |
@N | 画面上端から N ピクセルだけ下にある項目 |
end, last | 最後の項目 |
active | アクティブな状態にある項目 |
none | どれでもない項目 (全ての項目を非アクティブにするために使用する) |
パターン | パターンと一致するラベル名を持つ項目 |
リスト : メニューの状態を変更する import tkinter as tk root = tk.Tk() root.option_add('*font', ('', 14)) var = tk.StringVar() var.set('normal') def dummy(): pass # メニューの設定 m0 = tk.Menu(root) root.configure(menu = m0) m1 = tk.Menu(m0, tearoff = False) m0.add_cascade(label = 'Menu', under = 0, menu = m1) m1.add_command(label = 'Menu1', command = dummy) m1.add_command(label = 'Menu2', command = dummy) m1.add_command(label = 'Menu3', command = dummy) # 状態の変更 def change_state(): m1.entryconfigure('Menu1', state = var.get()) # ラジオボタンの設定 for x in ('normal', 'active', 'disabled'): tk.Radiobutton(root, text = x, value = x, variable = var, command = change_state).pack(anchor = tk.W) root.mainloop()
通常のメニュー
disabled に設定 (Menu1 が灰色)