Mouse Click to Make a Shape Disappear

Hello everyone!

I am writing with a question on a function of the PsychoPy mouse.

In my experiment, I have a function where the user can right click on any of the rectangles and a shape will appear within that rectangle. I also want to add a function where the user can left click on a rectangle which already has a shape populated and the shape will disappear. I have a small few lines that don’t seem to interfere with the code running, but also don’t seem to help.

                            if mouse.isPressedIn(g, buttons = [2]): # doesn't work yet... but doesn't interfere...
                                gabors_appear.pop(g)
                                g.autoDraw = False; win.flip()

I will attach the full section of code to this post as a file and paste the code beneath my signature. Would anyone happen to know how to get the shapes to disappear?

click.py (2.9 KB)

Thank you all for your help!

  • Lili
#========================================================================================#
# PACKAGES
## PsychoPy packages
from psychopy import core, event, monitors, visual 
from psychopy.hardware import keyboard 
kb = keyboard.Keyboard()
#========================================================================================#
# WINDOW 
mon = monitors.Monitor('glaptop') 
win = visual.Window(size = [1512, 900], monitor = mon, units = 'pix', color = [170, 170, 170], colorSpace = 'rgb255')
mouse = event.Mouse(visible = True, win = win)
#========================================================================================#
# POSITIONS 
all_pos = [(-150, 150), (-150, 0), (-300, 225), (-300, 75), (-450, 150), (-450, 0), (150, 150), (150, 0), (300, 225), (300, 75), (450, 150), (450, 0), (0, 225), (0, 75), (-150, -150), (-150, 0), (-300, -225), (-300, -75), (-450, -150), (-450, 0), (150, -150), (150, 0), (300, -225), (300, -75), (450, -150), (450, 0), (0, -225), (0, -75)]
#========================================================================================#
# SNAPSHOT
for i in range(3):
    rects = []
    for index, p in enumerate(all_pos): # emumerate keeps track of 'index' (number loop is on) and turns the positions into numbers
        rect_index = visual.Rect(win, width = 50, height = 50, units = 'pix', lineWidth = 5, fillColor = [200, 200, 200], lineColor = None, opacity = .2, autoDraw = False, pos = p) # because enumerate is keeping track of index, this is a differently labeled rectangle every time
        rects.append(rect_index) # appends each rectangle to list per position
    for r in rects: 
        r.autoDraw = True
    win.flip()
    gabors_appear = []
    global mouse_clicks
    mouse_clicks = []
    paused = True
    while paused: 
        if kb.getKeys('return'): 
            paused = False
        else: 
            for r in rects: 
                if mouse.isPressedIn(r, buttons = [0]): 
                    gabors_click = []
                    gabors_click.append(r.pos)
                    mouse_clicks.append((r.pos[0], r.pos[1]))
                    for index, c in enumerate(gabors_click): 
                        gabor_click_index = visual.GratingStim(win, tex = 'sin', mask = 'gauss', texRes=256, units = 'pix', opacity = 1,
                                    size = 60, sf = .13, ori = 0, contrast = 1, name = 'gabor1', pos = c)
                        gabors_appear.append(gabor_click_index)
                        for g in gabors_appear: 
                            g.autoDraw = True; win.flip()
                            if mouse.isPressedIn(g, buttons = [2]): # doesn't work yet... but doesn't interfere...
                                gabors_appear.pop(g)
                                g.autoDraw = False; win.flip()
    for g in gabors_appear: 
        g.autoDraw = False
    for r in rects: 
        r.autoDraw = False
    win.flip()
    core.wait(1)```

Hi @lilih ,

Not sure if this is what you are after but perhaps you can have the shapes drawn but not shown until a left click occurs - you can do this by playing around with the Opacity of the shape. I attach a minimal example.
Thanks
Yiannis

shapes.psyexp (24.8 KB)

Dear Lilih,

There’s a few problems with the code. One is that it only for a right click the first frame the object is made (based on your indentation). Then your left click code allows for multiple applications of the gabor when you press the same square, which probably isn’t intended.

This version seemed to fix the problem.

gab_fix.py (2.8 KB)

Issac

Thank you so much!! You’re a genius! It totally worked!