psychopy.org | Reference | Downloads | Github

How to set tStart and duration for Square?

Hello, I read from an Excel file which has a column called “warnSign” that has 0 or 1 values. If it’s 1 I should show a square to the subject. So I put the code below in the BeginRoutine :

if int(warnSign) == 1 :
   rect = visual.Rect(
       win=win,
       units="pix",
       width=200,
       height=200,
       fillColor=[-1,-1,-1]
   )
   rect.draw()


It shows nicely but I want to set tStart and duration for my square. Can you please tell me how can I do that?

What do you mean by

Do you just want to store when the presentation started? Or do you want to set the onset time. To accomplish the latter, you have to use loops. To learn how to dothisyou could first create a small example in the builder; then, compile it and try to understand the code.

If you just want to store the actual onset time and duration you can set the tStart attribute: t = yourClock.getTime(); rect.tStart =t and t = yourClock.getTime(); rect.duration =t- rect.tStart

1 Like

@LukasPsy Thanks, I had used loops but I recalled can use Polygon component and with 0 or 1 values I change opacity of the polygon. :blush:
And thanks for the codes above, I’m sure can use them in the future projects.

Oh sorry. I overlooked that you were working in the builder view. I finally get what you wanted to do. Your solution with the polygon component is definitely correct.
Should you ever have to do this in code, you could move your initialisation of the rect object to the ‘begin experiment’ tab, but without the if statement, and add a variable rect_onsettime = 2; rect_duration = 2 (substitute 2 with what you want tStart / your duration to be).
Then, in the ‘each frame’ tab set the autodraw attribute to true:

if t >= rect_onsettime and rect.status == NOT_STARTED: 
    rect.autodraw = true
    rect.status = STARTED
    rect.tStart = t
if rect.status == STARTED and t >= (rect.tStart + rect_duration)
    rect.autodraw = false
    rect.status = NOT_STARTED

I haven’t tested this code. There are probably some mistakes in it, but it could serve as a starting point.

1 Like

I appreciate it, Thanks for your response.