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
@ -73,14 +75,14 @@ class StripWidget(Pmw.MegaWidget):
_NUMCHIPS = 40 _NUMCHIPS = 40
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),
('generator', None, Pmw.INITOPT), ('generator', None, Pmw.INITOPT),
('axis', None, Pmw.INITOPT), ('axis', None, Pmw.INITOPT),
('label', '', Pmw.INITOPT), ('label', '', Pmw.INITOPT),
) )
self.defineoptions(kw, options) self.defineoptions(kw, options)
@ -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,39 +182,53 @@ 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
for t in self.__generator(self.__numchips, rgbtuple): chips = self.__chips = []
rrggbb = ColorDB.triplet_to_rrggbb(t) tclcmd = []
self.__canvas.itemconfigure(self.__chips[i], for t in self.__generator(self.__numchips, rgbtuple):
fill=rrggbb, rrggbb = ColorDB.triplet_to_rrggbb(t)
outline=rrggbb) chips.append(rrggbb)
tred, tgreen, tblue = t ## self.__canvas.itemconfigure(i,
if tred <= red and tgreen <= green and tblue <= blue: ## fill=rrggbb,
chip = i ## outline=rrggbb)
i = i + 1 tclcmd.append(self.__canvas._w)
# get the arrow's text tclcmd.append('itemconfigure')
coloraxis = rgbtuple[self.__axis] tclcmd.append(`i`)
text = repr(coloraxis) tclcmd.append('-fill')
tclcmd.append(rrggbb)
tclcmd.append('-outline')
tclcmd.append(rrggbb)
tclcmd.append('\n')
tred, tgreen, tblue = t
if tred <= red and tgreen <= green and tblue <= blue:
chip = i
i = i + 1
# call the raw tcl script
script = string.join(tclcmd, ' ')
self.__canvas.tk.eval(script)
# move the arrow, and set it's text # get the arrow's text
if coloraxis <= 128: coloraxis = rgbtuple[self.__axis]
# use the left chip text = repr(coloraxis)
self.__leftarrow.set_text(text)
self.__leftarrow.move_to(self.__arrow_x(chip)) # move the arrow, and set it's text
self.__rightarrow.move_to(-100) if coloraxis <= 128:
else: # use the left chip
# use the right chip self.__leftarrow.set_text(text)
self.__rightarrow.set_text(text) self.__leftarrow.move_to(self.__arrow_x(chip-1))
self.__rightarrow.move_to(self.__arrow_x(chip)) self.__rightarrow.move_to(-100)
self.__leftarrow.move_to(-100) else:
# and set the chip's outline # use the right chip
pmwrgb = ColorDB.triplet_to_pmwrgb(rgbtuple) self.__rightarrow.set_text(text)
b = Pmw.Color.rgb2brightness(pmwrgb) self.__rightarrow.move_to(self.__arrow_x(chip-1))
if b <= 0.5: self.__leftarrow.move_to(-100)
outline = 'white' # and set the chip's outline
else: pmwrgb = ColorDB.triplet_to_pmwrgb(rgbtuple)
outline = 'black' b = Pmw.Color.rgb2brightness(pmwrgb)
self.__canvas.itemconfigure(self.__chips[chip], if b <= 0.5:
outline=outline) outline = 'white'
else:
outline = 'black'
self.__canvas.itemconfigure(chip, outline=outline)