Optimizing canvas performance

yanivk at my-deja.com yanivk at my-deja.com
Sun Aug 20 07:53:19 EDT 2000


Hi folks, I am resending this note because it didn't come out well last time.
Sorry for the inconvenience. I'm working on an application that requires
drawing widgets on a canvas. The amount of widgets can be up to about 1000
(including text, frames, buttons and menu widgets). I use
Canvas.create_window to draw them as I wish but for some reason this takes
too long on AIX and runs very fast on Windows NT. I'm looking for a way to
rush it on AIX since the application is written for that. Following is a
sample of the code that simulates the exact operations I perform in my
aplication. Run it on AIX and I will be glad to get any ideas for rushing it:

from Tkinter import *
import string

class Form(Frame):
   def __init__(self, master=None):
   #---------------------------------
      Frame.__init__(self, master)
      self.height=30
      self.coords = []
      self.formulaslist = []
      self.makewidgets()

  def makewidgets(self):  #--------------------------  self.sb =
Scrollbar(self)  self.sb.pack(side=RIGHT, fill=Y)  f = Frame(self,
relief=SUNKEN, bd=2)  f.pack(fill=BOTH, expand=1)  self.canvas = Canvas(f,
yscrollcommand = self.sb.set, height=10, width=10, highlightthickness=0) 
self.canvas.pack(fill=BOTH, expand=1)  self.sb.config(command =
self.canvas.yview)

  def prepare_formulas(self, formulas): 
#------------------------------------------------  import time	self.templist
= []  self.formulas=len(formulas)  pos = 0  line_width=110  frame=None	for
eng, sug, exp in formulas:     mbutton = Label(self.canvas, text=' 
'+`pos+1`+'  ', height=1)	   menu = mbutton.bind('<1>', lambda event,
s=self, f=pos+1: s.select_formula(f))	       mbutton.bind('<3>', lambda
event, s=self, f=pos+1: s.popup_menu(f))     b1 = None	     b2 = None	    
frame = Frame(self.canvas, height=150)		text = Text(frame,
selectborderwidth=0,		     width=line_width, height=6)     l =
'SUGAR:'+sug+'\n'	    lines = string.splitfields(l, '\n')     count =
len(lines)	for line in lines:	if len(line)>line_width-1: count =
count+len(line)/line_width	     if eng:	     lines =
string.splitfields(eng, '\n')		count = count+len(lines)	for
line in lines:		    if len(line)>line_width-1: count =
count+len(line)/line_width	     text.config(height=count+1)     if eng: 
       text.insert(0.0, 'ENGLISH:', 'english')	      
text.insert(END,eng+'\n')	text.insert(END, 'SUGAR:', 'sugar')    
text.insert(END, l[6:], 'sugar_text')		else:	       
text.insert(0.0, 'SUGAR:', 'sugar')	text.insert(END, l[6:], 'sugar_text')
	  pos = pos+1	  self.templist.append( (eng, sug, exp, frame, menu,
mbutton, text, b1, b2) )

  def commit_formulas(self):  #-----------------------------  pos = 0  status
= 'Unknown'  for (eng, sug, exp, frame, menu, mbutton, text, b1, b2) in
self.templist:	    self.canvas.create_window(40, self.height,
tag='mbutton'+`pos+1`, window=mbutton)	     st = self.canvas.create_text(80,
self.height, text=status, anchor=W, tag='status'+`pos+1`)      text.pack()   
 self.canvas.create_window(400, 30+self.height, window=frame,
tag='text'+`pos+1`, anchor='n')    self.update_idletasks()	   h =
frame.winfo_reqheight()     text.config(state=DISABLED)    
self.canvas.create_line(0, 60+h+self.height,			       
int(self.canvas.cget('width')), 60+h+self.height,			     
  fill='black', width=2, tag='line'+`self.formulas`)	 
self.coords.append(self.height) 	self.height = self.height+60+h+35    
  pos = pos+1	  self.formulaslist.append( (status, sug, exp, None, None,
NO, st, menu, mbutton, text, (b1, -1, b2, -1)) )  self.updatescrollregion() 
self.templist = []

   def updatescrollregion(self):
   #----------------------------------
       h = max( self.height, int(self.canvas.cget('height')) )
       w = int(self.canvas.cget('width'))
       self.canvas.config(scrollregion = (0,0, w, h))



if __name__=='__main__':
    f = Form()
    f.pack(fill=BOTH, expand=1)
    f.update_idletasks()
    formulas = []
    for i in range(200):
	formulas.append(('a', 'a', 'a'))
    f.prepare_formulas(formulas)
    f.commit_formulas()
    f.canvas.config(height=400, width=900)
    f.mainloop()


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list