Вопрос

I would like to draw as many as 86,000 (small circles) on a Tkinter Canvas. On average it will be more like 8,600 circles. At times as few as 400. All of the circles being drawn at once are the same (size , color). The radius of the circles is related to the number of circles being drawn (as little as 1-2px when there are many circles to draw), but a difference in radius has had little overall impact.

canvas.create_oval(px+r,py+r,px-r,py-r,fill='green') is quite expensive in computing time. Ideally I would pre-create the circle and paste copies of it on the canvas as needed.

At the moment calling canvas.create_oval(...) 86,000 times takes almost 20 seconds. (The logic that decides what circle to draw where itself runs in less than 100 msec.)

How would I go about copying a single circle instead of creating them all individually?

Это было полезно?

Решение

I don't think there's anything you can do to speed up the creation of circles. The canvas wasn't designed to handle 80,000 objects, and it doesn't support the ability to copy and paste items (beyond simply creating new objects with the same coordinates). It handles a few thousand OK, and even 10,000 is pretty performant on my machine, but 86,000 items is a lot.

You might try creating a single image of the given size (or have pre-computed images). You can have a single PhotoImage instance that you use to create all of the images on the canvas. On my machine I can create 100,000 image objects on a canvas in just a couple of seconds. deleting that many objects, however, is still quite slow.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top