[英]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.
我已经使用一些图形元素通过 VPython 实现了它。 I used the slider function from Vpython to change the variables of radius and mass of the ball in real time.
我使用 Vpython 中的 slider function 实时更改球的半径和质量变量。 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 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
Create a button widget, binding it to a function that resets all the important variables that are used in the loop.创建一个按钮小部件,将其绑定到 function 以重置循环中使用的所有重要变量。 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.
那function可以重置球object的属性,因为“球”是一个“全局”变量,但是如果还需要重置一个名为“z”的普通变量,则需要在ZC1C4252068E68394F1AB45A的绑定中指定“全局z”按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.