Dear Nynne,
Find the line:
saveThisResponse(captured_string) #write to file
and change it to something like:
thisExp.addData("ResponseText", captured_string) # add to PsychoPy data file
Whoever wrote that code used their own system to save the data. Above, you are simply replacing that with a call to PsychoPy's built-in data handling routines instead. This will add a new column to the data output file, with a header label of "ResponseText" or whatever you prefer, and store the response in the same row along with all of the other info for that trial.
Regards,
Michael
On 15 Feb, 2013, at 23:24, <
nynn...@gmail.com> wrote:
> Hi Psycho-Py users.
> Psycho-Py-version: 1.75.01
> Operating system: Windows
> I'm new to Python and PsychoPY, so please bear with me if my request is total nonsense.
> I have inserted a python-code into the code component in the Builder-version which generates a text-box where participants are allowed to type in their responses in text-format. This works fine.
> However, all the participants' responses are saved in the same file (named 'myResponses.txt'), and not in separate excel-files. I would prefer that their text-responses are saved in the same excel-file as the data from my looped trial-routines.
> Can anybody help? Below is the text-box code that I've put into the code component in Builder. Can anybody please tell me, where and what I should change and what I should change it to? (I have bolded the code-section, where I believe something should be changed)
> Thank you.
> Best, Nynne
>
> #!/usr/bin/env python
>
> # SUPPLIED WITH NO GUARANTEES!!
> from psychopy import visual, core, event
> #my lazy way of keeping time NB can be relatively inaccurate
> import time
>
> # set up a stimulus window
> myWin = visual.Window(size=(1366, 768), fullscr=True, screen=0, allowGUI=False, allowStencil=False,
> monitor='testMonitor', color=[0,0,0], colorSpace='rgb') #visual.Window((1366.0,768.0), allowGUI=False,winType='pygame',
> #monitor='testMonitor', units ='deg', fullscr=True, screen=0)
>
> #set up some fonts. If a list is provided, the first font found will be used.
> sans = ['Gill Sans MT', 'Arial','Helvetica','Verdana'] #use the first font found on this list
>
>
> #INITIALISE SOME STIMULI
> #the stimulus could be anything, I'm just using simple text
> The_stimulus = visual.TextStim(myWin,
> units='norm',height = 0.1,
> pos=(0, 0), text='',
> font=sans,
> alignHoriz = 'center',alignVert='center',
> color='Black')
>
> #acts as a cue for the participant to reponsd
> # shown at the top of the screen
> ResponseInstuction = visual.TextStim(myWin,
> units='norm',height = 0.1,
> pos=(0, 0.5), text='Angiv din alder. Tryk derefter enter',
> font=sans,
> alignHoriz = 'center',alignVert='center',
> color='Black')
>
> #will be used to show the text they are typing: will update every
> # time they type a letter
> CapturedResponseString = visual.TextStim(myWin,
> units='norm',height = 0.1,
> pos=(0, 0.0), text='',
> font=sans,
> alignHoriz = 'center',alignVert='center',
> color='Black')
>
> captured_string = '' #empty for now.. this is a string of zero length that
> # we will append our key presses to in sequence
>
> #a routine to save responses to file anytime we want to
> def saveThisResponse(captured_string):
> outfile = "./myResponses.txt"
> f = open(outfile, 'a')
> f.write(captured_string)
> f.write('; typed at %s' %time.asctime())
> f.write('\n')
> f.close()
>
> #a routine to update the string on the screen as the participant types
> def updateTheResponse(captured_string):
> CapturedResponseString.setText(captured_string)
> CapturedResponseString.draw()
> ResponseInstuction.draw()
> myWin.flip()
>
>
> #setup done, now start doing stuff
> subject_response_end = 0 # only changes when they hit return
>
> while subject_response_end == 0: # we don't want to keep going forever (if we did, we would write "while True:")
> #draw the stimulus .. this can be anything but here it is text
> The_stimulus.draw() # draw it
> myWin.flip() # show it
> time.sleep(1) #leave it on the screen for 1s
>
> #now instruct the participnat to respond
> ResponseInstuction.draw() # draw instruction
> myWin.flip() # show instruction
>
> # now we will keep tracking what's happening on the keyboard
> # until the participant hits the return key
> subject_response_finished = 0 # only changes when they hit return
>
> #check for Esc key / return key presses each frame
> while subject_response_finished == 0:
> for key in event.getKeys():
> #quit at any point
> if key in ['escape']:
> myWin.close()
> core.quit()
>
> #if the participant hits return, save the string so far out
> #and reset the string to zero length for the next trial
> elif key in ['return']:
> print 'participant typed %s' %captured_string #show in debug window
> saveThisResponse(captured_string) #write to file
> captured_string = '' #reset to zero length
> subject_response_finished = 1 #allows the next trial to start
> subject_response_end = 1 # allows the code-routine to end
>
> #allow the participant to do deletions too , using the
> # delete key, and show the change they made
> elif key in ['delete','backspace']:
> captured_string = captured_string[:-1] #delete last character
> updateTheResponse(captured_string)
> #handle spaces
> elif key in ['space']:
> captured_string = captured_string+' '
> updateTheResponse(captured_string)
> elif key in ['period']:
> captured_string = captured_string+'.'
> updateTheResponse(captured_string)
> elif key in ['comma']:
> captured_string = captured_string+','
> updateTheResponse(captured_string)
> elif key in ['æ']:
> captured_string = captured_string+'ae'
> updateTheResponse(captured_string)
> elif key in ['lshift','rshift']:
> pass #do nothing when some keys are pressed
> #etc ...
>
> #if any other key is pressed, add it to the string and
> # show the participant what they typed
> else:
> captured_string = captured_string+key
> #show it
> updateTheResponse(captured_string)
>
> --
> You received this message because you are subscribed to the Google Groups "psychopy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
psychopy-user...@googlegroups.com.
> To post to this group, send email to
psychop...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msg/psychopy-users/-/a-VlcFOo0f4J.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>
--
Michael R. MacAskill, PhD
michael....@nzbri.org
Research Director,
New Zealand Brain Research Institute
66 Stewart St
http://www.nzbri.org/macaskill
Christchurch 8011 Ph:
+64 3 3786 072
NEW ZEALAND Fax:
+64 3 3786 080