I am trying to design a task, where stimulus is presented on screen, when user is ready, they press any key then shows a window which asks user to type their input. I want to set a timer of 90 seconds for input and then presenting another stimulus after time out.
Here is the code:
from __future__ import absolute_import, division
from psychopy import locale_setup, gui, visual, core, data, event, logging, sound
import numpy as np # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle
import os # handy system and path functions
import sys # to get file system encoding
from random import shuffle
import codecs, os
import time
#from pytribe import EyeTribe
from re import match
PATH =''Path for stimulus pictures'
OUTPATH = '{0:s}\\results\\'.format(PATH) # output path for storing the results
sans = ['Gill Sans MT', 'Arial','Helvetica','Verdana'] #use the first font found on this list
# Store info about the experiment session
expName = 'Gleiche Eigenschaften' pt
expInfo = {'participant':'', 'gender (m/f)':'', 'age':''}
dlg = gui.DlgFromDict(dictionary=expInfo, title=expName)
if dlg.OK == False:
core.quit() # user pressed cancel
expInfo['date'] = data.getDateStr() # add a simple timestamp
expInfo['expName'] = expName
output_file = OUTPATH + expInfo['expName'] + '_{0:02d}.txt'.format(int(expInfo['participant']))
if os.path.isfile(output_file):
print 'Error: File already exists. Restart experiment and choose subject number which is not used yet.'
exit(1)
myWin = visual.Window(size =[1366,768], monitor= "testmonitor",color = (230,230,230),allowGUI = False, fullscr = True,
winType='pyglet',colorSpace = 'rgb255', units = 'deg')
clock = core.Clock()
#a routine to save responses to file anytime we want to
def saveThisResponse(captured_string):
outfile = OUTPATH + expInfo['expName'] + '_{0:02d}.txt'.format(int(expInfo['participant']))
f = open(outfile, 'a') #open our results file in append mode so we don't overwrite anything
f.write(captured_string) #write the string they typed
f.write('; typed at %s' %time.asctime()) #write a timestamp (very course)
f.write('\n') # write a line ending
f.close() #close and "save" the output file
#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()
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# present instructions
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
scale =0.8
# instruction screen
instr_screen = visual.ImageStim(myWin, image='{0:s}/instr_pics/GE_instr_1.PNG'.format(PATH,file))
instr_screen.size*= scale
instr_screen.draw()#draw
myWin.flip()#show
event.waitKeys()#click
# stimulus pictures
files = [f for f in os.listdir('{0:s}/stim_pics/'.format(PATH))if match(r'GEitem_[1-6]+.png', f)] # looping over 6 images as #stimulus
for file in files:
stimulus = visual.ImageStim(myWin, image='{0:s}/stim_pics/{1:s}'.format(PATH,file))
ResponseInstuction = visual.TextStim(myWin,
units='norm',height = 0.1,
pos=(0, 0.5), text='Type response now ... (escape to quit)',
font=sans,
alignHoriz = 'center',alignVert='center',
color='Black')
CapturedResponseString = visual.TextStim(myWin,
units='norm',height = 0.1,
pos=(0, 0.0), text='',
font=sans,
alignHoriz = 'center',alignVert='center',
color='Black')
captured_string = ' '
stimulus.draw() #draw stimulus1 screen
myWin.flip()#show stimulus1 screen
event.waitKeys()
ResponseInstuction.draw() # draw instruction
myWin.flip() # show instruction
# 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
#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 ['lshift','rshift']:
pass #do nothing when some keys are pressed
else:
captured_string = captured_string+key
#show it
updateTheResponse(captured_string)
Please help!!!