PySimpleGui - 如何使用问题更新 GUI 但在第一次迭代期间不检查答案

PySimpleGui - How can I update GUI with a question but not check for the answer during the first iteration

我正在用 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()