This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice.py
159 lines (144 loc) · 4.95 KB
/
practice.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from tkinter import *
import tkinter.messagebox as MessageBox
from wframe import WFrame, StartPosition
from enum import Enum
from time import time
from fixer import Fixer, TypoList
## 줄의 보기 설정을 관리하는 열거형
class DisplayMode(Enum):
## 예문, 입력 모두 보기
default = 0
## 예문, 입력 겹쳐 보기
overlap = 1
## 입력만 보기
hidden = 2
## 타자 연습하는 WFrame
class Practice(WFrame):
## 타자 연습에 쓸 기사
# @var Article
article = None
## 현재 타자 중인 줄 색인
# @var int
line = int
LINES_PER_PAGE = 6
## 첫 타자 시각
#
# 타자 수 계산에 활용
# @see keyDown()
# @var time
typeStart = float
## 타자 수
#
# 타자 수 계산에 활용
# @see keyDown()
# @var int
typed = int
## 오타 저장을 위한 리스트
# @var TypoList
typos = None
## 줄의 보기 설정
# @var DisplayMode
displayMode = DisplayMode.default
## indicator
# @var string
@property
def indicator(self):
return '' if self.displayMode == DisplayMode.overlap else '_'
## 생성자
def __init__(self, master=None, article=None, **kw):
self.article = article
self.line = 0
self.typeStart = time()
self.typed = 0
self.typos = TypoList()
super().__init__(master, **kw)
def initializeWidget(self):
labelOffset = -10 if self.displayMode == DisplayMode.default else 0
textOffset = 10 if self.displayMode == DisplayMode.default else 0
self.labels = []
self.texts = []
for i in range(self.LINES_PER_PAGE):
label = Label(self)
label['justify'] = LEFT
label['font'] = 'Consolas'
label['padx'] = 0
label['pady'] = 0
if self.displayMode == DisplayMode.overlap:
label['fg'] = 'gray'
if not self.displayMode == DisplayMode.hidden:
label.place(x=40, y=30 + labelOffset + i * 60)
self.labels.append(label)
text = Label(self)
text['justify'] = LEFT
text['font'] = 'Consolas'
text['padx'] = 0
text['pady'] = 0
text.place(x=40, y=30 + textOffset + i * 60)
self.texts.append(text)
self.meter = Label(self)
self.meter['text'] = '현재 타속:'
self.meter.pack(side=BOTTOM, fill=X, pady=(0, 40))
self.text = '영어 타자 연습'
self.width = 640
self.height = 480
self.startPosition = StartPosition.centerParent
self.bind('<KeyPress>', self.keyDown)
def onLoad(self):
if self.line >= len(self.article.texts):
MessageBox.showerror(self.text, '타자 끝')
self.close()
return
line = self.line % self.LINES_PER_PAGE
# 페이지 넘기기
if line == 0:
contexts = self.article.texts[self.line:][:self.LINES_PER_PAGE]
for i in range(self.LINES_PER_PAGE):
self.labels[i]['text'] = contexts[i] if i < len(contexts) else ''
self.texts[i]['text'] = ''
# 타자 위치 안내자
self.texts[line]['text'] = self.indicator
## 키 입력 처리
# @see http://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm
def keyDown(self, e):
try:
ch = chr(e.keysym_num)
context = self.article.texts[self.line]
text = self.texts[self.line % self.LINES_PER_PAGE]
line = text['text'][:len(text['text']) - len(self.indicator)]
# default behavior
if e.keysym_num <= 0x7F and ch != '\0':
# line incompleted
expected = len(line)
if expected < len(context):
text['text'] = line + ch + self.indicator
# 정타 처리
if ch == context[expected]:
self.typed += 1
# 오타 처리
else:
self.typos.append(context, expected, ch)
# line completed
else:
self.endLine()
# erase
elif e.keysym == 'BackSpace' and len(line):
text['text'] = line[:-1] + self.indicator
# penalty
# self.typed -= 3
# change line
elif e.keysym == 'Return':
self.endLine()
except ValueError:
pass
# speed = typed / duration * 60
self.meter['text'] = '현재 타속: %d' % int(self.typed / (time() - self.typeStart) * 60)
## 줄 넘기기
def endLine(self):
text = self.texts[self.line % self.LINES_PER_PAGE]
line = text['text'][:len(text['text']) - len(self.indicator)]
text['text'] = line
self.typed += 1
self.line += 1
self.onLoad()
def onClosing(self):
Fixer.save(self.typos)