0

As part of a bigger programm i would like to show the progress my algorithm makes while running. For that purpose i display some dots (lines) on a canvas object. After drawing / end of algorithm my programm is not reactive any more, i.e. checkboxes take ages to react, and so on.

Finally i ended up with these 3 lines of code which will cause the problem:

for x in range( 1,300 ):
    for y in range(1, 300):
        self.__canvas.create_line(x,y,x+1,y+1 )

No other code running, gui is not reactive any more (very very slow).

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
siemon
  • 1
  • 1
  • 1
    This creates and stores 90,000 line objects that have to be refreshed every time the screen changes.. Were you hoping to have a single line that moves around? – Tim Roberts Dec 26 '22 at 17:32
  • Perhaps i was not clear enough: i paint the lines and do no more refresh, resize or other things. Below the canvas there are some checkboxes and trying to un-/check one of them AFTER painting takes ages. Just moving the cursor over the gui brings up cpu time of the app to 90 % - no action done. – siemon Dec 27 '22 at 10:56
  • Perhaps you don't understand what that loop does. That loop does not "paint" lines. It creates 90,000 line objects that each know how to paint themselves when refreshed. – Tim Roberts Dec 27 '22 at 18:39
  • It may well be that i have a wrong / no understanding of "paint" for tkinter. What i really want is just "some lines" showing what happened. There is no "refresh" which i intended / initiated, may be that tkinter "refreshes" the canvas after some time. Is there any way out of this ? How can i find out whether there is any "refresh" going on ? – siemon Dec 28 '22 at 10:54
  • The canvas has to keep track of all of those objects. When the mouse moves, it needs to scan through all 90,000 of them to see which one might have been hit. Perhaps you should create an image with PIL, then fill your image dot by dot and update it. That makes only ONE object for the canvas to track. – Tim Roberts Dec 29 '22 at 02:08
  • Thanks for your help so far and your advice to use PIL. It seems that someone else had had the same problem (https://stackoverflow.com/questions/1581799/how-to-draw-a-bitmap-real-quick-in-python-using-tk-only) and i will try to use PhotoImage now. – siemon Dec 30 '22 at 15:54
  • PhotoImage solved the problem for me, it is fast and gives what i wanted. Thanks for your help. How can i close this ? – siemon Jan 10 '23 at 17:05

1 Answers1

0

This creates and stores 90,000 line objects that have to be refreshed every time the screen changes.

The canvas has to keep track of all of those objects. When the mouse moves, it needs to scan through all 90,000 of them to see which one might have been hit. I suggest you create an image with PIL, then fill your image dot by dot and update it. That makes only ONE object for the canvas to track.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30