psychopy.org | Reference | Downloads | Github

Cancelling in the GUI

Hi guys,

I’m sure this is a relatively trivial question but I cannot seem to work out how to get an experiment to cancel once the GUI has been loaded. Please see below on how I am using the GUI.

OKGO = 0 
while OKGO == 0:
    dictDlg = gui.DlgFromDict(dictionary=info,
            title='memory - on going', fixed=['ExpVersion'])
    if len(info['Participant']) == 0:
        print "did not enter participant number"
        win32api.MessageBox(0,'You did not enter the participant number','Error')
    else:
       OKGO = 1
if dictDlg.OK:
    print(info)
else:
    print('User Cancelled')

If the user presses cancel or x it says ‘you did not enter the participant number’ and will not let the user leave until they have entered a participant number and started the experiment. Any help is appreciated :slight_smile:

You should check the dictDlg.OK immediately after you enter details, and break out of your while loop if you pressed cancel. Otherwise, you never actually exit your while loop!

You might also want to consider changing your while loop to use the condition len(info['Participant']) == 0 in the first place so that you don’t have to use the variable OKGO - but that’s just an idea for readability :wink:

OKGO = 0
while OKGO == 0:
    dictDlg = gui.DlgFromDict(dictionary=info,
            title='memory - on going', fixed=['ExpVersion'])
    if not dictDlg.OK:
        print("User cancelled")
        break
    if len(info['Participant']) == 0:
        print "did not enter participant number"
        win32api.MessageBox(0,'You did not enter the participant number','Error')
    else:
       OKGO = 1
if dictDlg.OK:
    print(info)

To make your code a bit more explicit and readable I’d recommend merging the two if statements:

OKGO = 0
while OKGO == 0:
    dictDlg = gui.DlgFromDict(dictionary=info,
            title='memory - on going', fixed=['ExpVersion'])
    if not dictDlg.OK:
        print("User cancelled")
        break
    elif len(info['Participant']) == 0:
        print "did not enter participant number"
        win32api.MessageBox(0,'You did not enter the participant number','Error')
    else:
       OKGO = 1
if dictDlg.OK:
    print(info)

Hope that helps!

1 Like

Thank you very much Jan! Yeah this was a big help. I made some small changes to what you suggested so that I used core.quit() instead of the break as the script was continuing with the break. But again, thank you for your help.

1 Like