-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
115 lines (109 loc) · 3.81 KB
/
test.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
# -*- mode:python;coding:utf-8 -*-
import random
import sys
import tkinter
import time
from typing import Any, List, Type
QA_INTERVAL = 300
DEBUG = False
def abort_process(event: tkinter.Event) -> None:
u'''強制終了
'''
sys.exit(1)
class QAFrame(tkinter.Frame):
u'''問題表示クラス'''
def __init__(self, window: tkinter.Tk,
*args: Any, **kwargs: Any) -> None:
tkinter.Frame.__init__(self, window, *args, **kwargs)
def result(self) -> bool:
return False
class AnsEntry(tkinter.Entry):
u'''回答入力
'''
def __init__(self, frame: tkinter.Frame,
status: tkinter.StringVar, trueAns: str) -> None:
tkinter.Entry.__init__(self, frame)
self.status = status
self.trueAns = trueAns
self.stringVar = tkinter.StringVar()
self.stringVar.trace('w', self._updated)
self.configure(textvariable=self.stringVar)
def _updated(self, *args: Any) -> None:
self.status.set("updated: {0}".format(args[0]))
if args[0] != self.stringVar._name:
return
s = self.stringVar.get()
if s == self.trueAns:
self._success()
else:
self._ng()
def _success(self) -> None:
self.status.set("OK")
def _ng(self) -> None:
self.status.set("NG")
def result(self) -> bool:
return str(self.status.get()) == "OK"
class QACalcAddFrame(QAFrame):
u'''簡単な足し算の問題を出す
'''
def __init__(self, window: tkinter.Tk,
*args: Any, **kwargs: Any) -> None:
QAFrame.__init__(self, window, *args, **kwargs)
self.pack()
a = random.randrange(0, 20)
b = random.randrange(0, 20)
qtext = '{0} + {1} ='.format(a, b)
status = tkinter.StringVar()
statusLabel = tkinter.Label(self, textvariable=status)
questionLabel = tkinter.Label(self, text=qtext)
self.ansEntry = AnsEntry(self, status, str(a + b))
statusLabel.grid(column=0, row=0,)
questionLabel.grid(column=1, row=0)
self.ansEntry.grid(column=2, row=0)
def result(self) -> bool:
u'''回答内容が正しいなら文字列 'OK' を返す。
'''
return self.ansEntry.result()
class SummaryFrame(tkinter.Frame):
u'''結果表示
'''
def __init__(self, window: tkinter.Tk,
qainstances: List[QAFrame], *args: Any, **kwargs: Any) -> None:
tkinter.Frame.__init__(self, window, *args, **kwargs)
self.window = window
self.qainstances = qainstances
self.after(1000, self._check)
self.status = tkinter.StringVar()
statusLabel = tkinter.Label(self, textvariable=self.status)
statusLabel.pack()
def _check(self) -> None:
ngcount = sum([(0 if qainstance.result() else 1)
for qainstance in self.qainstances])
if ngcount == 0:
self.status.set('おわり!!!'.format(ngcount))
self.after(1000, self.window.destroy)
else:
self.status.set('のこり {0} もん'.format(ngcount))
self.after(1000, self._check)
# 問題を3つ出す
QACLASSES: List[Type[QAFrame]] = [QACalcAddFrame,QACalcAddFrame,QACalcAddFrame]
def display_qa() -> None:
# メイン関数
window = tkinter.Tk()
time.sleep(1)
window.attributes('-fullscreen', True)
window.title("test window")
qainstances = []
for qaclass in QACLASSES:
qa = qaclass(window, pady=10, padx=10)
qainstances.append(qa)
summary = SummaryFrame(window, qainstances)
summary.pack()
# デバッグ用
if DEBUG:
button = tkinter.Button(window, text="exit")
button.pack()
button.bind("<ButtonPress>", abort_process)
window.mainloop()
if __name__ == '__main__':
display_qa()