| Reference | Downloads | Github

Using dialogue box input as variable in experiment

I am trying to use some info in the dialogue box at the start of the experiment as a variable later in the same task.
Here is my code for the dialogue box:

dateStr = time.strftime("%b_%d_%H%M", time.localtime())#add the current time
info = {} #a dictionary
#present dialog to collect info
info['participant'] = ''
info['threshold'] = ''
dlg = gui.DlgFromDict(info, title = 'Vis100 TDCS Version') #(and from psychopy import gui at top of script) 
if not dlg.OK:
def makePath(path):
    except OSError as exception:
        if exception.errno != errno.EEXIST:

outputParentDir = os.environ['psychopyOutputParent']
participant = info['participant']
participantFolder = outputParentDir + '/' + str(participant)
task = 'Vis100 Staircase'
taskFolder = participantFolder + '/' + task
txtPath = taskFolder+'/txt/'
xlsPath = taskFolder+'/xls/'
psydatPath = taskFolder+'/psydat/'
figurePath = taskFolder + '/figure/'

#Create directories. Doing it this way will raise any errors except directory exists errors.

#make a text file to save data
fileName = info['participant'] + dateStr
dataFile = open(txtPath+fileName+'.txt', 'w')
dataFile.write('correctStimWas	stim1	stim2	correct\n	')

I am confused about where the data input from the dialogue box saves to?
When I try and create a variable ‘threshold’ later in my experiment e.g threshold = ??? I’m not sure what to write here to access the threshold input from the dialogue box.

Thanks for your help,

Hope you can see the code properly now… had to edit the post several times before ``` worked for some strange reason.

Have you looked at the docs here?

It should be in your dict named ‘info’ that you passed into gui.DlgFromDict .

threshold = info[‘threshold’]

1 Like

Hi Daniel,
Thanks for your help.
Yes, I spend hours studying all the documentation, but I am afraid I am well and truly special needs when it comes to python!
The good news is that threshold = info[‘threshold’] nearly worked, but it was giving error messages because it was treating it as a string and then trying to do maths with it later on in the programme.
But threshold = int(info[‘threshold’]) did the trick.

Thanks again,

Glad you figured it out! As long you’re reading and continue to learn and bang away you’ll get it, it can take a while to get comfortable.

I think that because you set up your info[‘threshold’] as a string at the beginning, even if the user types a number it is stored as a string. Try replacing:
info[‘threshold’] = ''
info[‘threshold’] = 0
This way you should not need later to convert the string to an int. Although of course your solution is also fine.

Thanks for the tip Marco, I’ll remember that for future use.
Best wishes,