单选按钮可更改显示的形状

时间:2017-01-21 19:04:19

标签: radio-button vpython

我有一些学生正在学习vpython科学。他们希望能够使用单选按钮来更改显示的形状。但是,我一直无法理解为什么形状没有正常变化(它重叠),但这可能是因为我在编辑其他人的代码时非常糟糕。有谁看到这个问题?谢谢! (是的,我知道他们从小部件示例基础开始,但这只是尝试编码此方案)。

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)

1 个答案:

答案 0 :(得分:0)

我不知道该如何对这个特定程序说些什么,但我要指出GlowScript VPython和Jupyter VPython现在有特别容易使用的小部件,包括单选按钮。这是一个例子:

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

点击"编辑此程序"查看代码,或获取完整文档的帮助。