Digital Clock during experiment

Hello Psychopy community,
I am looking for some help for an experiment in psychology. I am not new to psychopy, but I am new to Python coding, especially on how to integrate code lines within the builder.

The experiment consist in a simple categoric decision task, where people see a series of word on the screen presented individually, and decide if that word belongs to the category animals or professions by pressing specific keys (1,2). So far, I made this task quite easily on psychopy, running multiple blocks withint the same experiment.

However, and here’s the tricky part, people should remember to press ENTER each 2:00 minutes; for doing so, then can check the elapsing time (in mm:ss - e.g.: 04:09) by pressing the SPACEBAR.
Thus, my questions are:

  1. How can I program a clock that starts from the very beginning of the task and ends at the end? And how can I record this in the data?
  2. For the SPACEBAR call, one strategy could to put a square with the same color of the background, big enough to cover the clock. How can I record this, together with the ENTER key, associated to the exact moment in 00:00 format?

Thanks a lot in advance for the help, and I attach here the psychopy experiment with the example data.


DATA.xlsx (8.8 KB) TEST.psyexp (23.5 KB)

Hi There,

To present a running clock you can add a text component and in the ‘text’ field use


and ‘set every frame’

You can then round t accordingly :slight_smile:

‘t’ is the time of the ongoing clock that exists in the background of your experiment. For your keypress, is it that you want to watch the space bar across several routines?

Alternatively, (if I interpreted your solution correctly) you can make your text component (which is the clock itself) ‘clickable’ so that participants click the clock every 2 minutes throughout the task using the mouse or touching it on a touch screen.


1 Like

Hi Becca,
thanks a lot for your quick reply and very helpful comment.
I have tried your suggestion, but it runs a clock trial by trial (i.e.: it resets each time the next trial is displayed). I would like to have a clock that persists across several routines, from the end of the first trial, to the end of the last one.
To reply to the response question, I would like that the script would record the exact moment (even in ms) in which participant pressed the both the enter and the spacebar keys. Therefore, the clock displayed on the screen and the spacebar must be fully synchronised.

Anyway, thanks again foe the useful insight :slight_smile:


To make yourself a new clock you can add a clock to your first routine in a code component:

myClock = core.Clock()

then throughout your experiment you can instead add a text component where the text field is:


and set every frame.

Note that in your code component where you initially make the clock you will need to change code type to ‘both’ and set your JS side to read:

myClock = new util.Clock()

as in the crib sheet

If you want to save the time on the clock you have created at any point (e.g. when a key is pressed) you can add something like:

trials.addData('ThisTime', myClock.getTime())

Where trials is the name of the loop you are in.


1 Like

OMG thanks a lot that was very helpful!! :smiley:

I can see the time flowing on the bottom of the screen.
Now I will work on the rest.
Thanks again for your precious help!


No worries. Happy PsychoPying!

1 Like

Sounds like time-based prospective memory. Here’s one I wrote in 2018 which does the space bar = show clock (a maximum of once per word)pm_time.psyexp (47.3 KB) WordLists .xlsx (13.2 KB)

The basic structure is the same as the one I have used for event based PM since (experiment written in 2016)

Hi wakecarter,
yes, it is a time-based prospective memory task indeed. Thanks a lot for sharing the psychopy file: that is really useful. I will adapt it to my needs.


Hi wakecarter,
thanks a lot for sharing the file. It has been very helpful. However, i have to ask you one thing that was not clear to me at all.
in the code, at each frame fpr the trial routine, i have found this code:

if showclock == 1:
now_clock = now_clock +1
if now_clock > 100:
showclock = 0
now_clock = 0

Specifically, I do not understand what this line means: now_clock = now_clock +1
I saw tha now_clock is defined as zero at the beginning of the experiment. Therefore, is +1 a frame that is added on now_clock? If this is the case, I would like to show the clock on the screen for 3 seconds. Given that the refresh rate of my screen is 60Hz, I thought that I can put 180 to make the clock appearance lasting 3 seconds. However, this is not only the case: in the experiment, although most part of the times the clock is actually shown for 3 seconds, sometimes it appears for 5 or even 6 secs. Do you know how can I make the clock appearance’s timing more precise? And was the code above the solution to reach such regularity?
Thanks a lot for your help.



The code was written for an offline experiment where the refresh rate is more consistent. It should show the close for 100 frames, but I’m not sure that timing by frame works as well online.

How about setting now_clock to t+3 when the space bar is pressed. Then you could have:

if showclock == 1 and t > now_clock:
      showclock = 0