-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputCanvas.py
44 lines (36 loc) · 1.45 KB
/
InputCanvas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding:utf-8 -*-
from PIL import Image
import tkinter
import io
class InputCanvas(tkinter.Canvas):
def on_pressed(self, event):
# クリックされたとき
self.sx, self.sy = event.x, event.y
w = self.linewidth / 2
self.create_oval(self.sx-w, self.sy-w, self.sx+w, self.sy+w,
fill = self.linecolor,
width = 0)
def on_dragged(self, event):
# ドラッグ中
ex, ey = event.x, event.y
self.create_line(self.sx, self.sy, ex, ey,
fill = self.linecolor,
width = self.linewidth)
w = self.linewidth / 2
self.create_oval(ex-w, ey-w, ex+w, ey+w,
fill = self.linecolor,
width = 0)
self.sx, self.sy = event.x, event.y
def getImage(self):
# キャンバスを画像化する
ps = self.postscript(colormode='color')
return Image.open(io.BytesIO(ps.encode('utf-8')))
def clear(self):
# キャンバスを初期化する
self.delete('all')
def __init__(self, window, width, height, bg = "white"):
super(InputCanvas, self).__init__(window, bg = bg, width = width, height = height)
self.bind("<ButtonPress-1>", self.on_pressed)
self.bind("<B1-Motion>", self.on_dragged)
self.linecolor = "black"
self.linewidth = 22