473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Memory Leak with Tkinter Canvas (Python 2.5 Win32)

Hey everyone. I have been working with python for a couple years now,
but just recently built my first program with a GUI. I decided to
start with Tkinter since it is included with the base package,
although wxWindows will likely be my next choice. Tkinter seems to be
pretty slow for my needs.

Anyway - I am building a genetic algorithm simulator. I have a grid
where an Ant moves around. It is infeasible for me to update the grid
every simulation step - so I just do it at the end. But what I've
realized is that my program performs worse and worse when I update the
grid. Turns out there is a memory leak somewhere and I don't think it
is in my code. The memory leak occurs only when I write (via
create_rectangle) to the canvas widget. I wrote the following small
script to demonstrate this problem (see below). Every time the button
is pressed, _1040KB_ is added to the RAM of wpython.exe. This adds up
FAST. I have not verified this on my OS X box.

As you can see- I am doing nothing other than drawing a lot of
rectangles on the canvas. I have two questions.

1. Is this a bug in my usage of Tkinter? Am I somehow leaving
objects laying around that aren't being deleted? Is create_rectangle
not the appropriate function to use?)
2. Is there a better, quicker way to update a "grid"-like object?

Thanks,
Blaine

Current System:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Windows XP SP2 - all recent patches and updates

Script:

from Tkinter import *

canv = None
HEIGHT=600
WIDTH=600
def clear_grid():
for i in range(0,HEIGHT/10):
for j in range(0, HEIGHT/10):
canv.create_rectangle(i*10,j*10, \
i*10+10, j*10+10, \
fill = "white")

def draw_window(master):
global canv
frame = Frame(master)

btn_grid = Button(frame, text="draw grid", command=clear_grid)
btn_grid.pack(side=TOP)

canv = Canvas(frame, height=HEIGHT, width=WIDTH, bg='white')
canv.pack()
frame.pack()

root = Tk()
draw_window(root)
mainloop()

Aug 2 '07 #1
3 2630
frikk wrote:
[...]
As you can see- I am doing nothing other than drawing a lot of
rectangles on the canvas.
You aren't drawing, but **creating** rectangle objects, as
a meth. name suggests. You find more info at tkinter.effbot.org.
[...]

def clear_grid():
canv.delete(ALL)
for i in range(0,HEIGHT/10):
for j in range(0, HEIGHT/10):
canv.create_rectangle(i*10,j*10, \
i*10+10, j*10+10, \
fill = "white")
w.
Aug 2 '07 #2

"frikk" <fr...l.comwrote:
1. ....... Am I somehow leaving
objects laying around that aren't being deleted? Is create_rectangle
not the appropriate function to use?)
Try calling the canvas's delete method with the old rectangle before
making a new one.

- Hendrik

Aug 3 '07 #3
On Aug 3, 2:26 am, "Hendrik van Rooyen" <m...@microcorp.co.zawrote:
"frikk" <fr...l.comwrote:
1. ....... Am I somehow leaving
objects laying around that aren't being deleted? Is create_rectangle
not the appropriate function to use?)

Try calling the canvas's delete method with the old rectangle before
making a new one.

- Hendrik
Hey guys-

Both of your suggestions were perfect - I was just unaware I was
actually creating new *object* instead of drawing on the canvas. noob
mistake. Thanks again!

-Blaine

Aug 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank Stajano | last post by:
The compact form of pack behaves differently (and I believe incorrectly) compared to the long form. The following two scripts demonstrate it, at least on this interpreter: Python 2.3.2 (#1, Oct 9...
2
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the...
3
by: Guy | last post by:
Hi It might take me a little time to explain this but here goes. Firstly I'm not using the latest upto date python releases so my first plan is to try more upto date rels of python and win32...
1
by: Elaine Jackson | last post by:
Newbie. Playing with the 'turtle' module and wondering if there's a way to save the graphics you make with it. The documentation itself has nothing to say about this, nor (as far as I can tell)...
2
by: Elbert Lev | last post by:
#When I'm running this script on my windows NT4.0 box, #every time dialog box is reopened there is memory growth 384K. #Bellow is the text I sent to Stephen Ferg (author of easygui) # I have...
8
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
1
by: akineko | last post by:
Hello everyone, I'm trying to create custom Tkinter/Pmw widgets for my project. After testing my widgets under Unix (Solaris), I have tried them under Windows and I got a surprise. The...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.