Python Tk 1

Tkinter

Pythonに標準でついているGUIで、 Tcl/TkをPythonに移植したものがTkinterです。

その他のGUI toolkit

これらは、Pythonとは別にインストール必要です。

wxPython
Python用のGUI
PyGtk
GtkのフレームワークのPython言語バインディング
PyQt
QtのフレームワークのPython言語バインディング

ウィジェット

ウインドウ上に配置する部品のことを、ウィジェットと言います。
ウィジェットは、以下の形式でインスタンスを作成します。

変数 = ウィジェット名(親ウインドウまたは親ウィジェット, [オプション])

ウィジェット名一覧

Frame
ウィジェットを配置する枠
Label
文字列やイメージの表示
Message
複数行の文字列を表示
Button
ボタン
Radiobutton
ラジオボタン
Checkbutton
チェックボタン
Listbox
リストボックス
Scrollbar
スクロールバー
Scale
スケール
MELのスライダです。
Entry
1 行文字列の入力と編集
Menu
メニュー
Menubutton
メニューボタン
Bitmap
ビットマップ
Canvas
キャンバス
Text
テキスト入力と編集
LabelFrame
ラベル付きフレーム
Spinbox
スピンボックス
PanedWindow
ペインウィンドウ

よく使われるオプション一覧

foreground(fg)
文字や線を描くのに使用する色
background(bg)
背景色
text
ウィジェット内に表示されるテキスト
textvariable
テキストを格納するオブジェクト
image
ウィジェット内に表示されるイメージ
bitmap
ウィジェット内に表示されるビットマップ
borderwidth(bd)
ウィジェットの枠の幅
relief
ウィジェットの枠のスタイル
height
ウィジェットの高さ
width
ウィジェットの幅
anchor
ウィジェットや表示されるデータの位置

Tkinterで、色を指定するには以下の形式を使用します。
各R,G,Bの記号は4bitの16進数を表します。

例えば、#ffff00は赤255、緑255、青0で黄色になります。

ジオメトリマネージャ

ウィジェットをウインドウ上に配置するには、以下のメソッドを使用します。

place()
指定した座標に配置します。
pack()
縦または横方向に向かって一列に並べます。
grid()
格子状に配置します。

このうち、pack()grid()は、ウィジェットの数・大きさによってウィンドウの大きさが変わります。

Hello World

ラベルのついたウインドウを作成します。

#!/usr/bin/env python
import Tkinter

label = Tkinter.Label(None, text='Hello World!')
label.pack()
label.mainloop()

スクリプト解説

#!/usr/bin/env python
以下のコードを、Phthonスクリプトとして実行します。
import Tkinter
Tkinterモジュールをインポートします。
label = Tkinter.Label(None, text='Hello World!')
'Hello World'という文字列を表示したラベルのインスタンスを作成し、変数labelに代入します。
第一引数がNoneなので、親ウィジェットはありません。
label.pack()
pack()によって、ラベルをウインドウ並べます。
label.mainloop()
イベントループを開始します。

何もしないボタン

ボタンを表示するだけのウインドウを作成します。

#!/usr/bin/env python
import Tkinter

root = Tkinter.Tk()
button = Tkinter.Button(root, text='button1')
button.pack()
root.mainloop()

スクリプト解説

root = Tkinter.Tk()
Tkオブジェクトのインスタンスを作成します。
これがメインウインドウになります。
button = Tkinter.Button(root, text='button1')
メインウインドウを親にして、ボタンのインスタンスを作成します。

閉じるボタン

押すとウインドウを閉じるボタンを作成します。

#!/usr/bin/env python
import sys
import Tkinter

root = Tkinter.Tk()
button = Tkinter.Button(root, text='Exit', command=sys.exit)
button.pack()
root.mainloop()

スクリプト解説

button = Tkinter.Button(root, text='Exit', command=sys.exit)
sys.exit()メソッドを実行するボタンを作成します。

関数を実行するボタン

任意の関数を実行するボタンを作成します。

#!/usr/bin/env python
import sys
import Tkinter

def func1():
	label.configure(text='button1 pressed')

def func2():
	label.configure(text='button2 pressed')

root = Tkinter.Tk()
label = Tkinter.Label(root, text='press button')
label.pack()
button1 = Tkinter.Button(root, text='button1', command=func1)
button1.pack()
button2 = Tkinter.Button(root, text='button2', command=func2)
button2.pack()
button3 = Tkinter.Button(root, text='Exit', command=sys.exit)
button3.pack()
root.mainloop()

スクリプト解説

label.configure(text='button1 pressed')
ラベルのテキストを'button1 pressed'に書き換えます。
button1 = Tkinter.Button(root, text='button1', command=func1)
関数func1を実行するボタンを作成します。

ラベルの色を変えるスライダ 1

スライダによって、ラベルの色が変化するウインドウを作成します。
スライダは、0から255の間で1づつ変化します。

#!/usr/bin/env python
import sys
import Tkinter

def color_red(n):
	label.configure(bg='#%02x0000' % scale1.get())

root = Tkinter.Tk()
label = Tkinter.Label(root, text='press button', fg='#ffffff')
label.pack()
scale1 = Tkinter.Scale(root, label='label color', orient='h', from_=0, to=255, command=color_red)
scale1.pack()
button1 = Tkinter.Button(root, text='Exit', command=sys.exit)
button1.pack()
root.mainloop()

スクリプト解説

label.configure(bg='#%02x0000' % scale1.get())
スケールscale1の現在の値を得て、ラベルのバックの色に設定します。
%02xは、2桁の16進数という意味です。
例えば、スケールの数値が255なら色は#ff0000になります。
label = Tkinter.Label(root, text='press button', fg='#ffffff')
文字の色が#ffffffのラベルを作成します。
scale1 = Tkinter.Scale(root, label='label color', orient='h', from_=0, to=255, command=color_red)
スケールを作成します。
orient='h'
水平方向に配置します。
'v'で垂直向になります。
from_=0
最小値は0です。
to=255
最大値は255です。
command=color_red
スケールを移動したときに実行する関数は、color_red()です。

ラベルの色を変えるスライダ 2

同様にスライダによって、ラベルの色が変化するウインドウを作成します。
スライダは、0.0から1.0の間で0.1づつ変化します。

#!/usr/bin/env python
import sys
import Tkinter

def color_red(n):
	label.configure(bg='#%02x0000' % (int)(scale1.get()*255))

root = Tkinter.Tk()
label = Tkinter.Label(root, text='press button', fg='#ffffff')
label.pack()
scale1 = Tkinter.Scale(root, label='label color', orient='h', from_=0.0, to=1.0, resolution=0.1, command=color_red)
scale1.pack()
button1 = Tkinter.Button(root, text='Exit', command=sys.exit)
button1.pack()
root.mainloop()

スクリプト解説

scale1 = Tkinter.Scale(root, label='label color', orient='h', from_=0.0, to=1.0, resolution=0.1, command=color_red)
スケールを作成します。
from_=0.0
最小値は0.0です。
to=1.0
最大値は1.0です。
resolution=0.1
スケールの移動単位は0.1です。

練習問題

  1. 上のslider2.pyを参考にして、以下のようなウインドウを作成し、ラベルの色を赤・緑・青で変更できるようにしてみましょう。

Prev | Next
index | home
abetmhr@gmail.com