{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "require.undef(\"nbextensions/vpython_libraries/glow.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/vpython_libraries/glowcomm\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require.undef(\"nbextensions/vpython_libraries/jquery-ui.custom.min\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/vpython_libraries/glow.min\"], function(){console.log(\"GLOW LOADED\");})" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/vpython_libraries/glowcomm\"], function(){console.log(\"GLOWCOMM LOADED\");})" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "require([\"nbextensions/vpython_libraries/jquery-ui.custom.min\"], function(){console.log(\"JQUERY LOADED\");})" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import ipywidgets as wd\n", "from vpython import *\n", "# This version uses Jupyter notebook widgets: button, slider, menu\n", "# See ButtonsSlidersMenus2 for a version that uses VPython button, slider, menu, radio button, checkbox\n", "\n", "scene.width = 350\n", "scene.height = 300\n", "scene.range = 1.5\n", "scene.title = \"Buttons, Sliders, and Drop-down Menus\"\n", "\n", "box_object = box(visible=True)\n", "cone_object = cone(visible=False, radius=0.5)\n", "pyramid_object = pyramid(visible=False)\n", "cylinder_object = cylinder(visible=False, radius=0.5)\n", "objects = {'box':box_object, 'cone':cone_object, 'pyramid':pyramid_object, 'cylinder':cylinder_object}\n", "\n", "currentobject = box_object\n", "currentobject.color = color.cyan\n", "\n", "b = wd.Button(description='Pause')\n", "c = wd.Button(description='Red')\n", "m = wd.Dropdown(options=['box', 'cone', 'cylinder', 'pyramid'], value='box',\n", " description='Object:')\n", "container = wd.HBox(children=[b,c,m])\n", "display(container)\n", "sl = wd.FloatSlider(description='Rotation speed:', min=20, max=500, step=1, value=250)\n", "display(sl)\n", "\n", "running = True\n", "\n", "def b_handler(s):\n", " global running\n", " running = not running\n", " if s.description == 'Run': s.description = 'Pause'\n", " else: s.description = 'Run'\n", "b.on_click(b_handler)\n", "\n", "def c_handler(s):\n", " if s.description == 'Red':\n", " s.description = 'Cyan'\n", " currentobject.color = color.red\n", " else:\n", " s.description = 'Red'\n", " currentobject.color = color.cyan\n", "c.on_click(c_handler)\n", "\n", "def m_handler(s):\n", " global currentobject\n", " col = currentobject.color\n", " currentobject.visible = False\n", " currentobject = objects[s['new']]\n", " currentobject.color = col\n", " currentobject.visible = True\n", "m.observe(m_handler, names='value')\n", "\n", "while True:\n", " rate(100)\n", " if running:\n", " currentobject.rotate(angle=1e-4*sl.value, axis=vec(0,1,0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "VPython", "language": "python", "name": "vpython" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }