Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7919527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:53:46+00:00 2026-06-03T15:53:46+00:00

The following python program creates a Tkinter Canvas object and draws random matrix on

  • 0

The following python program creates a Tkinter Canvas object and draws random matrix on it.
It also measures the time it takes to make 10 subsequent updates. As you may see from the
output below, this time grows continiously and substantially during the course of
the program. What is the reason for this behavior and how can I fix it?

from Tkinter import Tk, Canvas
import time
import numpy as np

window = Tk()
nRows = 30
nCols = 30
CELL_SIZE = 10
canvas = Canvas(window, width=CELL_SIZE*nRows,
                height=CELL_SIZE*nCols)
canvas.pack()
def drawbox(m):
    for y in range(nRows):
        for x in range(nCols):
            if m[y][x]:
                color = '#00FF00'
            else:
                color = '#000000'
            canvas.create_rectangle(CELL_SIZE*x,
                                    CELL_SIZE*y,
                                    CELL_SIZE*x+CELL_SIZE,
                                    CELL_SIZE*y+CELL_SIZE,
                                    fill=color,
                                    outline="#000000", width=1)
count = 0
timeStart = time.time()
while(True):
    board = np.random.rand(nRows, nCols) > 0.5
    if count % 10 == 0:
        print '%.1f seconds'%(time.time() - timeStart)
        timeStart = time.time()
        count = 0
    count += 1
    drawbox(board)
    canvas.after(5)
    canvas.update()

Here is the output

0.0 seconds
1.7 seconds
4.1 seconds
6.3 seconds
8.7 seconds
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:53:48+00:00Added an answer on June 3, 2026 at 3:53 pm

    You create new items at each updates. The canvas display all the rectangles you have previously added and thus go slower and slower (each update create 900 rectangles, after 30 you have 27,000 objects in your scene…)

    To avoid this, you may create your rectangles once, and then only update their colors.

    You could have at toplevel:

    rectangles = [ [ canvas.create_rectangle (CELL_SIZE*x, CELL_SIZE*y,
                        CELL_SIZE*x+CELL_SIZE, CELL_SIZE*y+CELL_SIZE,
                        fill="#000000",outline="#000000", width=1) 
                     for x in range(nCols)] for y in range(nRows)]
    

    and in drawbox:

    canvas.itemconfig(rectangles[y][x], fill=color)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Python 2.6 program and YAML definition (using PyYAML ): import
the following is a line from a python program that calls the demo.exe file.
When running a Python program that reads from stdin, I get the following error:
Am trying to run the following python program import re regex=re.compile(http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]) f=open('out.txt') for a
I have a Python program that consists of several modules. The main module creates
A python program I created is IO bounded. The majority of the time (over
I have a python program that reads floating point values using the following regular
The following python script works fine: #!/usr/bin/env python import httplib, urllib params = urllib.urlencode({'url':'xxx/xxx/0AAAUw7n6qPQ922.jpg',
I have the following python code: class FooMeta(type): def __setattr__(self, name, value): print name,
I have the following python code: import logging import logging.config logging.config.fileConfig('a.conf') logging.debug('aaaa') with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.