Mouse Events Throwing AttributeError

228 views
Skip to first unread message

Russell Osterlund

unread,
Jul 18, 2017, 10:35:41 AM7/18/17
to VPython-users
Hi!

I am a relative newbie in Python (but have decades experience programming in many languages). When I execute the following code (from the mouse-events tutorial) the code "works", but I see the following in the shell (v3.6.1) window:

Traceback (most recent call last):
  File "D:\Projects\Python\Examples\mouse_events.py", line 7, in <module>
    if scene.mouse.events:
AttributeError: 'Mouse' object has no attribute 'events'

Here is the code generating the error:

from vpython import *
scene.range = 5 # fixed size, no autoscaling
ball = sphere(pos=vector(-3,0,0), color=color.cyan)
cube = box(pos=vector(+3,0,0), size=vector(2,2,2), color=color.red)
pick = None # no object picked out of the scene yet
while True: 
    if scene.mouse.events: 
        m1 = scene.mouse.getevent() # get event
        if m1.drag and m1.pick == ball: # if touched ball
            drag_pos = m1.pickpos # where on the ball
            pick = m1.pick # pick now true (not None) 
        elif m1.drop: # released at end of drag
            pick = None # end dragging (None is false)
    if pick:
        # project onto xy plane, even if scene rotated:
        new_pos = scene.mouse.project(normal=vector(0,0,1)) 
        if new_pos != drag_pos: # if mouse has moved
            # offset for where the ball was clicked:
            pick.pos += new_pos - drag_pos 
            drag_pos = new_pos # update drag position

The behavior currently is unacceptable because when I migrate the relevant pieces to my own program, it halts because of this error. (I am trying to use the mouse or the keyboard to halt a continuously running program.) This from the VPython shell V3.6.1

Thanks in advance.

Bruce Sherwood

unread,
Jul 18, 2017, 12:24:33 PM7/18/17
to VPython-users
You're using Classic VPython (VPython 6) syntax in VPython 7, which won't work. See the documentation at glowscript.org (click Help).

Russell Osterlund

unread,
Jul 18, 2017, 1:05:27 PM7/18/17
to VPython-users
Hi again.

The sample code was copied directly from the following link (from the VPython 6 site):


My Python environment is relatively clean (needed to "pip" VPython to get access to the library).  And I am trying to work without using "cloud-based" platforms. 

Are there any other options?

Thanks.

(PS - I have needed to modify another one of the tutorials - adding "vector" to some of the parameters.)

Bruce Sherwood

unread,
Jul 18, 2017, 2:12:25 PM7/18/17
to VPython-users
The link you give is for the documentation of Classic VPython (VPython 6). As I said, you are using the vpython module (VPython 7), so you need to use the documentation for VPython 7, which is available at glowscript.org by clicking Help.

Reply all
Reply to author
Forward
0 new messages