0

I have a few students that are learning vpython for science. They would like to be able to use a radio button to change what shape is displayed. However, I have been unable to see why the shape does not change normally (it overlaps) but that may be because I am horrible at editing other people's code. Anyone see the issue? Thank you! (yes I know they started with the widget example base but that was just to try coding this scenario).

from __future__ import division, print_function
from visual import *
from visual.graph import *
from physutil import *
import wx

def setleft(evt): # this will be used to rotate the box left
    cube.dir = -1

def setright(evt): # this will be used to rotate the box right
    cube.dir = 1

def cuberate(value): #  this creates a function to call upon later
    cube.dtheta = 2*value*pi/1e4

def setrate(evt): # this will be used to create a slider for user control
    value = rotation.GetValue()
    cuberate(value) # value is min-max slider position, 0 to 100

def togglecolor(evt): # this is how you set up radio buttons
    choice = t1.GetSelection()
    if choice == 0: # upper radio button (choice = 0)
        currentobject.color = color.red
    else: # lower radio button (choice = 1)
        currentobject.color = color.cyan

L = 320
Hgraph=400
w = window(width=2*(L+window.dwidth), 
       height=L+window.dheight+window.menuheight+Hgraph,
       menus=True, title='Widgets',
       style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)

d = 20
disp = display(window=w, x=d, y=d, width=L-2*d, height=L-2*d, 
      forward=-vector(0,1,2))
      gdisplay(window=w, y=disp.height+50, width=2*(L+window.dwidth),
      height=Hgraph)

cube = box(color=color.red)

currentobject = cube

def choose(evt):
    selected=t2.GetSelection()
    cube.visible=false
    if selected ==0:
        cube = box(color=color.red)
    elif selected ==1:
        cube = cylinder(radius=0.5, color=color.red)
    elif selected ==2:
        cube = sphere(radius=0.5, color=color.red)
    cube.visible=true

p = w.panel

wx.StaticText(p, pos=(d,4), size=(L-2*d,d), label='A Sample',
          style=wx.ALIGN_CENTRE | wx.ST_NO_AUTORESIZE)

left = wx.Button(p, label='Rotate left', pos=(L+10,15))
left.Bind(wx.EVT_BUTTON, setleft)

right = wx.Button(p, label='Rotate right', pos=(1.5*L+10,15))
right.Bind(wx.EVT_BUTTON, setright)

t1 = wx.RadioBox(p, pos=(1.0*L,0.3*L), size=(0.25*L, 0.25*L),
             choices = ['Red', 'Cyan'], style=wx.RA_SPECIFY_ROWS)
t1.Bind(wx.EVT_RADIOBOX, togglecolor)

t2 = wx.RadioBox(p, pos=(1.5*L,0.3*L), size=(0.25*L, 0.25*L),
             choices = ['Cube', 'Cylinder', 'Sphere'],   
             style=wx.RA_SPECIFY_ROWS)
t2.Bind(wx.EVT_RADIOBOX, choose)

rotation = wx.Slider(p, pos=(1.0*L,0.8*L), size=(0.9*L,20), minValue=0, 
             maxValue=100, style=wx.SL_HORIZONTAL|wx.SL_LABELS)
rotation.Bind(wx.EVT_SCROLL, setrate)
wx.StaticText(p, pos=(1.0*L,0.75*L), label='Set Angular Velocty Value')

rotation.SetValue(70) # update the slider
cuberate(rotation.GetValue()) 
cube.dir = -1 

while True:
    rate(100)
    cube.rotate(axis=(0,1,0), angle=cube.dir*cube.dtheta)
  • Welcome to Stack Overflow. People will help by answering your questions if you conform to these guidelines: http://stackoverflow.com/help/how-to-ask Your question currently presents a slab of code and asks why it doesn't work, which, as you can imagine, is a difficult assignment without your help in telling us where to look, what you have tried, and what errors you get (if any) – Mikkel Jan 21 '17 at 19:55

1 Answers1

0

I don't know what to say about this particular program, but I'll point out that GlowScript VPython and Jupyter VPython now have particularly easy to use widgets, including radio buttons. Here's an example:

http://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/program/ButtonsSlidersMenus-VPython

Click "Edit this program" to see the code, or Help for full documentation.

user1114907
  • 972
  • 8
  • 15