-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkivy_interface.py
More file actions
315 lines (238 loc) · 11.4 KB
/
Copy pathkivy_interface.py
File metadata and controls
315 lines (238 loc) · 11.4 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from kivy.clock import Clock
from kivy.uix.popup import Popup
from kivy.uix.dropdown import DropDown
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.app import App
from fast_help import form_speed, most_miss_buttons
Window.size = (1200, 600)
Window.minimum_width = 1200
Window.minimum_height = 600
Window.title = "Клавиатурный тренажер"
#color = kivy.utils.get_hex_from_color("#6f86c6")
Window.clearcolor = (0.2, 0.5, 0.7, 1)
COLOR_BUTTON = (0.2, 0.5, 0.7, 1)
APP_NAME = "Клавиатурный тренажер"
class KeyboardTrainApp(App):
def __init__(self, kyeTrain):
super().__init__()
self.kyeTrain = kyeTrain
self.MainLayout = FloatLayout()
self.textInput = TextInput()
self.TextLabel = Label(text='',
markup=True,
color=(1, 1, 1, 1),
font_size=30)
self.TextInputWidget = TextInput(hint_text=" ", font_size=30)
def build(self):
self.make_menu()
LabelWidget = Label(text=APP_NAME,
pos_hint={'top': 1.3,
'right': 1.3},
font_size=30,
color=(1, 1, 1, 1))
self.MainLayout.add_widget(LabelWidget)
Window.bind(on_request_close=self.on_request_close)
button = Button(text='Закрыть окно')
button.bind(on_press=self.close_window)
return self.MainLayout
def char_for_input(self, KeyboardInput, text):
self.MainLayout.clear_widgets()
self.TextLabel.text = text
self.MainLayout.add_widget(self.TextLabel)
mistakes_label = Label(text='Ошибки: 0',
font_size=30,
size_hint=(0.1, 0.1),
pos_hint={'top': 0.9, 'center_x': 0.5})
self.MainLayout.add_widget(mistakes_label)
speed_label = Label(text='Скорость: 0 символов',
font_size=30,
size_hint=(0.1, 0.1),
pos_hint={'top': 1, 'center_x': 0.5})
self.MainLayout.add_widget(speed_label)
menu = BoxLayout(spacing=3,
orientation='vertical',
size_hint=(0.15, 0.05),
pos_hint={'bottom': 0.1, 'center_x': 0.5})
self.MainLayout.add_widget(menu)
menu.add_widget(Button(text='Выход в меню',
font_size=20,
background_color=(0.2, 0.5, 0.7, 1),
on_press=self.kyeTrain.interupt))
self.MainLayout.add_widget(KeyboardInput.listener)
def update_mistakes_label(dt):
mistakes_label.text = f'Ошибки: {KeyboardInput.get_current_mistakes()}'
Clock.schedule_interval(update_mistakes_label, 0.1)
def update_speed_label(dt):
speed_label.text = f'Скорость: {KeyboardInput.get_current_typing_speed():.2f} печати в секунду'
Clock.schedule_interval(update_speed_label, 0.1)
def close_window(self, instance):
Window.close()
def on_stop(self):
self.show_confirmation_popup()
def on_request_close(self, *args):
self.show_confirmation_popup()
return True
def show_confirmation_popup(self):
popup = Popup(title='Подтверждение выхода',
content=BoxLayout(orientation='vertical', padding=10, spacing=10),
background_color=(1.9, 2.7, 3, 1),
size_hint=(None, None), size=(300, 150))
popup.content.add_widget(Label(text='Вы уверены, что хотите выйти?'))
button_layout = BoxLayout(spacing=10)
yes_button = Button(text='Да', on_press=self.stop_app)
no_button = Button(text='Нет', on_press=popup.dismiss)
button_layout.add_widget(yes_button)
button_layout.add_widget(no_button)
popup.content.add_widget(button_layout)
popup.open()
def stop_app(self, instance):
App.get_running_app().stop()
def load_text_from_lesson(self, fileName):
file = open(fileName, 'r', encoding='utf-8')
textFile = file.read()
file.close()
t = textFile
self.insert_text(t)
def change_char(self, index, text):
self.TextLabel.text = '[color=7f91d1]' + text[:index] + '[/color]' + text[index:]
def make_menu(self):
menu = BoxLayout(spacing=3,
orientation='vertical',
size_hint=(.5, .5),
pos_hint={'top': 0.6, 'right': 0.97})
self.MainLayout.add_widget(menu)
start1 = BoxLayout(spacing=3, orientation='horizontal')
start1.add_widget(Button(text='Начать',
font_size=30,
background_color=COLOR_BUTTON,
on_press=self.kyeTrain.new_input))
# start1.add_widget(Button(text='Добавить файл',
# font_size=25,
# background_color=COLOR_BUTTON,
# on_press=self.kyeTrain.load_text_for_input))
menu.add_widget(start1)
menu.add_widget(Button(text='Сбросить статистику',
font_size=30,
background_color=COLOR_BUTTON,
on_press=self.kyeTrain.reset))
menu.add_widget(Button(text='Выход',
background_color=COLOR_BUTTON,
font_size=30,
on_press=lambda instanse: self.on_stop()))
#______________________________________________________________________________________________________________________
menu2 = BoxLayout(spacing=3,
orientation='horizontal',
size_hint=(0.45, 0.25),
pos_hint={'top': 0.95, 'left': 0.5})
self.MainLayout.add_widget(menu2)
selected_lesson_ru = "Выбрать урок"
selected_lesson_en = "Choose a lesson"
dropdown1 = DropDown()
def reset_dropdown1(instance):
main_button_ru.text = selected_lesson_ru
dropdown1.dismiss()
def create_button_action1(lesson_number):
def button_action(instance):
file_name = lesson_file_mapping.get(lesson_number)
if file_name:
self.load_text_from_lesson(file_name)
reset_dropdown2(instance)
return button_action
lesson_file_mapping = {
1: "files/1 ваол.txt",
2: "files/2 фыдж.txt",
3: "files/3 мить.txt",
4: "files/4 епнр.txt",
5: "files/5 укгш.txt",
6: "files/6 чсбю.txt",
7: "files/7 йцщз.txt",
8: "files/8 яэхъ.txt",
9: "files/слова а-я.txt",
10: "files/короткие слова.txt",
11: "files/длинные слова.txt",
12: "files/слова с большой буквы.txt",
13: "files/5678.txt",
14: "files/3490.txt",
15: "files/12-=.txt",
}
for i in range(1, 16):
btn1 = Button(text=f"урок {i}", size_hint_y=None, height=44, background_color=(0.2, 0.5, 0.7, 1))
btn1.bind(on_release=lambda btn1, i=i: dropdown1.select(btn1.text))
btn1.bind(on_press=create_button_action1(i))
dropdown1.add_widget(btn1)
main_button_ru = Button(text=selected_lesson_ru, font_size=25, background_color=COLOR_BUTTON)
main_button_ru.bind(on_release=dropdown1.open)
dropdown1.bind(on_select=lambda instance, x: setattr(main_button_ru, 'text', x))
menu2.add_widget(main_button_ru)
dropdown2 = DropDown()
def reset_dropdown2(instance):
main_button_en.text = selected_lesson_en
dropdown2.dismiss()
def create_button_action2(lesson_number):
def button_action(instance):
file_name = lesson_file_mapping_en.get(lesson_number)
if file_name:
self.load_text_from_lesson(file_name)
reset_dropdown1(instance)
return button_action
lesson_file_mapping_en = {
1: "files_en/1 dfjk.txt",
2: "files_en/2 asl;.txt",
3: "files_en/3 vbnm.txt",
4: "files_en/4 tghy.txt",
5: "files_en/5 erui.txt",
6: "files_en/6 qwop.txt",
7: "files_en/7 cx.,.txt",
8: "files_en/англ коротко.txt",
9: "files_en/англ к2.txt",
10: "files_en/англ длинные.txt",
11: "files_en/англ верхний.txt",
12: "files/5678.txt",
13: "files/3490.txt",
14: "files/12-=.txt"
}
for i in range(1, 15):
btn2 = Button(text=f"lesson {i}", size_hint_y=None, height=44, background_color=(0.2, 0.5, 0.7, 1))
btn2.bind(on_release=lambda btn2, i=i: dropdown2.select(btn2.text))
btn2.bind(on_press=create_button_action2(i))
dropdown2.add_widget(btn2)
main_button_en = Button(text=selected_lesson_en, font_size=25, background_color=COLOR_BUTTON)
main_button_en.bind(on_release=dropdown2.open)
dropdown2.bind(on_select=lambda instance, x: setattr(main_button_en, 'text', x))
menu2.add_widget(main_button_en)
def end_menu(self, speed, mistakes, averageSpeed):
self.MainLayout.clear_widgets()
self.make_menu()
newText = "Скорость печати в минуту: " + form_speed(speed) + "\nСкорость печати в секунду: " + form_speed(speed/60)
newText += '\nСредняя скорость печати: ' + form_speed(averageSpeed)
newText += '\nОшибки: ' + str(mistakes)
newText += '\nСамые частые ошибки: ' + most_miss_buttons()
LabelWidget = Label(text=newText,
pos_hint={'top': 1.3,
'right': 1.2},
font_size=30,
color=(1, 1, 1, 1))
self.MainLayout.add_widget(LabelWidget)
def insert_text(self, text):
self.TextInputWidget.text = text
class Keyboard(Widget):
def __init__(self, triggerFunc):
super().__init__()
self.triggerFunc = triggerFunc
self._keyboard = Window.request_keyboard(
self.keyboard_closed, self, 'text')
self._keyboard.bind(on_key_down=self.on_keyboard_down)
def keyboard_closed(self):
self._keyboard.unbind(on_key_down=self.on_keyboard_down)
self._keyboard = None
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
needToRelease = self.triggerFunc(keycode, text, modifiers)
if needToRelease:
keyboard.release()
return True