Skip to content

Commit 36c36ef

Browse files
Merge pull request #218 from vpython/update_demos
Update ButtonsSlidersMenus
2 parents 7ce1bb9 + 5598e7b commit 36c36ef

File tree

2 files changed

+150
-52
lines changed

2 files changed

+150
-52
lines changed

Demos/ButtonsSlidersMenus2.ipynb

Lines changed: 116 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
{
44
"cell_type": "code",
55
"execution_count": null,
6-
"metadata": {
7-
"collapsed": false
8-
},
6+
"metadata": {},
97
"outputs": [
108
{
119
"data": {
@@ -22,7 +20,79 @@
2220
{
2321
"data": {
2422
"application/javascript": [
25-
"window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")}"
23+
"if (typeof Jupyter !== \"undefined\") { window.__context = { glowscript_container: $(\"#glowscript\").removeAttr(\"id\")};}else{ element.textContent = ' ';}"
24+
],
25+
"text/plain": [
26+
"<IPython.core.display.Javascript object>"
27+
]
28+
},
29+
"metadata": {},
30+
"output_type": "display_data"
31+
},
32+
{
33+
"data": {
34+
"application/javascript": [
35+
"if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/glow.min\");}else{element.textContent = ' ';}"
36+
],
37+
"text/plain": [
38+
"<IPython.core.display.Javascript object>"
39+
]
40+
},
41+
"metadata": {},
42+
"output_type": "display_data"
43+
},
44+
{
45+
"data": {
46+
"application/javascript": [
47+
"if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/glowcomm\");}else{element.textContent = ' ';}"
48+
],
49+
"text/plain": [
50+
"<IPython.core.display.Javascript object>"
51+
]
52+
},
53+
"metadata": {},
54+
"output_type": "display_data"
55+
},
56+
{
57+
"data": {
58+
"application/javascript": [
59+
"if (typeof Jupyter !== \"undefined\") {require.undef(\"nbextensions/vpython_libraries/jquery-ui.custom.min\");}else{element.textContent = ' ';}"
60+
],
61+
"text/plain": [
62+
"<IPython.core.display.Javascript object>"
63+
]
64+
},
65+
"metadata": {},
66+
"output_type": "display_data"
67+
},
68+
{
69+
"data": {
70+
"application/javascript": [
71+
"if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/glow.min\"], function(){console.log(\"GLOW LOADED\");});}else{element.textContent = ' ';}"
72+
],
73+
"text/plain": [
74+
"<IPython.core.display.Javascript object>"
75+
]
76+
},
77+
"metadata": {},
78+
"output_type": "display_data"
79+
},
80+
{
81+
"data": {
82+
"application/javascript": [
83+
"if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/glowcomm\"], function(){console.log(\"GLOWCOMM LOADED\");});}else{element.textContent = ' ';}"
84+
],
85+
"text/plain": [
86+
"<IPython.core.display.Javascript object>"
87+
]
88+
},
89+
"metadata": {},
90+
"output_type": "display_data"
91+
},
92+
{
93+
"data": {
94+
"application/javascript": [
95+
"if (typeof Jupyter !== \"undefined\") {require([\"nbextensions/vpython_libraries/jquery-ui.custom.min\"], function(){console.log(\"JQUERY LOADED\");});}else{element.textContent = ' ';}"
2696
],
2797
"text/plain": [
2898
"<IPython.core.display.Javascript object>"
@@ -34,9 +104,6 @@
34104
],
35105
"source": [
36106
"from vpython import *\n",
37-
"scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable\n",
38-
"# This version uses VPython widgets: button, radio button, checkbox, slider, menu\n",
39-
"# See ButtonsSlidersMenus1 for a version that uses Jupyter notebook widgets: button, slider, menu\n",
40107
"scene.width = 350\n",
41108
"scene.height = 300\n",
42109
"scene.range = 1.3\n",
@@ -56,7 +123,6 @@
56123
"cone_object = cone(visible=False, radius=0.5)\n",
57124
"pyramid_object = pyramid(visible=False)\n",
58125
"cylinder_object = cylinder(visible=False, radius=0.5)\n",
59-
"sphere(radius=0.3)\n",
60126
"\n",
61127
"col = color.cyan\n",
62128
"currentobject = box_object\n",
@@ -66,32 +132,49 @@
66132
" global col\n",
67133
" if col.equals(color.cyan): # change to red\n",
68134
" currentobject.color = col = color.red\n",
135+
" c.text = \"<b>Cyan</b>\"\n",
136+
" c.color = color.cyan\n",
137+
" c.background = color.red\n",
138+
" if c.name is None: # this is the top button\n",
139+
" r1.checked = False\n",
140+
" r2.checked = True\n",
141+
" else: # change to cyan\n",
142+
" currentobject.color = col = color.cyan\n",
143+
" c.text = \"<b>Red</b>\"\n",
144+
" c.color = color.red\n",
145+
" c.background = color.cyan\n",
146+
" if c.name is None: # this is the top button\n",
147+
" r1.checked = True\n",
148+
" r2.checked = False\n",
149+
" \n",
150+
"def cc(c):\n",
151+
" global col\n",
152+
" if col.equals(color.cyan): # change to red:\n",
153+
" currentobject.color = col = color.red\n",
69154
" cbutton.text = \"<b>Cyan</b>\"\n",
70-
" cbutton.textcolor = color.cyan\n",
155+
" cbutton.color = color.cyan\n",
71156
" cbutton.background = color.red\n",
72-
" r1.checked = False\n",
73-
" r2.checked = True\n",
74157
" else: # change to cyan\n",
75158
" currentobject.color = col = color.cyan\n",
76159
" cbutton.text = \"<b>Red</b>\"\n",
77-
" cbutton.textcolor = color.red\n",
160+
" cbutton.color = color.red\n",
78161
" cbutton.background = color.cyan\n",
79-
" r1.checked = True\n",
80-
" r2.checked = False\n",
81162
" \n",
82-
"cbutton = button(text='<b>Red</b>', textcolor=color.red, background=color.cyan, pos=scene.title_anchor, bind=Color)\n",
163+
"cbutton = button(text='<b>Red</b>', color=color.red, background=color.cyan, \n",
164+
" pos=scene.title_anchor, bind=Color, name=None)\n",
165+
"\n",
166+
"scene.caption = \"Vary the rotation speed: \\n\\n\"\n",
83167
"\n",
84-
"scene.caption = \"Vary the rotation speed: \\n\"\n",
85-
"speed = 150\n",
86168
"def setspeed(s):\n",
87-
" global speed\n",
88-
" speed = s.value\n",
169+
" wt.text = '{:1.2f}'.format(s.value)\n",
89170
" \n",
90-
"slider(min=20, max=500, value=250, length=350, bind=setspeed)\n",
171+
"sl = slider(min=0.3, max=3, value=1.5, length=220, bind=setspeed, right=15)\n",
91172
"\n",
92-
"scene.append_to_caption('\\n')\n",
173+
"wt = wtext(text='{:1.2f}'.format(sl.value))\n",
93174
"\n",
94-
"r1 = radio(bind=Color, checked=True, text='Cyan ')\n",
175+
"scene.append_to_caption(' radians/s\\n')\n",
176+
"\n",
177+
"r1 = radio(bind=cc, checked=True, text='Cyan', name='rads')\n",
95178
"\n",
96179
"scene.append_to_caption(' ')\n",
97180
"\n",
@@ -118,7 +201,7 @@
118201
"\n",
119202
"scene.append_to_caption('\\n')\n",
120203
"\n",
121-
"r2 = radio(bind=Color, text='Red ')\n",
204+
"r2 = radio(bind=cc, text='Red', name='rads')\n",
122205
"\n",
123206
"scene.append_to_caption(' ')\n",
124207
"\n",
@@ -128,19 +211,23 @@
128211
" else:\n",
129212
" currentobject.opacity = 1\n",
130213
"\n",
131-
"trans = checkbox(bind=transparency, text='Transparent')\n",
214+
"checkbox(bind=transparency, text='Transparent')\n",
132215
"\n",
216+
"dt = 0.01\n",
133217
"while True:\n",
134-
" rate(100)\n",
218+
" rate(1/dt)\n",
135219
" if running:\n",
136-
" currentobject.rotate(angle=speed*1e-4, axis=vector(0,1,0))\n"
220+
" currentobject.rotate(angle=sl.value*dt, axis=vector(0,1,0))\n"
137221
]
138222
},
139223
{
140224
"cell_type": "code",
141225
"execution_count": null,
142226
"metadata": {
143-
"collapsed": true
227+
"collapsed": true,
228+
"jupyter": {
229+
"outputs_hidden": true
230+
}
144231
},
145232
"outputs": [],
146233
"source": []
@@ -163,9 +250,9 @@
163250
"name": "python",
164251
"nbconvert_exporter": "python",
165252
"pygments_lexer": "ipython3",
166-
"version": "3.6.6"
253+
"version": "3.9.7"
167254
}
168255
},
169256
"nbformat": 4,
170-
"nbformat_minor": 2
257+
"nbformat_minor": 4
171258
}

Demos_no_notebook/ButtonsSlidersMenus.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
from vpython import *
2-
# This version uses VPython widgets: button, radio button, checkbox, slider, menu
3-
# See ButtonsSlidersMenus1 for a version that uses Jupyter notebook widgets: button, slider, menu
42
scene.width = 350
53
scene.height = 300
64
scene.range = 1.3
@@ -20,7 +18,6 @@ def Run(b):
2018
cone_object = cone(visible=False, radius=0.5)
2119
pyramid_object = pyramid(visible=False)
2220
cylinder_object = cylinder(visible=False, radius=0.5)
23-
sphere(radius=0.3)
2421

2522
col = color.cyan
2623
currentobject = box_object
@@ -29,36 +26,50 @@ def Run(b):
2926
def Color(c):
3027
global col
3128
if col.equals(color.cyan): # change to red
29+
currentobject.color = col = color.red
30+
c.text = "<b>Cyan</b>"
31+
c.color = color.cyan
32+
c.background = color.red
33+
if c.name is None: # this is the top button
34+
r1.checked = False
35+
r2.checked = True
36+
else: # change to cyan
37+
currentobject.color = col = color.cyan
38+
c.text = "<b>Red</b>"
39+
c.color = color.red
40+
c.background = color.cyan
41+
if c.name is None: # this is the top button
42+
r1.checked = True
43+
r2.checked = False
44+
45+
def cc(c):
46+
global col
47+
if col.equals(color.cyan): # change to red:
3248
currentobject.color = col = color.red
3349
cbutton.text = "<b>Cyan</b>"
34-
cbutton.textcolor = color.cyan
50+
cbutton.color = color.cyan
3551
cbutton.background = color.red
36-
r1.checked = False
37-
r2.checked = True
3852
else: # change to cyan
3953
currentobject.color = col = color.cyan
4054
cbutton.text = "<b>Red</b>"
41-
cbutton.textcolor = color.red
55+
cbutton.color = color.red
4256
cbutton.background = color.cyan
43-
r1.checked = True
44-
r2.checked = False
4557

46-
cbutton = button(text='<b>Red</b>', textcolor=color.red, background=color.cyan, pos=scene.title_anchor, bind=Color)
58+
cbutton = button(text='<b>Red</b>', color=color.red, background=color.cyan,
59+
pos=scene.title_anchor, bind=Color, name=None)
60+
61+
scene.caption = "Vary the rotation speed: \n\n"
4762

48-
scene.caption = "Vary the rotation speed:\n"
49-
speed = 150
5063
def setspeed(s):
51-
global speed
52-
speed = s.value
53-
wt.text = '{:1.0f}'.format(s.value)
64+
wt.text = '{:1.2f}'.format(s.value)
5465

55-
sl = slider(min=20, max=500, value=250, length=280, bind=setspeed, right=15)
66+
sl = slider(min=0.3, max=3, value=1.5, length=220, bind=setspeed, right=15)
5667

57-
wt = wtext(text='{:1.0f}'.format(sl.value))
68+
wt = wtext(text='{:1.2f}'.format(sl.value))
5869

59-
scene.append_to_caption('\n')
70+
scene.append_to_caption(' radians/s\n')
6071

61-
r1 = radio(bind=Color, checked=True, text='Cyan')
72+
r1 = radio(bind=cc, checked=True, text='Cyan', name='rads')
6273

6374
scene.append_to_caption(' ')
6475

@@ -85,7 +96,7 @@ def M(m):
8596

8697
scene.append_to_caption('\n')
8798

88-
r2 = radio(bind=Color, text='Red')
99+
r2 = radio(bind=cc, text='Red', name='rads')
89100

90101
scene.append_to_caption(' ')
91102

@@ -97,8 +108,8 @@ def transparency(b):
97108

98109
checkbox(bind=transparency, text='Transparent')
99110

111+
dt = 0.01
100112
while True:
101-
rate(100)
113+
rate(1/dt)
102114
if running:
103-
currentobject.rotate(angle=speed*1e-4, axis=vector(0,1,0))
104-
115+
currentobject.rotate(angle=sl.value*dt, axis=vector(0,1,0))

0 commit comments

Comments
 (0)