In this video Lesson, we show how to interact with a Python model using widgets. We show how Vpython allows use of dropdown menus, radio buttons, check boxes and slider bars. These combine to give the user great control over an animated model. In past lessons we changed parameters by editing the program. By incorporating widgets we can allow non-programmers to effectively interact with a model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | from vpython import * from time import * mRadius=.5 wallThickness=.1 roomWidth=12 roomDepth=20 roomHeight=15 floor=box(pos=vector(0,-roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) ceiling=box(pos=vector(0,roomHeight/2,0),size=vector(roomWidth,wallThickness,roomDepth), color=color.white) backWall=box(pos=vector(0,0,-roomDepth/2),size=vector(roomWidth,roomHeight,wallThickness), color=color.white) leftWall=box(pos=vector(-roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) rightWall=box(pos=vector(roomWidth/2,0,0),size=vector(wallThickness,roomHeight,roomDepth), color=color.white) marble=sphere(radius=mRadius,color=color.blue) deltaX=.1 deltaY=.1 deltaZ=.1 run=0 mySpeed=1 xPos=0 yPos=0 zPos=0 def ballColorRed(x): marble.color=color.red button(bind=ballColorRed,text="Red",color=color.black,background=color.red) scene.append_to_caption(' ') def ballColorGreen(x): marble.color=color.green button(bind=ballColorGreen,text="Green",color=color.black,background=color.green) scene.append_to_caption(' ') def ballColorBlue(x): marble.color=color.blue button(bind=ballColorBlue,text="Blue",color=color.black,background=color.blue) scene.append_to_caption(' ') def runRadio(x): global run print(x.checked) if x.checked==True: run=1 if x.checked==False: run=0 radio(bind=runRadio,text='Run') def bigBall(x): global mRadius if x.checked==True: mRadius=mRadius*2 marble.radius=mRadius if x.checked==False: mRadius=mRadius/2 marble.radius=mRadius checkbox(bind=bigBall, text='Big Ball') scene.append_to_caption('\n\n') wtext(text='Ball Opacity') scene.append_to_caption('\n\n') def ballOpacity(x): op=x.value marble.opacity=op slider(bind=ballOpacity,vertical=False,min=0,max=1,step=.05,value=1,text='Opacity') scene.append_to_caption('\n\n') wtext(text='Choose Your Speed') scene.append_to_caption('\n\n') def speed(x): global mySpeed if x.selected=='1': mySpeed=1 if x.selected=='2': mySpeed=2 if x.selected=='3': mySpeed=3 if x.selected=='4': mySpeed=4 if x.selected=='5': mySpeed=5 menu(bind=speed, choices=['1','2','3','4','5']) while True: rate(25*mySpeed) xPos=xPos+deltaX*run yPos=yPos+deltaY*run zPos=zPos+deltaZ*run Xrme=xPos+mRadius Xlme=xPos-mRadius Ytme=yPos+mRadius Ybme=yPos-mRadius Zbme=zPos-mRadius Zfme=zPos+mRadius Rwe=roomWidth/2-wallThickness/2 Lwe=-roomWidth/2+wallThickness/2 Cwe=roomHeight/2-wallThickness/2 FLOORwe=-roomHeight/2+wallThickness/2 Bwe=-roomDepth/2+wallThickness/2 Fwe=roomDepth/2-wallThickness/2 if (Xrme>=Rwe or Xlme<=Lwe): deltaX=deltaX*(-1) if (Ytme>=Cwe or Ybme<=FLOORwe): deltaY=deltaY*(-1) if (Zfme>=Fwe or Zbme<=Bwe): deltaZ=deltaZ*(-1) marble.pos=vector(xPos,yPos,zPos) |