简体   繁体   English

VPython 6-对象不会删除

[英]VPython 6 - Object won't delete

I have a 3D elastic collision simulation. 我有一个3D弹性碰撞模拟。 I'd like to clear all of the sphere objects from my program from a button press. 我想通过按一下按钮来清除程序中的所有球体对象。 The documentation indicates that I should do the following: 该文档指示我应该执行以下操作:

def clear_balls():
   for ball in balls:
      ball.visible = False
      del ball

This successfully makes the balls invisible in the scene, but they still take up memory and collide with balls that still exist. 这成功地使球在场景中不可见,但它们仍然占用内存并与仍然存在的球发生碰撞。 I want it completely removed. 我希望将其完全删除。 Trying this with a unique ball name this isn't part of a list as some have suggested still results in the same issue. 尝试使用唯一的球名称尝试此操作,这并不是列表的一部分,因为有些人建议仍然会导致相同的问题。

del ball is not doing what you think because balls still holds a reference to the object. del ball并没有按照您的想法做,因为balls仍然拥有对该对象的引用。 You need to empty the balls list: 你需要清空balls列表:

def clear_balls():
    for ball in balls:
        ball.visible = False
     balls[:] = []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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