简体   繁体   English

为什么我的旧箱子没有消失?

[英]Why aren't my old boxes disappearing?

So I did earlier, a few months ago, ask a similar question but I can't seem to apply the same rules/laws as were applied in a previous question of mine. 因此,几个月前,我曾提过一个类似的问题,但是我似乎无法采用与我先前的问题相同的规则/法律。 Like my name suggest my understanding of Python is very basic and so I am confused at why his code won't work... 就像我的名字一样,这表明我对Python的理解是非常基础的,因此我对为什么他的代码不起作用感到困惑...

Every time I load a new screen, the old set of boxes should disappear and new only the new ones should be left there. 每次加载新屏幕时,旧的一组框应消失,只有新的框保留在那里。 However this is not what happens. 但是,这不会发生。 Here are pictures to demonstrate: 以下是要演示的图片: 在此处输入图片说明在此处输入图片说明

Link to the full code. 链接到完整代码。

However I think the problem may lie here in this part of the code: 但是我认为问题可能出在代码的这一部分:

def deletePanes(self):

    print("delete panes")
    Panes = []
    self.NoOfPanes = 0

I also call Panes = [] in an earlier section of the code which lies in class Application(): . 我还在class Application():中的代码的较早部分中调用Panes = [] Is the problem possibly that I am calling it twice? 问题可能是我打了两次电话吗? What I mean by this is that is it possible that one Panes list has the old boxes in it and one Panes has the new set in it and so when it comes to draw on the PyGame screen it draw both old and new boxes? 我的意思是,一个“ Panes列表中可能有旧框,一个“ Panes有新框,因此在PyGame屏幕上绘制时,它会同时绘制新框和旧框吗? (All of this might make more sense if you have a look at the full code) (如果您看完整的代码,所有这些可能都更有意义)

Thank you for any help in advance. 感谢您的任何帮助。

When you call 你打电话时

Panes = []

inside a function, it creates a new local variable Panes and sets it to an empty list. 在函数内部,它将创建一个新的局部变量Panes并将其设置为一个空列表。

If it is a global variable, you should call 如果是全局变量,则应调用

Panes[:] = []

If it is in the class, you should call 如果在班上,你应该打电话

self.Panes = []

Panes is a local variable and will not change the value of your other use of Panes . Panes是一个局部变量,不会更改您使用Panes的其他值。 You need to either make Panes global or an instance variable. 您需要使Panes global变量或实例变量。 Do the same in each place in your code that references Panes 在引用Panes代码中的每个位置执行相同的操作

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

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