Why aren't my old boxes disappearing?

56 views Asked by At

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...

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: enter image description here enter image description here

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():. 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? (All of this might make more sense if you have a look at the full code)

Thank you for any help in advance.

2

There are 2 answers

3
mhlester On BEST ANSWER

When you call

Panes = []

inside a function, it creates a new local variable Panes and sets it to an empty list.

If it is a global variable, you should call

Panes[:] = []

If it is in the class, you should call

self.Panes = []
0
cmd On

Panes is a local variable and will not change the value of your other use of Panes. You need to either make Panes global or an instance variable. Do the same in each place in your code that references Panes