2017-01-21 36 views
0

我有几个学生正在学习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) 
+0

欢迎来到Stack Overflow。如果您遵守这些准则,人们将通过回答您的问题提供帮助:http://stackoverflow.com/help/how-to-ask您的问题目前呈现一小段代码并询问为什么它不起作用,因为您可以想象,如果没有你的帮助,告诉我们去哪里看,你试过了什么,以及你得到了什么样的错误(如果有的话) – Mikkel

回答