-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
71 lines (59 loc) · 2.1 KB
/
calculator.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Author: KANCHI TANK
"""
import tkinter as tk
from PIL import ImageGrab
import sys
sys.path.insert(0, sys.path[0]+'\\src')
import model
CANVAS_WIDTH = 1200
CANVAS_HEIGHT = 200
LINE_WIDTH = 20
class HandwritingCalculator(object):
"""Create gui apps for handwriting calculator using tkinter."""
def __init__(self):
"""Init for creating gui structure."""
self.translator = model.HandwritingTranslator()
self.root = tk.Tk()
self.root.title('Handwritten Calculator')
self.c = tk.Canvas(self.root, bg='white', width=CANVAS_WIDTH, height=CANVAS_HEIGHT)
self.c.grid(row=0, column=0)
self.v_calculation = tk.StringVar()
f1 = tk.Frame(self.root, width=CANVAS_WIDTH)
f1.grid(row=1, column=0, sticky='w')
self.label = tk.Label(f1, textvariable=self.v_calculation, font=('Courier', 30))
self.label.grid(row=0, column=0)
self.v_calculation.set('Right-click for clear')
self.setup()
def setup(self):
"""Setup initial position and binding to mouse movement."""
self.x_pos = None
self.y_pos = None
self.c.bind('<B1-Motion>', self.paint)
self.c.bind('<ButtonRelease-1>', self.finish_draw)
self.c.bind('<ButtonRelease-3>', self.reset)
self.reset(None)
self.root.mainloop()
def paint(self, event):
"""Run when user writing in canvas."""
if self.x_pos and self.y_pos:
self.c.create_line(self.x_pos, self.y_pos, event.x, event.y, width=LINE_WIDTH, smooth=tk.TRUE, capstyle=tk.ROUND)
self.x_pos = event.x
self.y_pos = event.y
def reset(self, event):
"""Delete all."""
self.c.delete('all')
self.v_calculation.set('Right-click for clear')
def finish_draw(self, event):
"""Run when user done writing single number/symbol."""
self.x_pos, self.y_pos = None, None
x1 = self.c.winfo_rootx() + self.c.winfo_x()
y1 = self.c.winfo_rooty() + self.c.winfo_y()
x2 = x1 + CANVAS_WIDTH
y2 = y1 + CANVAS_HEIGHT
image = ImageGrab.grab((x1,y1,x2,y2)).convert('L')
# image.save('tes.jpg')
calculation = self.translator.translate(image)
self.v_calculation.set(calculation)
if __name__ == '__main__':
HandwritingCalculator()