# Import neccesary libraries from tkinter import * from tkinter import messagebox as msg import serial from serial import Serial class Tile(Canvas): ''' Represents a Tile of the Interface ''' def __init__(self, master, coord,color = 'black'): '''Tile(master,coord,color) -> Tile Creates the Tile''' # Initiate the canvas Canvas.__init__(self,master,width=50,height=50,\ bg=color, highlightbackground='black',\ highlightthickness = 2, bd = 0) # Grids the square self.grid(row=coord[0],column=coord[1]) # Store and define variables self.color = color self.coord = coord self.master = master def changeColor(self,color): '''Tile.getColor(color) Changes the color of the tile''' self.color = color self['bg'] = self.color class InterfaceFrame(Frame): '''Represents the whole tkinter window''' def __init__(self,master): '''InterfaceFrame(master) -> InterfaceFrame initiantes the interface.''' # Inititiate the frame Frame.__init__(self,master,bg='black') self.grid() # Store and define variables self.Frame = Frame self.master = master self.tiles = [] self.data = "" # Make the squares # For every row for row in range(3): rowList = [] # For every column for column in range(3): # Store the coords coord = [row,column] # Make the Tile square = Tile(self,coord) rowList.append(square) self.tiles.append(rowList) # Establish the serial connection on a specific port self.ser = serial.Serial("/dev/cu.usbserial-A50285BI", 9600) for i in range(10000): inData = str(self.ser.readline(1000)) if inData[2] == "1": self.tiles[1][1].changeColor('green') print('Pressed') else: self.tiles[1][1].changeColor('red') print('Not Pressed') def Interface(): '''Interface() Starts the Interface''' # Makes the root root = Tk() # Gives a title root.title('Interface') # Starts the game game = InterfaceFrame(root) # root.mainloop() Interface()