Python Tk 2

Canvas

図形の表示には、Canvasウイジェットを使用します。

図形の描画メソッド

create_line()
直線、折れ線
create_oval()
楕円
create_arc()
円弧
create_rectangle()
矩形
create_polygon()
多角形
create_image()
イメージ
create_bitmap()
ビットマップ
create_text()
文字列
create_window()
任意のウィジェット

図形を操作するメソッド

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)
バインディングの設定

イベント

主なイベントの種類は、以下の通りです。

ButtonPress (Button)
マウスボタンが押されたとき
ButtonRelease
マウスボタンが離された
Motion
マウスボタンをドラッグしたとき
KeyPress, Key
キーボードが押されたとき
KeyRelease
キーボードが離されたとき

主なイベントの修飾子の種類は、以下の通りです。

Control
Controlキー
Shift
Shiftキー
Lock
Lockキー
Alt
Altキー
Button1 (B1)
マウス左ボタン
Button2 (B2)
マウス中ボタン
Button3 (B3)
マウス右ボタン
Double
ダブルクリック

空っぽのCanvas

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 200, height = 200)
c.pack()

root.mainloop()

スクリプト解説

c = Tkinter.Canvas(root, width = 200, height = 200)
幅 200、縦 200のCanvasを作成します。

楕円

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 200, height = 200)
c.create_oval(20, 20, 150, 150)
c.pack()

root.mainloop()

スクリプト解説

c.create_oval(20, 20, 150, 150)
(20, 20)から、(150, 150)の四角形に収まる楕円を描きます。

塗りつぶし楕円

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 200, height = 200)
c.create_oval(20, 20, 150, 150, fill = '#ff0000')
c.pack()

root.mainloop()

スクリプト解説

c.create_oval(20, 20, 150, 150, fill = '#ff0000')
作成した楕円の番号を、変数idに代入しておきます。
fillオプションを使用して色を塗りつぶします。
stipple = ビットマップ
内部を塗りつぶすときの模様になるビットマップ
outline =
枠の色
width =
枠の幅(デフォルト 1.0)

矩形

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 200, height = 200)
c.create_rectangle(20, 20, 150, 150)
c.pack()

root.mainloop()

スクリプト解説

c.create_rectangle(20, 20, 150, 150)
左上(20, 20)、右下(150, 150)の矩形を描きます。

直線・曲線

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 600, height = 200)
c.create_line(20, 20, 150, 50, 30, 150, 180, 180)
c.create_line(220, 20, 350, 50, 250, 150, 380, 180, width = 2.0, fill = '#00ff00')
c.create_line(420, 20, 550, 50, 450, 150, 580, 180, smooth = True)
c.pack()

root.mainloop()

スクリプト解説

c.create_line(20, 20, 150, 50, 50, 150, 180, 180)
点(20, 20)、(150, 50)、(50, 150)、(180, 180)を結んだ直線を描きます。
c.create_line(220, 20, 350, 50, 250, 150, 380, 180, width = 2.0, fill = '#00ff00')
点(220, 20)、(350, 50)、(50, 250)、(380, 180)を結んだ直線を、 線幅 2.0、緑色 #00ff00 で描きます。
c.create_line(420, 20, 550, 50, 450, 150, 580, 180, smooth = True)
点(420, 20)、(550, 50)、(50, 450)、(580, 180)をコントロールポイントにした曲線を描きます。

多角形

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 600, height = 200)
c.create_polygon(85, 20, 150, 80, 120, 150, 50, 150, 20, 80)
c.create_polygon(285, 20, 350, 80, 320, 150, 250, 150, 220, 80, fill = '#0000ff')
c.create_polygon(485, 20, 550, 80, 520, 150, 450, 150, 420, 80, smooth = True)
c.pack()

root.mainloop()

スクリプト解説

c.create_polygon(85, 20, 150, 80, 120, 150, 50, 150, 20, 80)
点(85, 20)、(150, 80)、(120, 150)、(50, 150)、(20, 80)を結んだ多角形を描きます。
塗りつぶしの色は、デフォールトで黒になります。
c.create_polygon(285, 20, 350, 80, 320, 150, 250, 150, 220, 80, fill = '#0000ff')
塗りつぶしの色を青 #0000ff にします。
c.create_polygon(485, 20, 550, 80, 520, 150, 450, 150, 420, 80, smooth = True)
各点をコントロールポイントにして曲線で結びます。

イメージ

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 200, height = 200)
c.pack()

im = Tkinter.PhotoImage(file = 'neko.gif')
c.create_image(100, 100, image = im)

root.mainloop()

スクリプト解説

im = PhotoImage(file = 'neko.gif')
neko.gif という画像ファイルを読み込みます。
読み込むことのできる画像フォーマットは gif と ppm です。
c.create_image(100, 100, image = im)
キャンバスの(100, 100)の位置に画像を表示します。

文字列

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 200, height = 200)
c.create_text(50, 50, text = 'hello, world!')
c.pack()

root.mainloop()

スクリプト解説

c.create_text(50, 50, text = 'hello, world!')
(50, 50)の位置から、textオプションでテキスト'hello, world!'を表示します。
text 以外のオプションは、以下の通りです。
anchor
座標とテキストの位置関係
font
文字のフォント
fill
文字の色
justify
center
中揃え
left
左揃え
right
右揃え
width
1 行の長さ

バインディング

バインディングを使用すると、図形をマウスで移動できるようになります。

#!/usr/bin/env python
import Tkinter
root = Tkinter.Tk()
c = Tkinter.Canvas(root, width = 200, height = 200)
c.pack()
id = c.create_oval(20, 20, 50, 50, fill = '#ff00ff')

def move_oval(event):
	x = event.x
	y = event.y
	c.coords(id, x - 15, y - 15, x + 15, y + 15)

c.tag_bind(id, '<Button1-Motion>', move_oval)

root.mainloop()

スクリプト解説

def move_oval(event):
イベントが発生したときに実行される関数です。
引数 event にイベントに関する情報が入っています。
x = event.x
マウスの位置のX座標を変数に代入します。
y = event.y
マウスの位置のY座標を変数に代入します。
c.coords(id, x - 15, y - 15, x + 15, y + 15)
idで表される図形の位置を変更します。
c.tag_bind(id, '<Button1-Motion>', move_oval)
idの図形で、マウスの左ボタンによってドラッグされたという、イベントが発生したとき(<Button1-Motion>)、関数 move_oval() を実行します。

練習問題

  1. 以下のようなウインドウを作成し、Create ボタンを押すと、 スライダで色を決めた円が Canvas の中央に作成されて、 マウスで移動できるようにしてください。

Prev | Next
index | home
abetmhr@gmail.com