Skip to content

Commit 1403d7d

Browse files
committed
GUI demonstration - PyQuiz
GUI demonstration using PySimpleGUI
1 parent 8596106 commit 1403d7d

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

Diff for: GUI/PyQuiz/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
PyQuiz is a simple script to demonstrate both the use of PySimpleGUI as an interface to python scripts,
2+
and dictionaries for storing data. The code is mostly commented and as easy to understand as I could make it.
3+
4+
It requires PySimpleGUI==4.60.1
5+
6+
Tested on Python 3.10
7+
8+
9+
Eduardo C.

Diff for: GUI/PyQuiz/main.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
PyQuiz - a simple graphical quiz software
3+
Demonstrates the use of PySimpleGui as an interface
4+
and simple dictionary interaction
5+
"""
6+
import PySimpleGUI as sg
7+
8+
# The dictionary containing our questions and answers (can come from a file, a database...)
9+
datatable = {
10+
"1": {
11+
"question": "10 + 10",
12+
"answers": {
13+
"a": "20",
14+
"b": "100",
15+
"c": "1010",
16+
},
17+
"correct_answer": "a",
18+
},
19+
"2": {
20+
"question": "5 * 5",
21+
"answers": {
22+
"a": "55",
23+
"b": "25",
24+
"c": "500",
25+
},
26+
"correct_answer": "b",
27+
},
28+
"3": {
29+
"question": "3 ** 2",
30+
"answers": {
31+
"a": "3",
32+
"b": "32",
33+
"c": "9",
34+
},
35+
"correct_answer": "c",
36+
},
37+
}
38+
39+
# Our window definition.
40+
def main_window():
41+
"""
42+
Defines the main window.
43+
:return: PySimpleGUI Window object.
44+
"""
45+
# Everything bound by []'s goes on one line.
46+
layout = [
47+
[sg.Text('Quiz!', font='_ 12 bold')],
48+
[sg.Text('Question:')],
49+
[sg.Input('', size=(30, 1), key='-QUESTION-')],
50+
[sg.Text('Answers:')],
51+
[sg.Multiline('', size=(30, 8), key='-OPTIONS-')],
52+
[sg.Radio('a', group_id='-RADIO-', key='a'),
53+
sg.Radio('b', group_id='-RADIO-', key='b'),
54+
sg.Radio('c', group_id='-RADIO-', key='c'),],
55+
[sg.Button('Start', key='-START-'), sg.Button('Answer', key='-ANSWER-'),
56+
sg.Button('Exit', key='-EXIT-')]
57+
]
58+
59+
return sg.Window('Quiz!', layout, finalize=True)
60+
61+
window = main_window()
62+
63+
# variables
64+
QUESTIONS_INDEX = 1
65+
END = False # To keep track of the end of the game
66+
ANSWERED = False # If the question is still unanswered
67+
QUESTIONING = False # If there's an active question
68+
CORRECT = 0
69+
70+
# aliases
71+
question = window['-QUESTION-']
72+
answers = window['-OPTIONS-']
73+
74+
while True: # This is the main loop.
75+
event, values = window.read()
76+
77+
if event == '-START-':
78+
if not END:
79+
QUESTIONING = True
80+
answers.update(value='')
81+
question.update(value=f'{datatable[str(QUESTIONS_INDEX)]["question"]}')
82+
for answer, answer_data in datatable[str(QUESTIONS_INDEX)]["answers"].items():
83+
answers.print(f'({answer}): {answer_data}')
84+
correct_answer = datatable[str(QUESTIONS_INDEX)]["correct_answer"]
85+
ANSWERED = False
86+
else:
87+
QUESTIONING = False
88+
sg.popup('End of Quiz.')
89+
90+
if event == '-ANSWER-':
91+
if (values['a'] or values['b'] or values['c']) and QUESTIONING:
92+
for idx in ('a', 'b', 'c'):
93+
if values[idx]:
94+
USER_CHOICE = idx
95+
if not ANSWERED:
96+
if USER_CHOICE == correct_answer:
97+
sg.popup('Correct!')
98+
CORRECT += 1
99+
else:
100+
sg.popup('Wrong.')
101+
if not ANSWERED:
102+
QUESTIONS_INDEX += 1
103+
ANSWERED = True
104+
if QUESTIONS_INDEX > len(datatable):
105+
END = True
106+
sg.popup(f'The end. You got {CORRECT} of {len(datatable)}.')
107+
if not END:
108+
window.write_event_value('-START-', '')
109+
110+
if event in (sg.WIN_CLOSED, '-EXIT-'):
111+
break
112+
113+
114+
window.close()

0 commit comments

Comments
 (0)