Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is causing this memory leak with Tkinter canvas?
    primarykey
    data
    text
    <p>Using the following code, the memory usage increases rapidly as the asteroid images move across the screen, then stops increasing as the images move beyond the edges of the canvas. Could anyone explain why this happens? I would like to keep an image moving on the screen indefinitely in my program, but it eventually eats up all the memory on my system.</p> <p>I am new to python, Tk, and Tkinter. Is there something obvious I am missing about canvas.move(...) or canvas.update()? Should I be using a different method to accomplish this task? Thanks. </p> <pre><code>from Tkinter import Tk, Canvas, Frame, BOTH, NW import Image import ImageTk from random import random root = Tk() f = Frame(root) f.pack(fill="both", expand=True) canvas = Canvas(f, width=1000, height=1000) canvas.pack(fill=BOTH, expand=1) image = ImageTk.PhotoImage(Image.open("asteroid01.png")) sprites = [] for i in range(10): sprites.append(canvas.create_image(50*random(), 50*random(), image=image)) vel = {'x': 1, 'y': 1} while True: for s in sprites: canvas.move(s, vel['x'], vel['y']) canvas.update() </code></pre> <p>EDIT: Calling update seems to a bad practice, so here is the code changed as suggested. However, the program still consumes memory while the images are moving and does not release it until the window is closed.</p> <pre><code>from Tkinter import Tk, Canvas, Frame, BOTH, NW import Image import ImageTk from random import random root = Tk() f = Frame(root) f.pack(fill="both", expand=True) canvas = Canvas(f, width=1000, height=1000) canvas.pack(fill=BOTH, expand=1) image = ImageTk.PhotoImage(Image.open("asteroid01.png")) sprites = [] for i in range(10): sprites.append(canvas.create_image(50*random(), 50*random(), image=image)) vel = {'x': 1, 'y': 1} def move(): for s in sprites: canvas.move(s, vel['x'], vel['y']) canvas.after(10, move) move() root.mainloop() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload