简体   繁体   中英

Clear all variables and restart VPython

I'm making a simulation about two springs and one mass. I've made it through VPython using some graphics elements. I used the slider function from Vpython to change the variables of radius and mass of the ball in real time. What I want now is create a button that clear all variables and restart the program so I dont have to run it again from console. How can I make that?

This is how it looks

This is the main loop that affects the variables:

while t < 100:
    rate(500)
    ball.velocity = ball.velocity + acc() * dt
    ball.pos = ball.pos + ball.velocity  * dt
    spring_right.axis = ball.pos - wall_right.pos
    spring_left.axis = ball.pos - wall_left.pos
    ball.trail.append(pos=ball.pos)
    t = t + dt

Complete code here

Create a button widget, binding it to a function that resets all the important variables that are used in the loop. That function can reset the attributes of the ball object, because "ball" is a "global" variable, but if you also need to reset an ordinary variable named "z", you need to specify "global z" in the function bound to the button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM