Several optimizations:

self.__chips now contains the list of rgbtuple values for the
    chips named i - 1 (Tkinter counts from 1, we count from zero).
    The chip number was just the index + 1.  This means color lookup
    need not do an itemcget(), it can just index into __chips.

    instead of calling __canvas.itemconfigure(), we glom up a huge Tcl
    script and call tk.eval() directly.  Actually we do many appends
    to a Python list, then string.join() them together into one huge
    string.  This reduces the overhead of Tkinter but making one fast
    call to Tcl.
This commit is contained in:
Barry Warsaw 1998-02-18 00:05:59 +00:00
parent 8d3e5ee552
commit f67a50c2e1
1 changed files with 80 additions and 50 deletions

View File

@ -1,7 +1,9 @@
import string
from Tkinter import * from Tkinter import *
import Pmw import Pmw
import ColorDB import ColorDB
class LeftArrow: class LeftArrow:
_ARROWWIDTH = 30 _ARROWWIDTH = 30
_ARROWHEIGHT = 15 _ARROWHEIGHT = 15
@ -74,7 +76,7 @@ class StripWidget(Pmw.MegaWidget):
def __init__(self, parent=None, **kw): def __init__(self, parent=None, **kw):
options = (('color', (128, 128, 128), self.__set_color), options = (('color', (128, 128, 128), self.__set_color),
('delegate', None, None), ('delegate', None, self.__set_delegate),
('chipwidth', self._CHIPWIDTH, Pmw.INITOPT), ('chipwidth', self._CHIPWIDTH, Pmw.INITOPT),
('chipheight', self._CHIPHEIGHT, Pmw.INITOPT), ('chipheight', self._CHIPHEIGHT, Pmw.INITOPT),
('numchips', self._NUMCHIPS, Pmw.INITOPT), ('numchips', self._NUMCHIPS, Pmw.INITOPT),
@ -116,12 +118,13 @@ class StripWidget(Pmw.MegaWidget):
x = 1 x = 1
y = 30 y = 30
for c in range(self.__numchips): for c in range(self.__numchips):
color = 'grey'
rect = self.__canvas.create_rectangle( rect = self.__canvas.create_rectangle(
x, y, x+chipwidth, y+chipheight, x, y, x+chipwidth, y+chipheight,
fill='grey', outline='grey') fill=color, outline=color)
x = x + chipwidth + 1 # for outline x = x + chipwidth + 1 # for outline
chips.append(rect) chips.append(color)
# create the string tag # create the string tag
self.__label = self.__canvas.create_text( self.__label = self.__canvas.create_text(
@ -140,24 +143,37 @@ class StripWidget(Pmw.MegaWidget):
self.__axis = self['axis'] self.__axis = self['axis']
assert self.__axis in (0, 1, 2) assert self.__axis in (0, 1, 2)
self.initialiseoptions(StripWidget) self.initialiseoptions(StripWidget)
self.__delegate = self['delegate']
def __set_color(self): def __set_color(self):
rgbtuple = self['color'] rgbtuple = self['color']
self.set_color(self, rgbtuple) self.set_color(self, rgbtuple)
def __arrow_x(self, chipnum): def __arrow_x(self, chipnum):
coords = self.__canvas.coords(self.__chips[chipnum]) coords = self.__canvas.coords(chipnum+1)
assert coords assert coords
x0, y0, x1, y1 = coords x0, y0, x1, y1 = coords
return (x1 + x0) / 2.0 return (x1 + x0) / 2.0
def __select_chip(self, event=None): def __select_chip(self, event=None):
chip = self.__canvas.find_closest(event.x, event.y) chip = self.__canvas.find_closest(event.x, event.y)[0]
delegate = self['delegate'] if chip and self.__delegate:
if chip and delegate: color = self.__chips[chip-1]
color = self.__canvas.itemcget(chip, 'fill')
rgbtuple = ColorDB.rrggbb_to_triplet(color) rgbtuple = ColorDB.rrggbb_to_triplet(color)
delegate.set_color(self, rgbtuple) self.__delegate.set_color(self, rgbtuple)
## import profile
## import pstats
## import tempfile
## statfile = tempfile.mktemp()
## p = profile.Profile()
## p.runcall(self.__delegate.set_color, self, rgbtuple)
## p.dump_stats(statfile)
## s = pstats.Stats(statfile)
## s.strip_dirs().sort_stats('time').print_stats(10)
def __set_delegate(self):
self.__delegate = self['delegate']
# #
@ -166,18 +182,33 @@ class StripWidget(Pmw.MegaWidget):
def set_color(self, obj, rgbtuple): def set_color(self, obj, rgbtuple):
red, green, blue = rgbtuple red, green, blue = rgbtuple
if self.__generator: assert self.__generator
i = 0 i = 1
chip = 0 chip = 0
chips = self.__chips = []
tclcmd = []
for t in self.__generator(self.__numchips, rgbtuple): for t in self.__generator(self.__numchips, rgbtuple):
rrggbb = ColorDB.triplet_to_rrggbb(t) rrggbb = ColorDB.triplet_to_rrggbb(t)
self.__canvas.itemconfigure(self.__chips[i], chips.append(rrggbb)
fill=rrggbb, ## self.__canvas.itemconfigure(i,
outline=rrggbb) ## fill=rrggbb,
## outline=rrggbb)
tclcmd.append(self.__canvas._w)
tclcmd.append('itemconfigure')
tclcmd.append(`i`)
tclcmd.append('-fill')
tclcmd.append(rrggbb)
tclcmd.append('-outline')
tclcmd.append(rrggbb)
tclcmd.append('\n')
tred, tgreen, tblue = t tred, tgreen, tblue = t
if tred <= red and tgreen <= green and tblue <= blue: if tred <= red and tgreen <= green and tblue <= blue:
chip = i chip = i
i = i + 1 i = i + 1
# call the raw tcl script
script = string.join(tclcmd, ' ')
self.__canvas.tk.eval(script)
# get the arrow's text # get the arrow's text
coloraxis = rgbtuple[self.__axis] coloraxis = rgbtuple[self.__axis]
text = repr(coloraxis) text = repr(coloraxis)
@ -186,12 +217,12 @@ class StripWidget(Pmw.MegaWidget):
if coloraxis <= 128: if coloraxis <= 128:
# use the left chip # use the left chip
self.__leftarrow.set_text(text) self.__leftarrow.set_text(text)
self.__leftarrow.move_to(self.__arrow_x(chip)) self.__leftarrow.move_to(self.__arrow_x(chip-1))
self.__rightarrow.move_to(-100) self.__rightarrow.move_to(-100)
else: else:
# use the right chip # use the right chip
self.__rightarrow.set_text(text) self.__rightarrow.set_text(text)
self.__rightarrow.move_to(self.__arrow_x(chip)) self.__rightarrow.move_to(self.__arrow_x(chip-1))
self.__leftarrow.move_to(-100) self.__leftarrow.move_to(-100)
# and set the chip's outline # and set the chip's outline
pmwrgb = ColorDB.triplet_to_pmwrgb(rgbtuple) pmwrgb = ColorDB.triplet_to_pmwrgb(rgbtuple)
@ -200,5 +231,4 @@ class StripWidget(Pmw.MegaWidget):
outline = 'white' outline = 'white'
else: else:
outline = 'black' outline = 'black'
self.__canvas.itemconfigure(self.__chips[chip], self.__canvas.itemconfigure(chip, outline=outline)
outline=outline)