From: Young-Jin L. <yl...@ui...> - 2002-01-18 02:47:37
|
Hi, I have a question how to repeat the same vpython scripts. I was implementing physics simulation software with vpython and wxpython and I wanted to repeat vpython simulation when a button is clicked. The following is what I tried. This is not runnable because I was using wxPython, not a Tkinter. But I don't think you would have problem understanding my intention. When I first clicked the "Start Simulation", it worked as I expected. But when I closed the VPython window and clicked again the "Start Simulation", I didn't see anything. But 'start simulation' and 'end simulation' debug straings appeared in the command window. Any ideas what I did wrong? Thanks in advance. Young-Jin Lee # initial review of 1d free fall example # Button ID BT_START = 10 from wxPython.wx import * from wxPython.html import wxHtmlWindow from visual import * class CPFreeFall1D_InitialPane( wxHtmlWindow ): def __init__( self, parent, id, **kwargs ): wxHtmlWindow.__init__( self, parent, id, **kwargs ) wxButton( self, BT_START, "Start Simulation", wxPoint( 100, 100 ) ) EVT_BUTTON( self, BT_START, self.OnClickStart ) def OnClickStart( self, event ): # read the configuration file import ConfigParser cfg = ConfigParser.ConfigParser() cfg.read( 'freefall1d.cfg' ) self.startSimulation( cfg ) # When "Start Simulation" button is clicked, VPython simulatoin starts. def startSimulation( self, cfg ): # Body of VPython simulation print 'start simulation' scene.width = scene.height = 500. scene.x = scene.y = 0 scene.forward = ( -0.2, -0.5, -1 ) scene.autoscale = 0 scene.userzoom = 0 scene.range = ( 15, 15, 15 ) scene.exit = 0 # only close the simulation window # start an actual simulation scene.select() xmax = 10 dx = xmax / 5 grid = [] gravity = cfg.get( 'Characteristics', 'gravity' ) r = float( cfg.get( 'Characteristics', 'diameter' ) ) / 2 for x in arange( -xmax, xmax+dx, dx ): grid.append( curve( pos = [(x, 0, -xmax), (x, 0, xmax)], color = ( .7, .7, .7 ) ) ) for z in arange( -xmax, xmax+dx, dx ): grid.append( curve( pos = [(-xmax, 0, z), (xmax, 0, z)], color = (.7, .7, .7) ) ) ball = sphere(pos=(0,8,0), radius=r, color=color.red) ball.selected = 0 ball.velocity = vector(0,-1,0) dt = 0.1 ballPosition = [] ballVelocity = [] t = [] currentT = 0 while ball.y > ball.radius: rate(10) ball.pos = ball.pos + ball.velocity*dt ball.velocity.y = ball.velocity.y - float(gravity)*dt wake = sphere( pos = ball.pos, radius = 0.1, color = color.blue ) t.append( currentT + dt ) ballPosition.append( ball.pos.y ) ballVelocity.append( ball.velocity.y ) currentT = currentT + dt print 'end simulation' |