Event in VPython 7

67 views
Skip to first unread message

ali lolo

unread,
Mar 12, 2018, 11:03:48 AM3/12/18
to VPython-users
from vpython import *
scene = canvas(title='Rubic solver!',
     width=800, height=600,
     center=vector(5,0,0), background=color.white,userzoom=0,userspin=0,userpan=0)
box()
ev = scene.waitfor('mousedown mouseup')
print(ev)
if ev.event == 'mousedown':
    print('You pressed the mouse button')
else:
    print('You released the mouse button')
print(ev.pos) # the position of the mouse
this is my code from Vpython .
but when i print "ev" the out put is "none" and worse than that if i put "keydown" instead of  "mousedown mouseup" in wait for command , it doesn't work when I press a key!!
can you explain me what the problem is?!

Bruce Sherwood

unread,
Mar 12, 2018, 11:41:15 AM3/12/18
to VPython-users
How do you launch your program? 

Below is a slightly modified version of your program that looks for either mousedown or keydown and uses both print and label to output the information. When launching from a terminal or from Spyder, both mousedown and keydown events work as expected. When launching from Jupyter notebook mousedown events work as expected, but keydown events are ignored because the notebook itself captures keypresses as part of the user interface. From the Help on keyboard events:

Jupyter notebook limitation: Key events are swallowed by the Jupyter notebook as part of the user interface and are not available to a VPython program.

from vpython import *
scene = canvas(title='Rubic solver!',
     width=600, height=200,
     center=vector(0,0,0), background=color.white,userzoom=0,userspin=0,userpan=0)
box()
ev = scene.waitfor('mousedown keydown')
print(ev)
if ev.event == 'mousedown':
    label(text='You pressed at location {}'.format(ev.pos))
    print('You pressed at location {}'.format(ev.pos))
else:
    label(text='You pressed {}'.format(ev.key))
    print('You pressed {}'.format(ev.key))

ali lolo

unread,
Mar 13, 2018, 6:58:50 AM3/13/18
to VPython-users
Actually I use IDLE Python to run my program and when i run it ,suddenly  a Local Host page from my browser appear and in that page my program runs. and there are those problems that i mentioned.

Bruce Sherwood

unread,
Mar 13, 2018, 9:23:26 AM3/13/18
to VPython-users
It works correctly for me, running from IDLE. Both mouse and keyboard events are processed correctly.

The fact that a browser starts up is normal; the 3D rendering done by VPython uses the WebGL 3D graphics library that is part of browsers.

Do you see the 3D box? Can you rotate and zoom? Is it only the event machinery that doesn't work?

Are you using Windows, Mac, or Linux? What version of Python are you using? Did you install only Python and the vpython module, or did you install a distribution such as Anaconda?

Bruce

ali lolo

unread,
Mar 13, 2018, 10:07:27 AM3/13/18
to VPython-users
Do you see the 3D box?  yes
Can you rotate and zoom? no but it's just because of my code.
Are you using Windows, Mac, or Linux?  windows
What version of Python are you using? 3.6
Did you install only Python and the vpython module, or did you install a distribution such as Anaconda? no!
Still I have this problem ! please help me!

Bruce Sherwood

unread,
Mar 13, 2018, 10:30:58 AM3/13/18
to VPython-users
Your problem seems unique, so it is difficult to help. You could try uninstalling vpython and reinstalling, but I doubt that will solve the problem. Something that probably would work is to uninstall Python and install Anaconda, as described on the first page of vpython.org.

I'll mention that your code makes no difference in whether you should be able to rotate and zoom. Built into VPython are the following mouse controls:

Right button drag or Ctrl-drag to rotate "camera" to view scene. To zoom, drag with middle button or Alt/Option depressed, or use scroll wheel. On a two-button mouse, middle is left + right. Shift-drag to pan left/right and up/down. Touch screen: pinch/extend to zoom, swipe or two-finger rotate.
Reply all
Reply to author
Forward
0 new messages