I am making a quiz program in PySimpleGui. I want it so that the first question appears automatically without pressing a button. Is there a way to do this without adding the question in the layout? With the code below the question only appears after the submit button is pressed.
I would prefer it not to be in the layout as I would like people to make their own quizzes without writing any code themselves.
JavaScript
x
58
58
1
import PySimpleGUI as sg
2
3
data = {
4
"question": [
5
"Q1. What equation for force",
6
"Q2. Define isotope",
7
"Q3. Define wavelength",
8
"Q4. Define modal dispersion"
9
],
10
"answers": [
11
12
"F=ma"
13
,
14
"Isotopes are atoms of the same element but with a different number of neutrons"
15
,
16
"The least distance between adjacent particles which are in phase"
17
,
18
"Causes light travelling at different angles to arrive at different times"
19
20
],
21
}
22
23
# initialise the question, question number and answer
24
question = (data['question'])
25
answers = (data['answers'])
26
q_no = 0
27
28
sg.theme('DarkBrown4') # Adding colour
29
# Stuff in side the window
30
31
layout = [[sg.Text(" "), sg.Text(size=(60, 1), key='-OUTPUT-')],
32
[sg.Text("correct answer:"), sg.Text(size=(60, 1), key='-OUTPUTA-')],
33
[sg.Text('Answer here:'), sg.InputText(size=(60, 1), key='-INPUT-')],
34
[sg.Button('Submit'), sg.Button('Cancel')]]
35
36
# Create the Window
37
window = sg.Window('Quiz', layout)
38
# Event Loop to process "events" and get the "values" of the inputs
39
while True:
40
event, values = window.read()
41
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
42
break
43
window['-OUTPUT-'].update(question[q_no])
44
45
if values['-INPUT-'] == answers[q_no]:
46
print('correct')
47
q_no += 1
48
window['-OUTPUT-'].update(question[q_no]) # accepts the answer as correct and moves onto the next question
49
window['-OUTPUTA-'].update('')
50
window['-INPUT-'].update('')
51
52
else:
53
print('incorrect')
54
print('answer was:', answers[q_no])
55
window['-OUTPUTA-'].update(answers[q_no]) # shows that the answer is incorrect and displays the right answer
56
57
window.close()
58
Advertisement
Answer
I add this two-line code, you can use visible=True
and after run the program makes this visible=False
, like This:
JavaScript
1
3
1
sg.Text(question[0], key = '_Q1_', visible = True)
2
window['_Q1_'].Update(visible = False)
3
final code:
JavaScript
1
62
62
1
import PySimpleGUI as sg
2
3
data = {
4
"question": [
5
"Q1. What equation for force",
6
"Q2. Define isotope",
7
"Q3. Define wavelength",
8
"Q4. Define modal dispersion"
9
],
10
"answers": [
11
12
"F=ma"
13
,
14
"Isotopes are atoms of the same element but with a different number of neutrons"
15
,
16
"The least distance between adjacent particles which are in phase"
17
,
18
"Causes light travelling at different angles to arrive at different times"
19
20
],
21
}
22
23
# initialise the question, question number and answer
24
question = (data['question'])
25
answers = (data['answers'])
26
q_no = 0
27
28
sg.theme('DarkBrown4') # Adding colour
29
# Stuff in side the window
30
31
32
layout = [[sg.Text(" "), sg.Text(question[0], key = '_Q1_', visible = True),
33
sg.Text(size=(60, 1), key='-OUTPUT-')],
34
[sg.Text("correct answer:"), sg.Text(size=(60, 1), key='-OUTPUTA-')],
35
[sg.Text('Answer here:'), sg.InputText(size=(60, 1), key='-INPUT-')],
36
[sg.Button('Submit'), sg.Button('Cancel')]]
37
38
39
# Create the Window
40
window = sg.Window('Quiz', layout)
41
# Event Loop to process "events" and get the "values" of the inputs
42
while True:
43
event, values = window.read()
44
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
45
break
46
window['_Q1_'].Update(visible = False)
47
window['-OUTPUT-'].update(question[q_no])
48
49
if values['-INPUT-'] == answers[q_no]:
50
print('correct')
51
q_no += 1
52
window['-OUTPUT-'].update(question[q_no]) # accepts the answer as correct and moves onto the next question
53
window['-OUTPUTA-'].update('')
54
window['-INPUT-'].update('')
55
56
else:
57
print('incorrect')
58
print('answer was:', answers[q_no])
59
window['-OUTPUTA-'].update(answers[q_no]) # shows that the answer is incorrect and displays the right answer
60
61
window.close()
62