diff --git a/Demos/ButtonsSlidersMenus2.ipynb b/Demos/ButtonsSlidersMenus2.ipynb index 51c8c9b5..c79fbfb1 100644 --- a/Demos/ButtonsSlidersMenus2.ipynb +++ b/Demos/ButtonsSlidersMenus2.ipynb @@ -3,9 +3,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -22,7 +20,79 @@ { "data": { "application/javascript": [ - "window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")}" + "if (typeof Jupyter !== \"undefined\") { window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")};}else{ element.textContent = ' ';}" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/glow.min\");}else{element.textContent = ' ';}" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/glowcomm\");}else{element.textContent = ' ';}" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/jquery-ui.custom.min\");}else{element.textContent = ' ';}" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/glow.min\"], function(){console.log(\"GLOW LOADED\");});}else{element.textContent = ' ';}" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/glowcomm\"], function(){console.log(\"GLOWCOMM LOADED\");});}else{element.textContent = ' ';}" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/jquery-ui.custom.min\"], function(){console.log(\"JQUERY LOADED\");});}else{element.textContent = ' ';}" ], "text/plain": [ "" @@ -34,9 +104,6 @@ ], "source": [ "from vpython import *\n", - "scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable\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", @@ -56,7 +123,6 @@ "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", @@ -66,32 +132,49 @@ " global col\n", " if col.equals(color.cyan): # change to red\n", " currentobject.color = col = color.red\n", + " c.text = \"Cyan\"\n", + " c.color = color.cyan\n", + " c.background = color.red\n", + " if c.name is None: # this is the top button\n", + " r1.checked = False\n", + " r2.checked = True\n", + " else: # change to cyan\n", + " currentobject.color = col = color.cyan\n", + " c.text = \"Red\"\n", + " c.color = color.red\n", + " c.background = color.cyan\n", + " if c.name is None: # this is the top button\n", + " r1.checked = True\n", + " r2.checked = False\n", + " \n", + "def cc(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.color = 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.color = 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", + "cbutton = button(text='Red', color=color.red, background=color.cyan, \n", + " pos=scene.title_anchor, bind=Color, name=None)\n", + "\n", + "scene.caption = \"Vary the rotation speed: \\n\\n\"\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 = '{:1.2f}'.format(s.value)\n", " \n", - "slider(min=20, max=500, value=250, length=350, bind=setspeed)\n", + "sl = slider(min=0.3, max=3, value=1.5, length=220, bind=setspeed, right=15)\n", "\n", - "scene.append_to_caption('\\n')\n", + "wt = wtext(text='{:1.2f}'.format(sl.value))\n", "\n", - "r1 = radio(bind=Color, checked=True, text='Cyan ')\n", + "scene.append_to_caption(' radians/s\\n')\n", + "\n", + "r1 = radio(bind=cc, checked=True, text='Cyan', name='rads')\n", "\n", "scene.append_to_caption(' ')\n", "\n", @@ -118,7 +201,7 @@ "\n", "scene.append_to_caption('\\n')\n", "\n", - "r2 = radio(bind=Color, text='Red ')\n", + "r2 = radio(bind=cc, text='Red', name='rads')\n", "\n", "scene.append_to_caption(' ')\n", "\n", @@ -128,19 +211,23 @@ " else:\n", " currentobject.opacity = 1\n", "\n", - "trans = checkbox(bind=transparency, text='Transparent')\n", + "checkbox(bind=transparency, text='Transparent')\n", "\n", + "dt = 0.01\n", "while True:\n", - " rate(100)\n", + " rate(1/dt)\n", " if running:\n", - " currentobject.rotate(angle=speed*1e-4, axis=vector(0,1,0))\n" + " currentobject.rotate(angle=sl.value*dt, axis=vector(0,1,0))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } }, "outputs": [], "source": [] @@ -163,9 +250,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.7" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/Demos_no_notebook/ButtonsSlidersMenus.py b/Demos_no_notebook/ButtonsSlidersMenus.py index 8e61aee4..960521b5 100644 --- a/Demos_no_notebook/ButtonsSlidersMenus.py +++ b/Demos_no_notebook/ButtonsSlidersMenus.py @@ -1,6 +1,4 @@ from vpython import * -# This version uses VPython widgets: button, radio button, checkbox, slider, menu -# See ButtonsSlidersMenus1 for a version that uses Jupyter notebook widgets: button, slider, menu scene.width = 350 scene.height = 300 scene.range = 1.3 @@ -20,7 +18,6 @@ def Run(b): cone_object = cone(visible=False, radius=0.5) pyramid_object = pyramid(visible=False) cylinder_object = cylinder(visible=False, radius=0.5) -sphere(radius=0.3) col = color.cyan currentobject = box_object @@ -29,36 +26,50 @@ def Run(b): def Color(c): global col if col.equals(color.cyan): # change to red + currentobject.color = col = color.red + c.text = "Cyan" + c.color = color.cyan + c.background = color.red + if c.name is None: # this is the top button + r1.checked = False + r2.checked = True + else: # change to cyan + currentobject.color = col = color.cyan + c.text = "Red" + c.color = color.red + c.background = color.cyan + if c.name is None: # this is the top button + r1.checked = True + r2.checked = False + +def cc(c): + global col + if col.equals(color.cyan): # change to red: currentobject.color = col = color.red cbutton.text = "Cyan" - cbutton.textcolor = color.cyan + cbutton.color = color.cyan cbutton.background = color.red - r1.checked = False - r2.checked = True else: # change to cyan currentobject.color = col = color.cyan cbutton.text = "Red" - cbutton.textcolor = color.red + cbutton.color = color.red cbutton.background = color.cyan - r1.checked = True - r2.checked = False -cbutton = button(text='Red', textcolor=color.red, background=color.cyan, pos=scene.title_anchor, bind=Color) +cbutton = button(text='Red', color=color.red, background=color.cyan, + pos=scene.title_anchor, bind=Color, name=None) + +scene.caption = "Vary the rotation speed: \n\n" -scene.caption = "Vary the rotation speed:\n" -speed = 150 def setspeed(s): - global speed - speed = s.value - wt.text = '{:1.0f}'.format(s.value) + wt.text = '{:1.2f}'.format(s.value) -sl = slider(min=20, max=500, value=250, length=280, bind=setspeed, right=15) +sl = slider(min=0.3, max=3, value=1.5, length=220, bind=setspeed, right=15) -wt = wtext(text='{:1.0f}'.format(sl.value)) +wt = wtext(text='{:1.2f}'.format(sl.value)) -scene.append_to_caption('\n') +scene.append_to_caption(' radians/s\n') -r1 = radio(bind=Color, checked=True, text='Cyan') +r1 = radio(bind=cc, checked=True, text='Cyan', name='rads') scene.append_to_caption(' ') @@ -85,7 +96,7 @@ def M(m): scene.append_to_caption('\n') -r2 = radio(bind=Color, text='Red') +r2 = radio(bind=cc, text='Red', name='rads') scene.append_to_caption(' ') @@ -97,8 +108,8 @@ def transparency(b): checkbox(bind=transparency, text='Transparent') +dt = 0.01 while True: - rate(100) + rate(1/dt) if running: - currentobject.rotate(angle=speed*1e-4, axis=vector(0,1,0)) - + currentobject.rotate(angle=sl.value*dt, axis=vector(0,1,0))