A LED control that can have different colors, each associated with a state. The default is green, amber, and red. Calling ledControl.State = 1 will set the color of the LED to the color by that index (given as a list in the constructor.) In the default case 0 is red, 1 is amber, and 2 is green.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | # Based on C++ code by Thomas Monjalon
# Developed by Daniel Eloff on 14/9/07
import wx
def change_intensity(color, fac):
rgb = [color.Red(), color.Green(), color.Blue()]
for i, intensity in enumerate(rgb):
rgb[i] = min(int(round(intensity*fac, 0)), 255)
return wx.Color(*rgb)
class LED(wx.Control):
def __init__(self, parent, id=-1, colors=[wx.Colour(220, 10, 10), wx.Colour(250, 200, 0), wx.Colour(10, 220, 10)],
pos=(-1,-1), style=wx.NO_BORDER):
size = (17, 17)
wx.Control.__init__(self, parent, id, pos, size, style)
self.MinSize = size
self._colors = colors
self._state = -1
self.SetState(0)
self.Bind(wx.EVT_PAINT, self.OnPaint, self)
def SetState(self, i):
if i < 0:
raise ValueError, 'Cannot have a negative state value.'
elif i >= len(self._colors):
raise IndexError, 'There is no state with an index of %d.' % i
elif i == self._state:
return
self._state = i
base_color = self._colors[i]
light_color = change_intensity(base_color, 1.15)
shadow_color = change_intensity(base_color, 1.07)
highlight_color = change_intensity(base_color, 1.25)
ascii_led = '''
000000-----000000
0000---------0000
000-----------000
00-----XXX----=00
0----XX**XXX-===0
0---X***XXXXX===0
----X**XXXXXX====
---X**XXXXXXXX===
---XXXXXXXXXXX===
---XXXXXXXXXXX===
----XXXXXXXXX====
0---XXXXXXXXX===0
0---=XXXXXXX====0
00=====XXX=====00
000===========000
0000=========0000
000000=====000000
'''.strip()
xpm = ['17 17 5 1', # width height ncolors chars_per_pixel
'0 c None',
'X c %s' % base_color.GetAsString(wx.C2S_HTML_SYNTAX).encode('ascii'),
'- c %s' % light_color.GetAsString(wx.C2S_HTML_SYNTAX).encode('ascii'),
'= c %s' % shadow_color.GetAsString(wx.C2S_HTML_SYNTAX).encode('ascii'),
'* c %s' % highlight_color.GetAsString(wx.C2S_HTML_SYNTAX).encode('ascii')]
xpm += [s.strip() for s in ascii_led.splitlines()]
self.bmp = wx.BitmapFromXPMData(xpm)
self.Refresh()
def GetState(self):
return self._state
State = property(GetState, SetState)
def OnPaint(self, e):
dc = wx.PaintDC(self)
dc.DrawBitmap(self.bmp, 0, 0, True)
|
any chance of an example? Not being terribly familiar with wxPython, could you provide a small example to show us how to use it?
Thanks, Mike
wx example.
Create one for see how it work ;) Michele