· 6 years ago · May 29, 2019, 08:06 PM
1from tkinter import *
2import sqlite3
3
4#executemany для списков!!!!!!!!!!!!!!!!!!!!!!!!
5conn = sqlite3.connect('TestsBase.db')
6c = conn.cursor()
7
8sql ='create table if not exists TestInfo (id INTEGER NOT NULL, name TEXT, kolq INTEGER, sysmark TEXT, PRIMARY KEY(id))'
9c.execute(sql)
10
11def creatt():
12 def nextstep():
13 numq=0
14 namet = textbox1.get()
15 kq = textbox2.get()
16 sm = textbox3.get()
17
18 sql = '''insert into TestInfo (name, kolq, sysmark) values (?,?,?)'''
19 c.execute(sql, (namet,kq,sm))
20 c.execute("SELECT * FROM TestInfo")
21 print(c.fetchall())
22
23 c.execute('''SELECT * FROM TestInfo WHERE name = ?;''',(namet,))
24 results=c.fetchall()
25 for result in results:
26 testi=result[0]
27
28 creat.destroy()
29 filltest = Tk()
30 filltest.title("Заполнение теста")
31 filltest.geometry("800x500")
32
33 frame1=Frame(filltest,bd=5)
34 frame2=Frame(filltest,bd=5)
35
36
37 lbl=Label(filltest, text="Тип вопроса:")
38 lbl.grid(row = 1, column = 0, sticky = W)
39 lbl.place(relx=0.2, rely=0.55)
40 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
41
42 def inputans():
43 def nextq():
44 sql ='create table if not exists TestText (id INTEGER, testid INTEGER, qtype TEXT, question TEXT, answer TEXT, varanswer TEXT, PRIMARY KEY(id))'
45 c.execute(sql)
46 c.execute("SELECT * FROM TestText")
47 result=c.fetchall()
48 if result==[]:
49 q = textbox1.get()
50 a = textbox2.get()
51 va = '---'
52 qt='1'
53 c.execute("INSERT INTO TestText (testid,qtype,question,answer,varanswer) VALUES (?,?,?,?,?)",(testi,qt,q,a,va,))
54 c.execute("SELECT * FROM TestText")
55 print(c.fetchall())
56 else:
57 c.execute('''SELECT * FROM TestInfo WHERE id = ?;''',(testi,))
58 results=c.fetchall()
59 for result in results:
60 kq=int(result[2])
61 c.execute('''SELECT * FROM TestText WHERE testid = ?;''',(testi,))
62 results=c.fetchall()
63 for result in results:
64 numq=int(result[0])
65 if numq < kq:
66 q = textbox1.get()
67 a = textbox2.get()
68 va = '---'
69 qt='1'
70 c.execute("INSERT INTO TestText (testid,qtype,question,answer,varanswer) VALUES (?,?,?,?,?)",(testi,qt,q,a,va,))
71 c.execute("SELECT * FROM TestText")
72 print(c.fetchall())
73 else:
74 filltest.destroy()
75 info = Tk()
76 info.title("!!!")
77 info.geometry("200x100")
78
79 lbl=Label(info, text="Вы ввели все вопросы!")
80 lbl.grid(row = 0, column = 0, sticky = W + E)
81 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
82 lbl=Label(info, text="Теперь Вы можете пройти тест.")
83 lbl.grid(row = 2, column = 0, sticky = W + E)
84 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
85
86 frame2.grid_remove()
87
88 lbl=Label(frame1, text="Введите вопрос:")
89 lbl.grid(row = 0, column = 0, sticky = W)
90 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
91 lbl=Label(frame1, text="Введите верный ответ:")
92 lbl.grid(row = 1, column = 0, sticky = W)
93 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
94
95 textbox1 = Entry(frame1, width = 50)
96 textbox1.focus_set()
97 textbox1.grid(row = 0, column = 1, sticky = W)
98 textbox2 = Entry(frame1, width = 10)
99 textbox2.grid(row = 1, column = 1, sticky = W)
100
101 butnextq = Button(frame1, text = "Следующий вопрос", command = nextq)
102 butnextq.grid(row = 4, column = 1, padx=20, pady=10, sticky = W + E)
103 butnextq.configure(font="Arial, 10",bg="#001D50", fg="green")
104 frame1.grid(row = 2, column = 0, columnspan = 3)
105
106 def selectans():
107 def nextq2():
108 sql ='create table if not exists TestText (id INTEGER, testid INTEGER, qtype TEXT, question TEXT, answer TEXT, varanswer TEXT, PRIMARY KEY(id))'
109 c.execute(sql)
110 c.execute("SELECT * FROM TestText")
111 result=c.fetchall()
112 if result==[]:
113 q = textbox1.get()
114 va = textbox2.get()
115 a = textbox3.get()
116 qt='2'
117 c.execute("INSERT INTO TestText (testid,qtype,question,answer,varanswer) VALUES (?,?,?,?,?)",(testi,qt,q,a,va,))
118 c.execute("SELECT * FROM TestText")
119 print(c.fetchall())
120 c.execute('''SELECT * FROM TestInfo WHERE id = ?;''',(testi,))
121 results=c.fetchall()
122 for result in results:
123 kq=int(result[2])
124 c.execute('''SELECT * FROM TestText WHERE testid = ?;''',(testi,))
125 results=c.fetchall()
126 for result in results:
127 numq=int(result[0])
128 if numq < kq:
129 q = textbox1.get()
130 a = textbox3.get()
131 va = textbox2.get()
132 qt='2'
133 c.execute("INSERT INTO TestText (testid,qtype,question,answer,varanswer) VALUES (?,?,?,?,?)",(testi,qt,q,a,va,))
134 c.execute("SELECT * FROM TestText")
135 print(c.fetchall())
136 else:
137 filltest.destroy()
138 info = Tk()
139 info.title("!!!")
140 info.geometry("200x100")
141
142 lbl=Label(info, text="Введены все вопросы")
143 lbl.grid(row = 0, column = 0, sticky = W + E)
144 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
145 lbl=Label(info, text="Теперь Вы можете пройти тест.")
146 lbl.grid(row = 2, column = 0, sticky = W + E)
147 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
148
149 frame1.grid_remove()
150 lbl=Label(frame2, text="Введите вопрос:")
151 lbl.grid(row = 0, column = 0, sticky = W)
152 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
153 lbl=Label(frame2, text="Введите варианты ответов через пробел:")
154 lbl.grid(row = 1, column = 0, sticky = W)
155 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
156 lbl=Label(frame2, text="Введите номер правильного ответа:")
157 lbl.grid(row = 2, column = 0, sticky = W)
158 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
159
160 textbox1 = Entry(frame2, width = 50)
161 textbox1.focus_set()
162 textbox1.grid(row = 0, column = 1, sticky = W)
163 textbox2 = Entry(frame2, width = 50)
164 textbox2.grid(row = 1, column = 1, sticky = W)
165 textbox3 = Entry(frame2, width = 10)
166 textbox3.grid(row = 2, column = 1, sticky = W)
167
168
169 butnextq = Button(frame2, text = "Следующий вопрос", command = nextq2)
170 butnextq.grid(row = 4, column = 1, padx=20, pady=10, sticky = W + E)
171 butnextq.configure(font="Arial, 10",bg="#001D50", fg="green")
172 frame2.grid(row = 3, column = 0, columnspan = 3)
173
174 qt = IntVar()
175 rbtn1 = Radiobutton(filltest, text = "Вопрос - полный ответ", value = 1, variable = qt, command=inputans)
176 rbtn2 = Radiobutton(filltest, text = "Вопрос - множественный", value = 2, variable = qt, command=selectans)
177 rbtn1.grid(row = 1, column = 1, padx=10, pady=20)
178 rbtn1.place(relx=0.4, rely=0.5)
179 rbtn1.configure(font="Arial, 10",bg="#001D50", fg="green")
180
181 rbtn2.grid(row = 1, column = 2, padx=10, pady=20)
182 rbtn2.place(relx=0.4, rely=0.6)
183 rbtn2.configure(font="Arial, 10",bg="#001D50", fg="green")
184
185 creat = Tk()
186 creat.title("Создание теста")
187 creat.geometry("800x500")
188
189
190 lbl=Label(creat, text="Назовите тест:")
191 lbl.grid(row = 1, column = 0, sticky = W)
192 lbl.place(relx=0.2, rely=0.1)
193 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
194 lbl=Label(creat, text="Сколько вопросов:")
195 lbl.grid(row = 2, column = 0, sticky = W)
196 lbl.place(relx=0.2, rely=0.2)
197 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
198 lbl=Label(creat, text="Разбалловка:")
199 lbl.grid(row = 3, column = 0, sticky = W)
200 lbl.place(relx=0.2, rely=0.3)
201 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
202
203 textbox1 = Entry(creat, width = 50)
204
205 textbox1.focus_set()
206 textbox1.grid(row = 1, column = 1, sticky = W)
207 textbox1.place(relx=0.4, rely=0.1)
208
209 textbox2 = Entry(creat, width = 10)
210 textbox2.grid(row = 2, column = 1, sticky = W)
211 textbox2.place(relx=0.4, rely=0.2)
212
213 textbox3 = Entry(creat, width = 10)
214 textbox3.grid(row = 3, column = 1, sticky = W)
215 textbox3.place(relx=0.4, rely=0.3)
216
217 butnext = Button(creat, text = "Далее", command = nextstep)
218 butnext.grid(row = 4, column = 1, padx=200, pady=100, sticky = W + E)
219 butnext.place(relx=0.3, rely=0.7)
220 butnext.configure(font="Arial, 10",bg="#001D50", fg="green")
221
222#_________________________________________________________________________
223
224def executet():
225
226 execute = Tk()
227 execute.title("Прохождение теста")
228 execute.geometry("800x500")
229 lbl=Label(execute, text="INFO:")
230 lbl.grid(row = 0, column = 1, sticky = W)
231
232 lbl=Label(execute, text="Введите название теста:")
233 lbl.grid(row = 3, column = 0, sticky = W)
234 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
235 textbox1 = Entry(execute, width = 50)
236 textbox1.grid(row = 3, column = 1, sticky = W)
237
238 lbl=Label(execute, text="Введите ФИО:")
239 lbl.grid(row = 4, column = 0, sticky = W)
240 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
241 textbox2 = Entry(execute, width = 50)
242 textbox2.grid(row = 4, column = 1, sticky = W)
243
244 sql ='create table if not exists TestAnswers (id INTEGER NOT NULL, name TEXT, uanswer TEXT, PRIMARY KEY(id))'
245 c.execute(sql)
246
247 def starttest():
248 def nexta():
249 def nextaa():
250 c.execute('''SELECT * FROM TestText WHERE testid = ? AND id = ?;''',(takenid,ida))
251 results=c.fetchall()
252 for result in results:
253 questiontype=int(result[2])
254
255 if questiontype == 1:
256 uans = textbox1.get()
257 c.execute("INSERT INTO TestAnswers (uanswer) VALUES (?)",(uans,))
258 c.execute("SELECT * FROM TestAnswers")
259 print(c.fetchall())
260
261 if ida == kq:
262 start.destroy()
263 resulttest = Tk()
264 resulttest.title("Результаты")
265 resulttest.geometry("800x500")
266 lbl=Label(resulttest, text="Тест пройден!")
267 lbl.grid(row = 0, column = 0, sticky = W)
268 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
269 i=1
270 point=0
271 while (i<=kq):
272 c.execute('''SELECT * FROM TestText WHERE id = ?;''',(i,))
273 que=c.fetchall()
274 for correcta in que:
275 correctans=correcta[4]
276 c.execute('''SELECT * FROM TestAnswers WHERE id = ?;''',(i,))
277 ans=c.fetchall()
278 for ua in ans:
279 yourans=ua[2]
280 if correctans == yourans:
281 point=point+1
282 print(point)
283 i=i+1
284 c.execute('''SELECT * FROM TestInfo WHERE name = ?;''',(namecheck,))
285 results=c.fetchall()
286 for result in results:
287 typemark=result[3]
288 if typemark == '1':
289 lbl2=Label(resulttest, text="Правильных ответов: ")
290 lbl2.grid(row = 1, column = 0, sticky = W)
291 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
292 lbl2=Label(resulttest, text=point)
293 lbl2.grid(row = 1, column = 1, sticky = W)
294 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
295 else:
296 mark = (point/kq)*100
297 print(mark)
298 if mark < 50:
299 mark = 2
300 if (mark >= 50) and (mark<75):
301 mark = 3
302 if (mark >= 75) and (mark < 90):
303 mark = 4
304 if (mark >= 90) and (mark < 100):
305 mark = 5
306 lbl2=Label(resulttest, text="Оценка: ")
307 lbl2.grid(row = 1, column = 0, sticky = W)
308 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
309 lbl2=Label(resulttest, text=mark)
310 lbl2.grid(row = 1, column = 1, sticky = W)
311 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
312 else:
313 start.withdraw()
314 nexta()
315
316 start = Tk()
317 start.title("Вопрос")
318 start.geometry("800x500")
319
320 c.execute('''SELECT * FROM TestInfo WHERE id = ?;''',(takenid,))
321 results=c.fetchall()
322 for result in results:
323 kq=int(result[2])
324
325 lbl=Label(start, text="Всего вопросов:")
326 lbl.grid(row = 0, column = 0, sticky = W)
327 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
328 lbl=Label(start, text=kq)
329 lbl.grid(row = 0, column = 1, sticky = W)
330 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
331
332 c.execute("SELECT * FROM TestAnswers")
333 result = c.fetchall()
334
335 if result == []:
336 ida=1
337 c.execute('''SELECT * FROM TestText WHERE testid = ? AND id = ?;''',(takenid,ida))
338 results=c.fetchall()
339 for result in results:
340 numq=int(result[0])
341 questiontype=int(result[2])
342 question=str(result[3])
343 answer=str(result[4])
344 varanswer=str(result[5])
345 print(numq,questiontype,question,answer,varanswer)
346
347 if questiontype == 1:
348 lbl=Label(start, text=question)
349 lbl.grid(row = 2, column = 0, sticky = W)
350
351 textbox1 = Entry(start, width = 50)
352 textbox1.focus_set()
353 textbox1.grid(row = 3, column = 0, sticky = W)
354#HEREEEEEEEEEEEEEEEEEEE
355 if questiontype == 2:
356 def varinsert():
357 uans = ans.get()
358 print(uans)
359 c.execute("INSERT INTO TestAnswers (uanswer) VALUES (?)",(uans,))
360 nextaa()
361
362 lbl=Label(start, text=question)
363 lbl.grid(row = 2, column = 0, sticky = W)
364 varanswer=varanswer.split()
365 v1 = str(varanswer[0])
366 v2 = str(varanswer[1])
367 v3 = str(varanswer[2])
368 v4 = str(varanswer[3])
369
370 ans = StringVar()
371 rbtn1 = Radiobutton(start, text = v1, indicatoron=0, value = '1', variable = ans, command = varinsert)
372 rbtn2 = Radiobutton(start, text = v2, indicatoron=0, value = '2', variable = ans, command = varinsert)
373 rbtn3 = Radiobutton(start, text = v3, indicatoron=0, value = '3', variable = ans, command = varinsert)
374 rbtn4 = Radiobutton(start, text = v4, indicatoron=0, value = '4', variable = ans, command = varinsert)
375 rbtn1.grid(row = 3, column = 1)
376 rbtn2.grid(row = 4, column = 1)
377 rbtn3.grid(row = 5, column = 1)
378 rbtn4.grid(row = 6, column = 1)
379
380 butnextq = Button(start, text = "Далее", command = nextaa)
381 butnextq.grid(row = 7, column = 1, sticky = W + E)
382 butnextq.configure(font="Arial, 10",bg="#001D50", fg="green")
383
384 else:
385 c.execute("SELECT * FROM TestAnswers")
386 result = c.fetchall()
387 print(result)
388 for i in result:
389 ida = int(i[0])
390 ida=ida+1
391 c.execute('''SELECT * FROM TestText WHERE testid = ? AND id = ?;''',(takenid,ida))
392 results=c.fetchall()
393 for result in results:
394 numq=int(result[0])
395 questiontype=int(result[2])
396 question=str(result[3])
397 answer=str(result[4])
398 lbl=Label(start, text=question)
399 lbl.grid(row = 2, column = 0, sticky = W)
400
401 textbox1 = Entry(start, width = 50)
402 textbox1.focus_set()
403 textbox1.grid(row = 3, column = 0, sticky = W)
404
405 butnextq = Button(start, text = "Далее", command = nextaa)
406 butnextq.grid(row = 7, column = 1, sticky = W + E)
407 butnextq.configure(font="Arial, 10",bg="#001D50", fg="green")
408
409 c.execute('''SELECT * FROM TestText WHERE testid = ? AND id = ?;''',(takenid,ida))
410 results=c.fetchall()
411
412 lbl=Label(start, text="Номер текущего вопроса:")
413 lbl.grid(row = 1, column = 0, sticky = W)
414 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
415 lbl=Label(start, text=ida)
416 lbl.grid(row = 1, column = 1, sticky = W)
417 lbl.configure(font="Arial, 10",bg="#001D50", fg="green")
418
419 namecheck = textbox1.get()
420 username = textbox2.get()
421 takenid = IntVar()
422 c.execute('''SELECT * FROM TestInfo WHERE name = ?;''',(namecheck,))
423 results=c.fetchall()
424 for i in results:
425 takenid = i[0]
426 if results == []:
427 print('Error')
428 error = Tk()
429 error.title("Ошибка")
430 error.geometry("200x100")
431 lbl=Label(error, text="Тест не найден.")
432 lbl.configure(font="Arial, 10",bg="#001D50", fg="red")
433 lbl.grid(row = 0, column = 1, sticky = W + E)
434 lbl=Label(error, text="Попробуйте ввести название теста снова.")
435 lbl.grid(row = 1, column = 1, sticky = W + E)
436 lbl.configure(font="Arial, 10",bg="#001D50", fg="red")
437 else:
438 nexta()
439
440 butnext = Button(execute, text = "Начать", command = starttest)
441 butnext.grid(row = 5, column = 1, padx=200, pady=100, sticky = W + E)
442 butnext.configure(font="Arial, 10",bg="#001D50", fg="green")
443
444window = Tk()
445window.title("Тест")
446window.geometry("500x500")
447butcreat = Button(window, text = "Создать тест", command = creatt)
448butcreat.grid(row = 5, column = 5)
449butcreat.place(relx=0.2, rely=0.5)
450butcreat.configure(font="Arial, 14",bg="#001D50", fg="green")
451butstart = Button(window, text = "Пройти тест", command = executet)
452butstart.grid(row = 5, column = 5)
453butstart.place(relx=0.6, rely=0.5)
454butstart.configure(font="Arial, 14",bg="#001D50", fg="green")
455
456
457
458
459
460conn.commit()
461window.mainloop()