Utilizza button, checkbox, menu, radio
from vpython import *
#----------------------------------------------------------------- Oggetti 3D
box_object = box (visible=True)
cone_object = cone (visible=False, radius=0.5)
pyramid_object = pyramid (visible=False)
cylinder_object = cylinder(visible=False, radius=0.5)
#----------------------------------------------------------------- Proprietà degli oggetti
scene.width = 500
scene.height = 500
running = True
col = color.cyan
current_object = box_object
current_object.color = col
#----------------------------------------------------------------- Binding
def bind_run(evt):
global running
running = not running
if(running): evt.text = "Pause"
else: evt.text = "Run"
def bind_color_1(evt):
global col
if(col == color.cyan): # cyan --> red
current_object.color = col = color.red
evt.text = "<b>Cyan</b>"
evt.color = color.cyan
evt.background = color.red
r1.checked = False
r2.checked = True
else: # red --> cyan
current_object.color = col = color.cyan
evt.text = "<b>Red</b>"
evt.color = color.red
evt.background = color.cyan
r1.checked = True
r2.checked = False
def bind_color_2(evt):
global col
if(col == color.cyan): # cyan --> red
current_object.color = col = color.red
cbutton.text = "<b>Cyan</b>"
cbutton.color = color.cyan
cbutton.background = color.red
else: # red --> cyan
current_object.color = col = color.cyan
cbutton.text = "<b>Red</b>"
cbutton.color = color.red
cbutton.background = color.cyan
def bind_setspeed(evt):
wt.text = '{:1.2f}'.format(evt.value)
def bind_menu(evt):
global col, current_object
op = current_object.opacity
current_axis = current_object.axis
current_object.visible = False
val = evt.selected
if(val == "box") : current_object = box_object
elif(val == "cone") : current_object = cone_object
elif(val == "cylinder"): current_object = cylinder_object
elif(val == "pyramid") : current_object = pyramid_object
current_object.color = col
current_object.axis = current_axis
current_object.visible = True
current_object.opacity = op
def bind_transparency(evt):
if(evt.checked):
current_object.opacity = 0.5
else:
current_object.opacity = 1
#----------------------------------------------------------------- Interfaccia
scene.title = "Widgets (button, checkbox, menu, radio, slider)\n\n"
button(bind = bind_run,
text = "Pause",
pos = scene.title_anchor)
scene.append_to_title(' ')
cbutton = button(background = color.cyan,
bind = bind_color_1,
color = color.red,
name = None,
pos = scene.title_anchor,
text = '<b>Red</b>')
scene.append_to_title('\n\n')
scene.append_to_caption('\n\nObject: ')
menu(bind = bind_menu,
choices = ['box', 'cone', 'cylinder', 'pyramid'],
index = 0)
scene.append_to_caption('\n\nColor: ')
r1 = radio(bind = bind_color2,
checked = True,
name = 'rads',
text = 'Cyan')
r2 = radio(bind = bind_color2,
name = 'rads',
text = 'Red')
scene.append_to_caption('\n\nTransparency: ')
checkbox(bind = bind_transparency,
text = '')
scene.append_to_caption("\n\nSpeed: ")
sl = slider(bind = bind_setspeed,
max = 2.0,
min = 0.25,
length = 200,
right = 10,
value = 1.0)
wt = wtext(text = '{:1.2f}'.format(sl.value))
scene.append_to_caption(' rad/s')
#----------------------------------------------------------------- MAIN
dt = 0.01
while(True):
rate(50)
if(running):
current_object.rotate(angle = sl.value*dt,
axis = vector(0, 1, 0))