我正在 PySimpleGui 中制作一个测验程序。我希望它可以在不按下按钮的情况下自动出现第一个问题。有没有办法在布局中不添加问题的情况下做到这一点?使用下面的代码,问题仅在按下提交按钮后出现。
我希望它不在布局中,因为我希望人们在不编写任何代码的情况下进行自己的测验。
import PySimpleGUI as sg
data = {
"question": [
"Q1. What equation for force",
"Q2. Define isotope",
"Q3. Define wavelength",
"Q4. Define modal dispersion"
],
"answers": [
"F=ma"
,
"Isotopes are atoms of the same element but with a different number of neutrons"
,
"The least distance between adjacent particles which are in phase"
,
"Causes light travelling at different angles to arrive at different times"
],
}
# initialise the question, question number and answer
question = (data['question'])
answers = (data['answers'])
q_no = 0
sg.theme('DarkBrown4') # Adding colour
# Stuff in side the window
layout = [[sg.Text(" "), sg.Text(size=(60, 1), key='-OUTPUT-')],
[sg.Text("correct answer:"), sg.Text(size=(60, 1), key='-OUTPUTA-')],
[sg.Text('Answer here:'), sg.InputText(size=(60, 1), key='-INPUT-')],
[sg.Button('Submit'), sg.Button('Cancel')]]
# Create the Window
window = sg.Window('Quiz', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
break
window['-OUTPUT-'].update(question[q_no])
if values['-INPUT-'] == answers[q_no]:
print('correct')
q_no += 1
window['-OUTPUT-'].update(question[q_no]) # accepts the answer as correct and moves onto the next question
window['-OUTPUTA-'].update('')
window['-INPUT-'].update('')
else:
print('incorrect')
print('answer was:', answers[q_no])
window['-OUTPUTA-'].update(answers[q_no]) # shows that the answer is incorrect and displays the right answer
window.close()
我添加了这个两行代码,您可以使用visible=True
并在运行程序后使其成为这样visible=False
,如下所示:
sg.Text(question[0], key = '_Q1_', visible = True)
window['_Q1_'].Update(visible = False)
最终代码:
import PySimpleGUI as sg
data = {
"question": [
"Q1. What equation for force",
"Q2. Define isotope",
"Q3. Define wavelength",
"Q4. Define modal dispersion"
],
"answers": [
"F=ma"
,
"Isotopes are atoms of the same element but with a different number of neutrons"
,
"The least distance between adjacent particles which are in phase"
,
"Causes light travelling at different angles to arrive at different times"
],
}
# initialise the question, question number and answer
question = (data['question'])
answers = (data['answers'])
q_no = 0
sg.theme('DarkBrown4') # Adding colour
# Stuff in side the window
layout = [[sg.Text(" "), sg.Text(question[0], key = '_Q1_', visible = True),
sg.Text(size=(60, 1), key='-OUTPUT-')],
[sg.Text("correct answer:"), sg.Text(size=(60, 1), key='-OUTPUTA-')],
[sg.Text('Answer here:'), sg.InputText(size=(60, 1), key='-INPUT-')],
[sg.Button('Submit'), sg.Button('Cancel')]]
# Create the Window
window = sg.Window('Quiz', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
break
window['_Q1_'].Update(visible = False)
window['-OUTPUT-'].update(question[q_no])
if values['-INPUT-'] == answers[q_no]:
print('correct')
q_no += 1
window['-OUTPUT-'].update(question[q_no]) # accepts the answer as correct and moves onto the next question
window['-OUTPUTA-'].update('')
window['-INPUT-'].update('')
else:
print('incorrect')
print('answer was:', answers[q_no])
window['-OUTPUTA-'].update(answers[q_no]) # shows that the answer is incorrect and displays the right answer
window.close()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句