psychopy.org | Reference | Downloads | Github

AttributeError: 'module' object has no attribute 'globalKeys'

I am running psychopy 1.85.1 with python 2.7.13 on Windows 7. I get the following error while trying to run the example for the function “globalKeys”:

Traceback (most recent call last):

  File "C:\Users\User\Music\testipk.py", line 16, in <module>
    event.globalKeys.add(key='c', func=change_color, func_args=[win], func_kwargs=dict(log=True),name='change window color')
AttributeError: 'module' object has no attribute 'globalKeys'

The above mentioned example:

from __future__ import print_function
from psychopy import core, event, visual


def change_color(win, log=False):
    win.color = 'blue' if win.color == 'gray' else 'gray'
    if log:
        print('Changed color to %s' % win.color)


win = visual.Window(color='gray')
text = visual.TextStim(win, text='Press C to change color,\n CTRL + Q to quit.')

# Global event key to change window background color.
event.globalKeys.add(key='c', func=change_color, func_args=[win], func_kwargs=dict(log=True),name='change window color')

# Global event key (with modifier) to quit the experiment ("shutdown key").
event.globalKeys.add(key='q', modifiers=['ctrl'], func=core.quit)

while True:
    text.draw()
    win.flip()

I really haven’t got a clue what could be wrong, any help would be appreciated.

What is

[quote]the example for the function “globalKeys”:[/quote]?

The PsychoPy event module doesn’t have an attribute called globalKeys, as the error message states. So presumably that gets added somewhere else in this code example?

I did too got this error, following the tutorial at:
http://www.psychopy.org/coder/globalKeys.html
Which instructs to:
event.globalKeys.add(key=key, func=myfunc)

Ah, this is a feature that hasn’t yet been introduced but already exists in the documentation! Hold fire using it (or use the psychopy version from the repository)