| Reference | Downloads | Github

Psychopy on pycharm

I’m trying to control the position of an image stimuli for my experiment on PyCharm.
The stimulus window appears only for a second and then disappears, and it does not take any keyboard input.
Does working with PsychoPy on PyCharm need any specific arrangements?

This is my code:

from psychopy import core, event, visual, monitors, clock
import psychopy.event
# monitor
widthPix = 1920 # screen width in px
heightPix = 1080 # screen height in px
monitorwidth = 53.1 # monitor width in cm
viewdist = 60. # viewing distance in cm
monitorname = 'monitorname'
Screen = 0 # 0 to use main screen, 1 to use external screen
mon = monitors.Monitor(monitorname, width=monitorwidth, distance=viewdist)
mon.setSizePix((widthPix, heightPix))

# Initialize window
win = visual.Window(
    size=(400, 400),

period = psychopy.core.StaticPeriod(screenHz=60)

image = 'C:/Users/CVBE/PycharmProjects/spaceship_in_psychopy/spaceship.png'

image_stim = visual.ImageStim(win, image, pos=(0.0, 0.0))

# text_stim.draw()
# period.complete()

keys = psychopy.event.waitKeys()
text_stim = visual.TextStim(
    pos=(0.0, 0.0),
# period.start(2)

# image_stim.draw()

if keys == 'right':
    image_stim = visual.ImageStim(win, image, pos=(50, 0.0))
elif keys == 'left':
    image_stim = visual.ImageStim(win, image, pos=(-1.0, 0.0))
elif keys == 'up':
    image_stim = visual.ImageStim(win, image, pos=(0.0, 1.0))
elif keys == 'down':
    image_stim = visual.ImageStim(win, image, pos=(0.0, -1.0))
    image_stim = visual.ImageStim(win, image, pos=(0.0, 0.0))

I’m not an expert on PyCharm but there are a comparatively small number of posts about it. I’d recommend you search the forum and see if any of the previous issues and solutions can help you.

1 Like

May I ask if the code I shared works for you? Does it take the keyboard input to move the image to right/left/etc? (You can use a sample image of your own)

Hi @Zahra,
if the information from the issue linked below are still accurate then running PsychoPy from pycharm may be problematic.

You may try installing psychopy via conda (from the conda-forge channel) and trying running the pycharm script in that environment. Did you try running your script in PsychoPy Coder?

1 Like

Thanks @mmagnuski for your reply.
On Mac, I brewed Psychopy, but importing it into PyCharm wasn’t possible.
On Windows (11pro), I pip installed PsychoPy and I could import it into PyCharm. I haven’t used the Coder; I have coded it into a PyCharm script. It does create the stimuli that I code, but it does not take over the screen. It only shows the stimuli in a small window which disappears as soon as I press any key. I need to know if the disappearing is because I have coded it wrong, or it’s PyCharm’s problem.

Hi Zahra, I assume you’re not planning on putting your task online since you’re working from PyCharm? I have a builder task that can move an image with keyboard that works locally and in full screen, and you can take a look at the python script that can be generated from the task. Hopefully this will give you some ideas on how to proceed in PyCharm :slight_smile:

green square
moveSquare.psyexp (10.1 KB)

1 Like

Thanks a lot @suelynnmah this has helped me a lot!
I have to add much more features to it for my experiment, but it’s great for a start.

After reading your code I have the following suggestions:

  1. IIRC psychopy.event.waitKeys() returns a list of keys, not a single key, so you could change the waitKeys to:
    key = psychopy.event.waitKeys()[0]
    or you could later not test for equality (keys == 'right') but membership (`‘right’ in keys).
  2. after checking the pressed key and flipping the window you should add another waitKeys() or at least something like psychopy.core.wait(2) - otherwise the window will likely close before you notice anything.
  3. there is potential for refactor of the part where you check the pressed key, for example:
    image_stim = visual.ImageStim(win, image, pos=(0.0, 0.0))
    key_to_pos = {'right': (1, 0), 'left': (-1, 0), 'up': (0, 1),
                  'down': (0.0, -1.0)}
    if key in key_to_pos.keys():
        image_stim.pos = key_to_pos[key]
  4. I don’t think you need the StaticPeriod in the example you show.

But generally: to test if there is a problem in your code or the IDE you are using has problems running PsychoPy - just try testing your code using PsychoPy runner. :slight_smile:

1 Like

thanks a lot @mmagnuski for the useful suggestions! sorry I hadn’t checked here in a while because of illness. Your suggestions helped a lot!