Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- class LED:
- """Basic LED functionality to display Hexadecimal numbers
- (may be extended to handle all alphanumeric characters) as
- a 7-segment LED display."""
- master = None
- canvas = None
- height = 200
- width = 100
- chars = {'0':'abcdef',
- '1':'ed',
- '2':'bcefg',
- '3':'cdefg',
- '4':'adeg',
- '5':'acdfg',
- '6':'abcdfg',
- '7':'edf',
- '8':'abcdefg',
- '9':'acdefg',
- 'a':'abdefg',
- 'b':'abcdg',
- 'c':'abcf',
- 'd':'bcdeg',
- 'e':'abcfg',
- 'f':'abfg',
- 'g':'abcdf',
- 'h':'abdeg',
- 'i':'ab',
- 'j':'cde',
- 'l':'abc',
- 'm':'bdfg',
- 'n':'bdg',
- 'o':'bcdg',
- 'p':'abefg',
- 'q':'adefg',
- 'r':'bg',
- 's':'acdfg',
- 'u':'bcd',
- 'y':'adeg',
- 'z':'bcefg',
- ' ':'',
- '' :'',
- '_':'c'}
- def __init__(self, master, value, locX, locY, width = 100, height = 200):
- self.canvas = Canvas(master, height=height, width=width, bg='black')
- self.canvas.place(x=locX, y=locY)
- self.height = height
- self.width = width
- self.drawSeg()
- self.colorSeg(value)
- def __call__(self, value):
- self.colorSeg(value)
- def drawSeg(self):
- widthRatio = int(0.20*self.width) # vertical segment thickness
- heightRatio = int(0.1*self.height) # horizontal segment thickness
- padx = int(0.1*self.width) # padding of inner rectangle from canvas
- pady = int(0.05*self.height)
- segHeight = self.height/2 - pady # height of vertical segment
- coords = {
- 'a':[(padx, pady),
- (padx, pady + segHeight),
- (padx + widthRatio, pady + segHeight - heightRatio),
- (padx + widthRatio, pady + heightRatio)],
- 'b':[(padx, pady + segHeight),
- (padx, pady + segHeight*2),
- (padx + widthRatio, pady + segHeight*2 - heightRatio),
- (padx + widthRatio, pady + segHeight + heightRatio)],
- 'e':[(self.width - padx, pady),
- (self.width - padx, pady + segHeight),
- (self.width - (padx + widthRatio), pady + segHeight - heightRatio),
- (self.width - (padx + widthRatio), pady + heightRatio)],
- 'd':[(self.width - padx, pady + segHeight),
- (self.width - padx, pady + segHeight*2),
- (self.width - (padx + widthRatio), pady + segHeight*2 - heightRatio),
- (self.width - (padx + widthRatio), pady + segHeight + heightRatio)],
- 'f':[(padx, pady),
- (self.width - padx, pady),
- (self.width - (padx + widthRatio), pady + heightRatio),
- (padx + widthRatio, pady + heightRatio)],
- 'c':[(padx, pady + segHeight*2),
- (self.width - padx, pady + segHeight*2),
- (self.width - (padx + widthRatio), pady + segHeight*2 - heightRatio),
- (padx + widthRatio, pady + segHeight*2 - heightRatio)],
- 'g':[(padx, pady + segHeight),
- (padx + widthRatio, pady + segHeight - heightRatio),
- (self.width - (padx + widthRatio), pady + segHeight - heightRatio),
- (self.width - padx, pady + segHeight),
- (self.width - (padx + widthRatio), pady + segHeight + heightRatio),
- (padx + widthRatio, pady + segHeight + heightRatio)],
- }
- for char in coords.keys():
- self.canvas.create_polygon(coords[char], fill='black', tag=char, outline='black', width=3)
- def drawSeg2(self):
- """Alternate design of led's"""
- padx = int(0.05*self.height)
- pady = int(0.1*self.width)
- hSegThick = int((1.0/8)*self.width)
- vSegThick = int((1.0/16)*self.height)
- segHeight = self.height/2 - padx/2 - hSegThick
- vCoords = [(padx + vSegThick/2, pady + hSegThick/2 ),
- (padx , pady + hSegThick ),
- (padx , pady + segHeight ),
- (padx + vSegThick/2, pady + hSegThick/2 + segHeight),
- (padx + vSegThick , pady + segHeight ),
- (padx + vSegThick , pady + hSegThick )]
- hCoords = [(padx + vSegThick/2 , pady + hSegThick/2),
- (padx + vSegThick , pady + hSegThick ),
- (self.width - padx , pady + hSegThick ),
- (self.width - padx + vSegThick/2, pady + hSegThick/2),
- (self.width - padx , pady ),
- (padx + vSegThick , pady)]
- coords = {'a':transform(vCoords, 0 , 0 ),
- 'b':transform(vCoords, 0 , segHeight ),
- 'd':transform(vCoords, self.width - padx*2, segHeight ),
- 'e':transform(vCoords, self.width - padx*2, 0 ),
- 'c':transform(hCoords, 0 , segHeight*2),
- 'f':transform(hCoords, 0 , 0 ),
- 'g':transform(hCoords, 0 , segHeight )}
- for char in coords.keys():
- self.canvas.create_polygon(coords[char], fill='#200000', tag=char, outline='black', width=3)
- def colorSeg(self, value):
- if not str(value) in self.chars.keys(): return
- segList = self.chars[str(value)]
- for char in set('abcdefg').difference(set(segList)):
- self.canvas.itemconfigure(char, fill='#200000')
- for char in segList:
- self.canvas.itemconfigure(char, fill='#FF0000')
- '''for char in set(segList).intersection(set(coords.keys())):
- self.canvas.create_polygon(coords[char], fill='red')'''
- # Note for optimization of segment generation:
- # Define just two or four points and obtain remaining by adding or subtracting
- # appropriate measures.
- def transform(l, inrx, inry):
- return [(i + inrx,j + inry) for i,j in l]
- def f(e):
- global count, l1
- try:
- l1(count.next())
- except StopIteration:
- print "All done"
- def main():
- global count, l1
- count = (i for i in sorted(LED.chars.keys()))
- root = Tk()
- root.geometry('320x400+50+50')
- l1 = LED(root, "1", 50, 50)
- root.bind('<Button-1>', f)
- root.mainloop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement