# Mouse click on shape

**URL:** <https://discourse.psychopy.org/t/mouse-click-on-shape/2580>\
**Category:** Coding\
**Created:** [June 6, 2017, 7:10am UTC](https://discourse.psychopy.org/t/mouse-click-on-shape/2580 "2017-06-06T07:10:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiber](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tiber](https://discourse.psychopy.org/u/Tiber)\
**Post date:** [June 6, 2017, 7:10am UTC](https://discourse.psychopy.org/t/mouse-click-on-shape/2580/1 "2017-06-06T07:10:03Z")

</div>

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:

```auto

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

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [June 6, 2017, 11:21am UTC](https://discourse.psychopy.org/t/mouse-click-on-shape/2580/2 "2017-06-06T11:21:10Z")

</div>

> [@Please surround python code with backticks](https://discourse.psychopy.org/t/please-surround-python-code-with-backticks/1775):
>
> When you include python code on this forum please put triple “backticks” like this ``` on the line before and the line after your code. If you’ve done it correctly then your code will appear in a grey box with correct indentation like this (check the preview of your post on the right hand side before posting it) and other people will be better able to work out what might be right/wrong with your code. if myVal==True: break If you’ve already posted something that didn’t follow this rule th…

---

<div class="post-metadata">

**Author:** ![Tiber](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tiber](https://discourse.psychopy.org/u/Tiber)\
**Post date:** [June 6, 2017, 3:38pm UTC](https://discourse.psychopy.org/t/mouse-click-on-shape/2580/3 "2017-06-06T15:38:37Z")

</div>

Thanks Jon for telling me. I corrected it.

Best,  
Tiber

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [June 8, 2017, 5:01am UTC](https://discourse.psychopy.org/t/mouse-click-on-shape/2580/4 "2017-06-08T05:01:10Z")

</div>

> [@Tiber](#):
>
> I came up with the following code which however is insufficient:

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`
