I am trying to implement virtual LEDs on a Python window. I turn the LED "on" by drawing a green oval. I then use root.after() to schedule another call that turns the LED "off" by drawing a dark green oval. I use a 250ms delay. There are 4 active LEDs on the display. When I allow them to run (I have a global boolean to indicate if I want LEDs to be active), the processor usage starts to climb, going up a percent or two a minute, steadily climbing. I know root.after() returns a handle and you can use that to delete the action, but I don't think I'm overloading the system with too many of these calls.
Location is a tuple with 2 pairs of X,Y values for bounding box. LED() draws the LED immediately. ServiceLEDs() is called at 8 Hrz, and checks the time for all active LEDs to see if the time has passed; this is my second attempt, as my first attempt did a simpler root.after() call for every LED that needed to be handled. I thought this would be faster. Commented out ScheduleLED implements setting an "after" for each LED. The active ScheduleLED sets an array entry with a True flag, the desired state and the time it should expire.
Is it the after() that is causing me time proplems? Is it the tuples? All I know, is if I set UseLEDs to False, the progam runs along at about 3 percent of CPU, and if True, it grows and grows...
Help. Please!
Kelly
Here's code snippets:
def LED(self, location, state):
if (UseLEDs):
self.LEDCanvas.create_oval( location, fill=LEDColors[state])
def ServiceLEDs(self):
if (not UseLEDs): return
FLAG = 0
STATE = 1
TIME = 2
theTime = getTime()
for ii in range(0,4):
if (LEDTimes[ii][FLAG]):
if ( theTime > LEDTimes[ii][TIME] ):
self.LEDCanvas.create_oval( LEDTuples[ii], fill=LEDColors[LEDTimes[ii][STATE]])
LEDTimes[ii] = ( False, 0, 0 )
root.after(125, self.ServiceLEDs)
# def ScheduleLED(self, delay, tuple, state):
# if (UseLEDs):
# print "DEBUG", "LED", delay, state, tuple
# root.after( delay, lambda l=tuple, s=state : self.LED( l, s ))
def ScheduleLED(self, delay, theLED, state):
if (UseLEDs):
LEDTimes[theLED] = (True, state, getTime()+(delay/1000))
#root.after( delay, lambda l=tuple, s=state : self.LED( l, s ))