Home

If you're new to Python
and VPython: Introduction

A VPython tutorial

Pictures of 3D objects

What's new in VPython 6

VPython web site
VPython license
Python web site
Math module (sqrt etc.)
Numpy module (arrays)

 

Click Example

There are two ways to handle mouse clicks, "polling" and "callbacks". Polling is the older way of handling mouse events, and it still works. Starting with VPython 6, there is another scheme involving callbacks. For more information about these two methods, see general mouse documentation. We show here two programs that achieve the same result by the two different methods. You can use either method, but you cannot mix polling and callback methods in the same program.

 

Handling mouse clicks with callbacks

This program displays a box, then repeatedly waits for a mouse left click and displays a cyan sphere at the mouse location. A mouse click is defined as pressing and releasing the left mouse button with no motion of the mouse, so the sphere appears when you release the mouse button.

from visual import *
scene.range = 4
box() # display a box for context

def showSphere(evt):
    loc = evt.pos
    sphere(pos=loc, radius=0.2, color=color.cyan)

scene.bind('click', showSphere)

Copy this program into an edit window and run the program. Click outside the box and a cyan sphere appears where you click. If you click inside the box, nothing seems to happen. This is because the mouse click is in the xy plane, and the sphere is buried inside the box. If you rotate the scene and then click, you'll see that the spheres go into the new plane parallel to the screen and passing through scene.center.

If you want all of the spheres to go into the xy plane, perpendicular to the z axis, change the latter part of the program like this:

    loc = evt.project(normal=(0,0,1))
    # loc is None if no intersection with plane
    if loc:
        sphere(pos=loc,radius=0.2,color=color.cyan)

 

Handling mouse clicks with polling

This program displays a box, then repeatedly waits for a mouse left click and displays a cyan sphere at the mouse location. A mouse click is defined as pressing and releasing the left mouse button with no motion of the mouse, so the sphere appears when you release the mouse button.

from visual import *
scene.range = 4
box() # display a box for context
while True:
    rate(30)
    if scene.mouse.clicked:
        m = scene.mouse.getclick()
        loc = m.pos
        sphere(pos=loc, radius=0.2, color=color.cyan)

Copy this program into an edit window and run the program. Click outside the box and a cyan sphere appears where you click. If you click inside the box, nothing seems to happen. This is because the mouse click is in the xy plane, and the sphere is buried inside the box. If you rotate the scene and then click, you'll see that the spheres go into the new plane parallel to the screen and passing through scene.center.

If you want all of the spheres to go into the xy plane, perpendicular to the z axis, change the latter part of the program like this:

        loc = m.project(normal=(0,0,1))
        # loc is None if no intersection with plane
        if loc:
            sphere(pos=loc,radius=0.2,color=color.cyan)