psychopy.org | Reference | Downloads | Github

Mouse click on shape

Hello,

For a couple of hours I try to figure out how write a simple straightforward code which says: In case you click on a given rectangular box shown on the screen, another rectangular box appears on a new screen. If you click on the other rectangular object, another box appears on a new screen.

I came up with the following code which however is insufficient:


import sys
import random
from psychopy import visual,event,core,gui



# definitions

win1 = visual.Window([1370,710], color= 'black', units = 'pix')
timer = core.Clock()
mouse= event.Mouse()


def BOX():
    box2 = visual.Rect(win1,lineColor="black",fillColor="LightGreen",size [300,150],pos=[0,-160])
    box2.draw()

    box5 = visual.Rect(win1,lineColor="black",fillColor="Blue",size=[300,150],pos=[0,160])
    box5.draw()

    win1.flip()
    mouse



def keys():
    if mouse.isPressedIn(box2):
        box4 = visual.Rect(win1,lineColor="black",fillColor="LightBlue",size=[100,50],pos=[-200,-90])
        box4.draw()
        win1.flip()
       
    
    elif mouse.isPressedIn(box5) :
        box6 = visual.Rect(win1,lineColor="black",fillColor="Pink",size=[100,50],pos=[220,-90])
        box6.draw()
        win1.flip()
        

BOX()

keys()


Optimally the mouse click code should also work for pad touch screen,
Any piece of advice would be very much appreciated.

Best,
Tiber

Thanks Jon for telling me. I corrected it.

Best,
Tiber

You really need to tell us exactly what is going wrong rather than simply saying it is “insufficient”.

But there are a number of issues here to do with variable scoping and also you need to be implementing a continuous drawing loop so that you keep checking the mouse position. At the moment you are just doing it once, so even if the target variables were in scope, you would b e unlikely to detect any press.

For examples, look at the Coder demos like shapeContains.py and mouse.py