from Tkinter import * root = Tk() enabled = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] #list of led states (1 for on, -1 for off) buttons = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] #initialize matrix for buttons with dummy values #addresses = [[00,01,02,03], [04,05,06,07], [08,09,10,11], [12,13,14,15], [16,17,18,19]] height = 900 #size of screen that pops up width = 900 canvas = Canvas(highlightthickness = 0, height = height, width = width) canvas.master.title("5 x 4 LED Matrix") #class Arduino: # connection = None # def __init__(self, path): # self.connection = serial.Serial(path, 9600) # pass # def __del__(self): # self.connection.close() # pass # def isOpen(self): # return self.connection.isOpen() #def writeline(self, line): # if self.isOpen(): # self.connection.write(line) # time.sleep(0.1) #arduino = Arduino(2) #sets serial port to 2, which is probably COM3 #arduino.writeLine('!') #resets all LED's def ledWrite(a): print str(a[0]) + str(a[1]) #arduino.writeLine(str(a[0])+str(a[1])) if enabled[a[0]][a[1]] == 1: buttons[a[0]][a[1]].configure(bg = '#FFFF99') #if buttons are enabled they are yellow else: buttons[a[0]][a[1]].configure(bg = '#000000') #buttons black when off buttons[a[0]][a[1]].update() enabled[a[0]][a[1]] = enabled[a[0]][a[1]] * -1 #toggle button state #print 'Address = ' + str(addresses[a[0]][a[1]]) for i in range(0,5): for j in range(0,4): #these two lines loop through give row and column numbers w = Button(root, bg = 'black', width = 20, height = 9, relief = RAISED, command = lambda arg = [i,j]: ledWrite(arg)) #create a button for each LED index, write state w.grid(row=i,column=j) #places the button in the grid buttons[i][j] = w #replaces placeholder in button matrix with button object print buttons #print statement for debugging mainloop() #arduno.writeLine('!') print '!' #arduino.connection.close()