{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "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": [ "from vpython import *\n", "# This version uses VPython widgets: button, radio button, checkbox, slider, menu\n", "# See ButtonsSlidersMenus1 for a version that uses Jupyter notebook widgets: button, slider, menu\n", "scene.width = 350\n", "scene.height = 300\n", "scene.range = 1.3\n", "scene.title = \"Widgets (buttons, etc.)\\n\"\n", "\n", "running = True\n", "\n", "def Run(b):\n", " global running\n", " running = not running\n", " if running: b.text = \"Pause\"\n", " else: b.text = \"Run\"\n", " \n", "button(text=\"Pause\", pos=scene.title_anchor, bind=Run)\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", "sphere(radius=0.3)\n", "\n", "col = color.cyan\n", "currentobject = box_object\n", "currentobject.color = col\n", "\n", "def Color(c):\n", " global col\n", " if col.equals(color.cyan): # change to red\n", " currentobject.color = col = color.red\n", " cbutton.text = \"Cyan\"\n", " cbutton.textcolor = color.cyan\n", " cbutton.background = color.red\n", " r1.checked = False\n", " r2.checked = True\n", " else: # change to cyan\n", " currentobject.color = col = color.cyan\n", " cbutton.text = \"Red\"\n", " cbutton.textcolor = color.red\n", " cbutton.background = color.cyan\n", " r1.checked = True\n", " r2.checked = False\n", " \n", "cbutton = button(text='Red', textcolor=color.red, background=color.cyan, pos=scene.title_anchor, bind=Color)\n", "\n", "scene.caption = \"Vary the rotation speed: \\n\"\n", "speed = 150\n", "def setspeed(s):\n", " global speed\n", " speed = s.value\n", " wt.text = '{}'.format(s.value)\n", " \n", "sl = slider(min=20, max=500, value=250, length=280, bind=setspeed, right=15)\n", "\n", "wt = wtext(text=sl.value)\n", "\n", "scene.append_to_caption('\\n')\n", "\n", "r1 = radio(bind=Color, checked=True, text='Cyan')\n", "\n", "scene.append_to_caption(' ')\n", "\n", "def M(m):\n", " global col, currentobject\n", " op = currentobject.opacity\n", " currentaxis = currentobject.axis\n", " currentobject.visible = False\n", " val = m.selected\n", " if val == \"box\": \n", " currentobject = box_object\n", " elif val == \"cone\": \n", " currentobject = cone_object\n", " elif val == \"pyramid\": \n", " currentobject = pyramid_object\n", " elif val == \"cylinder\": \n", " currentobject = cylinder_object\n", " currentobject.color = col\n", " currentobject.axis = currentaxis\n", " currentobject.visible = True\n", " currentobject.opacity = op\n", "\n", "menu(choices=['Choose an object', 'box', 'cone', 'pyramid', 'cylinder'], index=0, bind=M)\n", "\n", "scene.append_to_caption('\\n')\n", "\n", "r2 = radio(bind=Color, text='Red')\n", "\n", "scene.append_to_caption(' ')\n", "\n", "def transparency(b):\n", " if b.checked:\n", " currentobject.opacity = 0.5\n", " else:\n", " currentobject.opacity = 1\n", "\n", "checkbox(bind=transparency, text='Transparent')\n", "\n", "while True:\n", " rate(100)\n", " if running:\n", " currentobject.rotate(angle=speed*1e-4, axis=vector(0,1,0))\n", "\n" ] }, { "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.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }