Repository navigation
Expand file tree
/
Copy pathaxis.py
More file actions
499 lines (364 loc) · 15.8 KB
/
Copy pathaxis.py
File metadata and controls
499 lines (364 loc) · 15.8 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import tkinter
import random
import math
from draw_line import draw_lines
marks = dict()
used = []
user_enter = dict()
delta = []
task = 0
clas = 0
counter = 1
a, b, c = 0, 0, 0
start = True
task_for_8, task_for_9, task_for_10, task_for_11 = [], [], [], []
# наборы функций
f8_1 = lambda x: x/b + d
f8_2 = lambda x: x**2 + d
f8_3 = lambda x: b/x + d
f9_1 = lambda x: (a*x**2+b*x+c) / d
f9_2 = lambda x: a*x**3+b*x**2+c*x+d
f9_3 = lambda x: (x**(1/2))*b+d
f9_4 = lambda x: math.sin(x) *a
f9_5 = lambda x: math.cos(x) *a
f10_1 = lambda x: math.log(x, 2)
f10_2 = lambda x: math.log(x, 1/2)
f10_3 = lambda x: math.atan(x)
f11_1 = lambda x: math.exp(x) * a
f11_2 = lambda x: abs(math.log(x, 2))
def generate_coificients():
global a, b, c, d, tasks_for_8, tasks_for_9, tasks_for_10, tasks_for_11
a = random.choice([-2, 2])
b = random.choice([2, 3, 4])
c = random.choice([2, 3, 4])
d = random.choice([1, 2, 3])
tasks_for_8 = [(f8_1, "x:"+str(b) +"+"+ str(d)), (f8_2, "x"+chr(178)+"+"+str(d)), (f8_3, "("+str(b)+"/x)"+"+"+str(d))]
tasks_for_9 = [(f9_1, ( "(x"+chr(178)+"+"+str(b)+"x+"+str(d)+"):"+str(c)) ), (f9_2, (str(a)+"x"+chr(179)+"+"+str(b)+"x"+"+"+str(c)+"x+"+str(d)) ), (f9_3, (str(b)+"√x"+"+"+str(d)) ),
(f9_4, (str(a)+"sin(x)") ), (f9_5, (str(a)+"cos(x)") )]
tasks_for_10 = [(f10_1, "log" + "\u2082" + "(x)"), (f10_2, "log(" + "\u2081" + ":" + "\u2082" + ")(x)"), (f10_3, "arctan(x)"), (f11_1, '2ˣ'), (f11_2, "|log"+"\u2082"+"x|")]
tasks_for_11 = [(f11_1, '2ˣ'), (f11_2, "|log"+"\u2082"+"x|"), (f10_1, "log" + "\u2082" + "(x)"), (f10_2, "log(" + "\u2081" + ":" + "\u2082" + ")(x)"), (f10_3, "arctan(x)")]
avaliable_config = False
# создание поля
master = tkinter.Tk()
master.title("Тренажер - графики функций")
# чтение размеров окна
screen_width = master.winfo_screenwidth()
screen_height = master.winfo_screenheight()
k = screen_width * 5/216
w = screen_width //2
h = screen_height //2
field_width = 5/6 * screen_width
field_height = 5/6 * screen_height
# задаём начальные параметры canvas
canv = tkinter.Canvas(master, width=screen_width, height=screen_height)
bg_img = tkinter.PhotoImage(file="background.png")
bg_img_2 = tkinter.PhotoImage(file="background2.png")
canv.create_image(w/2, h/2, image=bg_img)
canv.create_image(w/2, h/2, image=bg_img_2)
# главное меню
def eight():
global change_window, func, task, avaliable_config, clas
clas = 8
avaliable_config = True
change_window()
def nine():
global change_window, func, task, avaliable_config, clas
clas = 9
avaliable_config = True
change_window()
def ten():
global change_window, func, task, avaliable_config, clas
clas = 10
avaliable_config = True
change_window()
def eleven():
global change_window, func, task, avaliable_config, clas
clas = 11
avaliable_config = True
change_window()
def choise(tasks):
global task, used
m = random.choice(tasks)
while m[1] == task or m[1] in used:
m = random.choice(tasks)
else:
return m
def change_function():
global task, func, clas
generate_coificients()
t = []
clear()
if clas == 8: t = tasks_for_8
if clas == 9: t = tasks_for_9
if clas == 10: t = tasks_for_10
if clas == 11: t = tasks_for_11
m = choise(t)
task = str(m[1])
func = m[0]
label_task = tkinter.Label(master, text=task, font='Times 16', fg="DarkBlue")
canv.create_window([100, 30], width=200, height=60, window=label_task, tags="programm")
btn_eight = tkinter.Button(master, text='8 класс', command=eight)
btn_nine = tkinter.Button(master, text='9 класс', command=nine)
btn_ten = tkinter.Button(master, text='10 класс', command=ten)
btn_eleven = tkinter.Button(master, text='11 класс', command=eleven)
btn_eight.pack()
btn_nine.pack()
btn_ten.pack()
btn_eleven.pack()
# в зависимости от размеров начального окна задаём положение кнопок
cooods_btn_x = w * 2/3
coords_btn_y = h /2
btn_coords_8 = [cooods_btn_x, coords_btn_y]
btn_coords_9 = [cooods_btn_x, coords_btn_y + 45]
btn_coords_10 = [cooods_btn_x, coords_btn_y + 2 * 45]
btn_coords_11 = [cooods_btn_x, coords_btn_y + 3 * 45]
def main_size():
global w, h
master.geometry('{}x{}'.format(w, h))
def start_screen():
global btn_coords_8, btn_coords_9, btn_coords_10, btn_coords_11, start, bg_img, w, h
canv.create_image(w / 2, h / 2, image=bg_img)
canv["cursor"] = "arrow"
master.resizable(False, False)
generate_coificients()
if start:
main_size()
start = False
else:
pass
# сами кнопки помещаем на экран через окна
canv.create_window(btn_coords_8, height=40, width=400, window=btn_eight)
canv.create_window(btn_coords_9, height=40, width=400, window=btn_nine)
canv.create_window(btn_coords_10, height=40, width=400, window=btn_ten)
canv.create_window(btn_coords_11, height=40, width=400, window=btn_eleven)
# текст на экране
choice = tkinter.Label(text = "выберите класс обучения", bg="SkyBLue")
canv.create_window([cooods_btn_x, coords_btn_y - 45], height=40, width=400, window=choice)
hello = tkinter.Label(text="Добро пожаловать", bg="SkyBLue")
canv.create_window([w/2, h/4], height=50, width=200, window=hello)
canv["bg"] = "Lightgray"
start_screen()
def change_window():
global screen_width, screen_height
a = round(screen_width * 5/6)
b = round(screen_height * 5/6)
master.geometry('{}x{}+{}+{}'.format(a, b, 100, 50))
canv["bg"] = "lightblue"
canv["cursor"] = "pencil"
print_screen()
def back_to_menu():
global avaliable_config, canv, D, counter
counter = 1
avaliable_config = False
main_size()
canv.delete("all")
clear()
start_screen()
# интерфейс и визуал
def print_screen():
global field_width, field_height
canv.delete("all")
canv.size = (field_width, field_height)
k = field_width / 36
canv.create_line(field_width / 2, field_height, field_width / 2, 0, width=2, arrow=tkinter.LAST, tags="programm")
canv.create_line(0, field_height / 2, field_width, field_height / 2, width=2, arrow=tkinter.LAST, tags="programm")
canv.create_text([field_width / 2 - 8, field_height / 2 + 10], text="0", font="Times 16")
canv.create_text([field_width / 2 - 8 + k, field_height / 2 + 10], text="1", font="Times 16")
canv.create_text([field_width / 2 - 8, field_height / 2 + 10 + k], text="-1", font="Times 16")
canv.create_text([field_width / 2 + 8, 10], text="y", font="Times 16")
canv.create_text([field_width - 8, field_height / 2 + 10], text="x", font="Times 16")
draw_lines(k, field_width, field_height, canv)
btn_clear = tkinter.Button(master, text='очистить душу', command=clear)
btn_menu = tkinter.Button(master, text="главное меню", command=back_to_menu)
btn_next = tkinter.Button(master, text="следующая функция", command=next_func)
canv.create_window([field_width - 210, field_height - 20], height=40, width=120, window=btn_clear, tags="btn")
canv.create_window([field_width - 310, field_height - 20], height=40, width=90, window=btn_menu, tags="btn")
canv.create_window([field_width - 75, field_height - 20], height=40, width=150, window=btn_next, tags="btn_next")
counter_label = tkinter.Label(text="задание " + str(counter) + "/7", font='Times 14')
canv.create_window([field_width - 70, 35], height=70, width=140, window=counter_label)
change_function()
def next_func():
global counter, func
check()
counter += 1
counter_label = tkinter.Label(text=str(counter)+"/7")
canv.create_window([field_width - 70, 35], height=70, width=140, window=counter_label)
change_function()
canv.delete("user")
canv.delete("right")
if counter == 8:
results()
def results():
global canv, w, h, delta, used, user_enter, marks, bg_img_2, canv
main_size()
canv["cursor"] = "arrow"
canv["bg"]="lightgray"
canv.delete("all")
btn_menu = tkinter.Button(master, text="<-- главное меню", command=back_to_menu)
canv.create_window([60, 25], height=40, width=120, window=btn_menu, tags="result")
canv.create_image(w / 4, h / 2 , image=bg_img_2)
val = round(sum(marks.values()) / len(marks))
if val == 5: color = "medium spring green"
if val == 4: color = "lime green"
if val == 3: color = "yellow"
if val == 2: color = "red"
if val == 1: color = "lightcoral"
results_label = tkinter.Label(text="ваш результат: " + str(val), bg="{}".format(color))
canv.create_window([3*w/4-30, h/2+200], width=200, height=40, window=results_label, tags="result")
buttons = [tkinter.Button(master, text="задание 1", command=back_to_func_1), tkinter.Button(master, text="задание 2", command=back_to_func_2), tkinter.Button(master, text="задание 3", command=back_to_func_3),
tkinter.Button(master, text="задание 4", command=back_to_func_4), tkinter.Button(master, text="задание 5", command=back_to_func_5), tkinter.Button(master, text="задание 6", command=back_to_func_6),
tkinter.Button(master, text="задание 7", command=back_to_func_7)]
for j in range(len(buttons)):
canv.create_window([3*w/4-30, h/2+(j-1)*50-100], width=200,height=40,window=buttons[j], tags="result")
if marks["{}".format(j+1)] == 5: col = "medium spring green"
if marks["{}".format(j+1)] == 4: col = "lime green"
if marks["{}".format(j+1)] == 3: col = "yellow"
if marks["{}".format(j+1)] == 2: col = "red"
if marks["{}".format(j+1)] == 1: col = "lightcoral"
label = tkinter.Label(text=str(marks["{}".format(j+1)]), bg = "{}".format(col))
canv.create_window([3*w/4+120, h/2+(j-1)*50-100], width=60, height=40, window=label)
num = tkinter.Label(text="№ задания")
mark = tkinter.Label(text="оценка")
canv.create_window([3*w/4-30, h/2-200], width=200, height=40, window = num, tags="result")
canv.create_window([3*w/4+120, h/2-200], width=60, height=40, window=mark, tags="result")
back_to_func_1 = lambda: back_to_func(1)
back_to_func_2 = lambda: back_to_func(2)
back_to_func_3 = lambda: back_to_func(3)
back_to_func_4 = lambda: back_to_func(4)
back_to_func_5 = lambda: back_to_func(5)
back_to_func_6 = lambda: back_to_func(6)
back_to_func_7 = lambda: back_to_func(7)
def back_to_func(i):
global user_enter, used, k, field_width, field_height, canv
canv.delete("all")
a = round(field_width)
b = round(field_height)
master.geometry('{}x{}+{}+{}'.format(a, b, 100, 50))
canv["bg"] = "lightblue"
canv.size = (a, b)
canv.create_line(field_width / 2, field_height, field_width / 2, 0, width=2, arrow=tkinter.LAST, tags="programm")
canv.create_line(0, field_height / 2, field_width, field_height / 2, width=2, arrow=tkinter.LAST, tags="programm")
draw_lines(k, a, b, canv)
btn_back_to_results = tkinter.Button(master, text="итоги -->", command=results)
btn_menu = tkinter.Button(master, text="<-- главное меню", command=back_to_menu)
canv.create_window([field_width-190, 25], height=40, width=120, window=btn_menu, tags="result")
canv.create_window([field_width-70, 25], height=40, width=120, window=btn_back_to_results, tags="result")
lab = tkinter.Label(master, text=used[i-1][1], font='Times 16', fg="DarkBlue")
canv.create_window([100, 30], width=200, height=60, window=lab, tags="result")
show_user(user_enter["{}".format(i)])
show_right(used[i-1])
# логика
#очистить поле
def clear():
canv.delete("user")
canv.delete("right")
coords.clear()
# перевод в привычные людям координаты
def convert_x(x, width):
global k
result = (x - width/2) / k
return result
def convert_y(y, height):
global k
result = (height/2 - y) / k
return result
def total():
check()
results()
# SMAPE "счёт прогрешности"
def check():
global field_width, field_height, func, canv, counter, user_enter, marks, coords_all
coords_all.pop(-1)
if counter == 6:
canv.delete("btn_next")
final = tkinter.Button(master, text="завершить", command=total)
canv.create_window([field_width - 75, field_height - 20], height=40, width=150, window=final)
summ = 0
try:
if func == f10_1 or func == f10_2 or func == f11_2 or func == f9_3:
for i in coords_all:
if not(math.isnan(i[0])):
x = convert_x(i[0], field_width)
y = convert_y(i[1], field_height)
if x > 0: summ += 2 * abs(y - func(x)) / (abs(y) + abs(func(x)))
else:
for i in coords_all:
if not(math.isnan(i[0])):
x = convert_x(i[0], field_width)
y = convert_y(i[1], field_height)
if x != 0: summ += 2 * abs(y - func(x)) / (abs(y) + abs(func(x)))
except:
pass
try:
if len(coords_all) > k*2:
D = summ / len(coords_all)
else:
D = 100
except:
D = 100
print(D)
delta.append(D)
res = 0
if D < 0.4: res = 5
elif D < 0.8: res = 4
elif D <= 1.2: res = 3
elif D < 4: res = 2
elif D >= 12: res = 1
marks["{}".format(counter)] = int(res)
used.append((func, task, [a, b, c, d]))
print(str(task))
coord = coords_all.copy()
user_enter["{}".format(counter)] = coord
coords_all = []
def unconvert(y):
global k, field_height
result = field_height/2 - k*y
return result
def show_right(func1):
global field_width, field_height, a, b, c, d
ms = []
a = func1[2][0]
b = func1[2][1]
c = func1[2][2]
d = func1[2][3]
print(func1[1])
for i in range(round(field_width) - 2):
try:
x = i
j = convert_x(x, field_width)
l = func1[0](j) # .ToScreen(...) From... TODO
y = unconvert(l)
ms.append([x, y])
except :
for i in range(len(ms) - 1):
canv.create_line(ms[i][0], ms[i][1], ms[i + 1][0], ms[i + 1][1], fill="red", width=2, tags="right")
ms = []
for i in range(len(ms) - 1):
canv.create_line(ms[i][0], ms[i][1], ms[i + 1][0], ms[i + 1][1], fill="red", width=2, tags="right")
def show_user(user_coords):
for i in range(len(user_coords)-1):
if not(math.isnan(user_coords[i][0]) or math.isnan(user_coords[i][1]) or math.isnan(user_coords[i+1][0]) or math.isnan(user_coords[i+1][1])):
canv.create_line(user_coords[i][0], user_coords[i][1], user_coords[i + 1][0], user_coords[i + 1][1], fill="black", width=2, tags="right")
# ввод пользователя
# создание массивов с координатами
coords = []
coords_all = []
# основная функция, сохраняющая координаты положения мыши, когда нажата ЛКМ
def motion(event):
if avaliable_config:
x, y = event.x, event.y
if len(coords) > 0:
canv.create_line(x, y, coords[-1][0], coords[-1][1], tags='user')
if x >= 0 and y >= 0:
coords.append([x, y])
coords_all.append([x, y])
# очистка координат, нужна для бинда на мышь
def clear_coords(self):
coords.clear()
coords_all.append([math.nan, math.nan])
# необходимые бинды на мышь
master.bind('<B1-Motion>', motion)
master.bind('<ButtonRelease-1>', clear_coords)
# необходимое для 'tkinter'
canv.pack()
master.mainloop()