-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
392 lines (309 loc) · 13.6 KB
/
main.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
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
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from css import get_css
import time
import pyodbc
global data
import random
def choose_random():
global data
cursor = connection.cursor()
cursor.execute('SELECT top 5 * FROM tbl_question order by newid()')
data=[]
for row in cursor:
data.append(row)
def choose_scores():
cursor = connection.cursor()
cursor.execute('SELECT * FROM tbl_score order by score')
scores_data=[]
for row in cursor:
scores_data.append(row)
class Ui_Form(object):
def __init__(self):
super().__init__()
self.name=""
self.score=0
self.order=0
self.QUESTION_NUMBER=5#total question to ask is tis number-
self.answer_order=[2,3,4,5]
random.shuffle(self.answer_order)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(622, 473)
css_string=get_css()
Form.setStyleSheet(css_string)
self.stacked_widget = QtWidgets.QStackedWidget(Form)
self.stacked_widget.setGeometry(QtCore.QRect(20, 30, 571, 411))
self.stacked_widget.setAutoFillBackground(False)
self.stacked_widget.setObjectName("stacked_widget")
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.start = QtWidgets.QPushButton(self.page)
self.start.setGeometry(QtCore.QRect(370, 240, 110, 51))
font = QtGui.QFont()
font.setPointSize(22)
self.start.setFont(font)
self.start.setObjectName("start")
self.main_label = QtWidgets.QLabel(self.page)
self.main_label.setGeometry(QtCore.QRect(50, 40, 441, 131))
font = QtGui.QFont()
font.setPointSize(28)
self.main_label.setFont(font)
self.main_label.setWordWrap(True)
self.main_label.setObjectName("main_label")
self.scores = QtWidgets.QPushButton(self.page)
self.scores.setGeometry(QtCore.QRect(370, 300, 110, 51))
font = QtGui.QFont()
font.setPointSize(21)
self.scores.setFont(font)
self.scores.setObjectName("scores")
self.stacked_widget.addWidget(self.page)
self.page_4 = QtWidgets.QWidget()
self.page_4.setObjectName("page_4")
self.lineEdit = QtWidgets.QLineEdit(self.page_4)
self.lineEdit.setGeometry(QtCore.QRect(210, 120, 291, 61))
font = QtGui.QFont()
font.setPointSize(28)
self.lineEdit.setFont(font)
self.lineEdit.setAutoFillBackground(False)
self.lineEdit.setObjectName("lineEdit")
self.cont = QtWidgets.QPushButton(self.page_4)
self.cont.setGeometry(QtCore.QRect(370, 240, 150, 51))
font = QtGui.QFont()
font.setPointSize(22)
self.cont.setFont(font)
self.cont.setObjectName("cont")
self.name_label = QtWidgets.QLabel(self.page_4)
self.name_label.setGeometry(QtCore.QRect(10, 130, 201, 41))
font = QtGui.QFont()
font.setPointSize(28)
self.name_label.setFont(font)
self.name_label.setObjectName("name_label")
self.stacked_widget.addWidget(self.page_4)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.soru = QtWidgets.QLabel(self.page_2)
self.soru.setGeometry(QtCore.QRect(20, 20, 521, 111))
font = QtGui.QFont()
font.setPointSize(24)
self.soru.setFont(font)
self.soru.setScaledContents(False)
self.soru.setWordWrap(True)
self.soru.setObjectName("soru")
self.qa = QtWidgets.QPushButton(self.page_2)
self.qa.setGeometry(QtCore.QRect(30, 130, 51, 51))
font = QtGui.QFont()
font.setPointSize(22)
self.qa.setFont(font)
self.qa.setObjectName("qa")
self.qb = QtWidgets.QPushButton(self.page_2)
self.qb.setGeometry(QtCore.QRect(30, 200, 51, 51))
font = QtGui.QFont()
font.setPointSize(22)
self.qb.setFont(font)
self.qb.setObjectName("qb")
self.qc = QtWidgets.QPushButton(self.page_2)
self.qc.setGeometry(QtCore.QRect(30, 270, 51, 51))
font = QtGui.QFont()
font.setPointSize(22)
self.qc.setFont(font)
self.qc.setObjectName("qc")
self.qd = QtWidgets.QPushButton(self.page_2)
self.qd.setGeometry(QtCore.QRect(30, 340, 51, 51))
font = QtGui.QFont()
font.setPointSize(22)
self.qd.setFont(font)
self.qd.setObjectName("qd")
self.c1 = QtWidgets.QLabel(self.page_2)
self.c1.setGeometry(QtCore.QRect(100, 130, 301, 51))
font = QtGui.QFont()
font.setPointSize(21)
self.c1.setFont(font)
self.c1.setObjectName("c1")
self.c2 = QtWidgets.QLabel(self.page_2)
self.c2.setGeometry(QtCore.QRect(100, 200, 301, 51))
font = QtGui.QFont()
font.setPointSize(21)
self.c2.setFont(font)
self.c2.setObjectName("c2")
self.c3 = QtWidgets.QLabel(self.page_2)
self.c3.setGeometry(QtCore.QRect(100, 270, 301, 51))
font = QtGui.QFont()
font.setPointSize(21)
self.c3.setFont(font)
self.c3.setObjectName("c3")
self.c4 = QtWidgets.QLabel(self.page_2)
self.c4.setGeometry(QtCore.QRect(100, 340, 301, 51))
font = QtGui.QFont()
font.setPointSize(21)
self.c4.setFont(font)
self.c4.setObjectName("c4")
self.stacked_widget.addWidget(self.page_2)
self.page_3 = QtWidgets.QWidget()
self.page_3.setObjectName("page_3")
self.sore_label = QtWidgets.QLabel(self.page_3)
self.sore_label.setGeometry(QtCore.QRect(70, 50, 341, 101))
font = QtGui.QFont()
font.setPointSize(28)
self.sore_label.setFont(font)
self.sore_label.setObjectName("sore_label")
self.back_from_your_score = QtWidgets.QPushButton(self.page_3)
self.back_from_your_score.setGeometry(QtCore.QRect(420, 260, 81, 61))
font = QtGui.QFont()
font.setPointSize(22)
self.back_from_your_score.setFont(font)
self.back_from_your_score.setObjectName("back_from_your_score")
self.stacked_widget.addWidget(self.page_3)
self.page_5 = QtWidgets.QWidget()
self.page_5.setObjectName("page_5")
self.tableWidget = QtWidgets.QTableWidget(self.page_5)
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 301, 381))
self.tableWidget.setAutoScrollMargin(19)
self.tableWidget.setRowCount(100)
self.tableWidget.setColumnCount(2)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.horizontalHeader().setDefaultSectionSize(140)
self.tableWidget.setHorizontalHeaderLabels(('Name','Score'))
self.back_from_scores = QtWidgets.QPushButton(self.page_5)
self.back_from_scores.setGeometry(QtCore.QRect(470, 300, 81, 61))
font = QtGui.QFont()
font.setPointSize(22)
self.back_from_scores.setFont(font)
self.back_from_scores.setObjectName("back_from_scores")
self.stacked_widget.addWidget(self.page_5)
# self.update_table(Form)
self.retranslateUi(Form)
self.stacked_widget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(Form)
self.start.clicked.connect(lambda x : self.pass_to_your_name(Form))
self.cont.clicked.connect(lambda x : self.check_name_line_edit(Form))
self.qa.clicked.connect(lambda x : self.change_question_or_pass_to_your_score(Form,1))
self.qb.clicked.connect(lambda x : self.change_question_or_pass_to_your_score(Form,2))
self.qc.clicked.connect(lambda x : self.change_question_or_pass_to_your_score(Form,3))
self.qd.clicked.connect(lambda x : self.change_question_or_pass_to_your_score(Form,4))
self.back_from_your_score.clicked.connect(lambda x : self.pass_to_main(Form))
self.back_from_scores.clicked.connect(lambda x : self.pass_to_main(Form))
self.scores.clicked.connect(lambda x : self.pass_to_scores(Form))
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Trivia Quiz"))
self.start.setText(_translate("Form", "Start"))
self.cont.setText(_translate("Form", "Continue"))
self.main_label.setText(_translate("Form", "Trivia Quiz"))
self.scores.setText(_translate("Form", "Scores"))
self.lineEdit.setText(_translate("Form", ""))
self.name_label.setText(_translate("Form", "Your Name:"))
self.soru.setText(_translate("Form", "3.sayfa"))
self.qa.setText(_translate("Form", "A"))
self.qb.setText(_translate("Form", "B"))
self.qc.setText(_translate("Form", "C"))
self.qd.setText(_translate("Form", "D"))
self.c1.setText(_translate("Form", "cvp1"))
self.c2.setText(_translate("Form", "cvp2"))
self.c3.setText(_translate("Form", "cvp3"))
self.c4.setText(_translate("Form", "cvp4"))
self.sore_label.setText(_translate("Form", "Your Score: "))
self.back_from_your_score.setText(_translate("Form", "Back"))
self.back_from_scores.setText(_translate("Form", "Back"))
def retranslateQA(self, Form):
global data
_translate = QtCore.QCoreApplication.translate
self.soru.setText(_translate("MainWindow", str(data[self.order][1])))
self.c1.setText(_translate("MainWindow", str(data[self.order][self.answer_order[0]])))
self.c2.setText(_translate("MainWindow", str(data[self.order][self.answer_order[1]])))
self.c3.setText(_translate("MainWindow", str(data[self.order][self.answer_order[2]])))
self.c4.setText(_translate("MainWindow", str(data[self.order][self.answer_order[3]])))
def retranslateYourScore(self, Form):
_translate = QtCore.QCoreApplication.translate
self.sore_label.setText(_translate("Form", f"Your Score:{self.score}"))
def retranslateLineEdit(self, Form):
_translate = QtCore.QCoreApplication.translate
self.lineEdit.setText(_translate("Form", ""))
def set_to_default(self):
self.score=0
self.name=""
self.order=0
def pass_to_main(self,Form):
self.stacked_widget.setCurrentIndex(0)
def pass_to_qa(self,Form):
self.stacked_widget.setCurrentIndex(2)
self.retranslateQA(Form)
def pass_to_your_score(self,Form):
self.stacked_widget.setCurrentIndex(3)
self.retranslateYourScore(Form)
self.save_them_to_database(Form)
self.set_to_default()
choose_random()
def pass_to_your_name(self, Form):
self.stacked_widget.setCurrentIndex(1)
def save_them_to_database(self, Form):
"""saves score and name to db"""
cursor = connection.cursor()
scores_belog_to_that_name=[]
cursor.execute('select count(score) from tbl_score where name=? group by name',self.name)
for row in cursor:
scores_belog_to_that_name.append(row)
if len(scores_belog_to_that_name)==1:
cursor.execute('update tbl_score set score=? where name=?',self.score,self.name)
cursor.commit()
else:
cursor.execute('insert into tbl_score values(?,?)',self.name,self.score)
cursor.commit()
def pass_to_scores(self,Form):
try:
choose_scores()
self.stacked_widget.setCurrentIndex(4)
self.update_table(Form)
except:
print("Please connect Database")
def update_table(self,Form):
cursor = connection.cursor()
cursor.execute('SELECT * FROM tbl_score order by score desc')
table_items=[]
for row in cursor:
table_items.append(row)
self.tableWidget.setRowCount(len( table_items))
for i in range(len( table_items)):
self.tableWidget.setItem(i,0,QTableWidgetItem(table_items[i][0]))
self.tableWidget.setItem(i,1,QTableWidgetItem(str(table_items[i][1])))
def change_question_or_pass_to_your_score(self,Form,choice):
self.change_score(choice)
if (self.order<self.QUESTION_NUMBER-1):
self.order+=1
random.shuffle(self.answer_order)
self.retranslateQA(Form)
else:
self.pass_to_your_score(Form)
def check_name_line_edit(self,Form):
if self.lineEdit.text()!="":
self.get_text_and_pass_to_qa(Form)
def get_text_and_pass_to_qa(self,Form):
self.name=self.lineEdit.text()
self.retranslateLineEdit(Form)
self.pass_to_qa(Form)
self.retranslateQA(Form)
def change_score(self,choice):
true_answer_index=self.which_index_is_true()
if true_answer_index==choice:
self.score+=20
def which_index_is_true(self):
for i in range(4):
if str(data[self.order][5])== str(data[self.order][self.answer_order[i]]):
return i+1
if __name__ == "__main__":
import sys
try:
connection = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=DESKTOP-1IRMV7Q;"
"Database=TriviaQuiz;"
"Trusted_Connection=yes;")
choose_random()
except:
print("Please connect Database, for now data was set to default")
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())