#! /usr/bin/python from serial import * from tkinter import * import time #import ctypes #Serial ser = Serial("COM9",38400,timeout=0,writeTimeout=0) time.sleep(1) xPos = 500 yPos = 300 #function for making a circle def drawcircle(canv,x,y,rad,color): canv.create_oval(x-rad,y-rad,x+rad,y+rad,width=0,fill=color) def goToClick(event): global xPos global yPos xPos = event.x yPos = event.y #drawcircle(sketcher,xPos,yPos,5,"#8D8A8A") #Main window root = Tk() #Tk is a function that makes a class root.wm_title("Canvas Sketch") root.config(bg="#8D8A8A", bd="0") sketcher = Canvas(root, width=1000, height=600, bg='white') sketcher.grid(row=0, column=0) sketcher.bind('', goToClick) #log = Text(root, width=50, height=8, takefocus=0) #log.grid(row=1, column=0, padx=2, pady=2) color = 'black' verPos = 0 horPos = 0 def runLoop(event=0): ln = ser.readline() lineStr = ln.decode(encoding='UTF-8') #log.insert('0.0',lineStr) #print(lineStr) Xline = 0 Yline = 0 circline = 0 newLine = 0 global verPos global horPos global xPos global yPos global color #determine selected color if "C" in lineStr: for line in lineStr.split(','): if "R" in line: color = 'red' if "Y" in line: color = 'yellow' if "G" in line: color = 'green' if "B" in line: color = 'blue' if "K" in line: color = 'black' #get X value elif "X" in lineStr: if len(lineStr) > 6: for line in lineStr.split(','): if Xline == 1: #take value from lineStr verPos = float(line.strip("\n \r"))*-1 #print(verPos) Xline = 0 elif Xline == 0: Xline = 1 #get Y value elif "Y" in lineStr: for line in lineStr.split(','): if Yline == 1: #take value from lineStr horPos = float(line.strip("\n \r")) #print(horPos) Yline = 0 newLine = 1 elif Yline == 0: Yline = 1 #draw circle elif "O" in lineStr: if len(lineStr) > 4: for line in lineStr.split(','): if circline == 1: circDiam = float(line.strip("\n \r")) #draw circle drawcircle(sketcher,xPos,yPos,5*circDiam,color) circline = 0 elif circline == 0: circline = 1 if newLine == 1: newY = yPos+5*verPos newX = xPos+5*horPos sketcher.create_line(xPos, yPos, newX, newY, fill=color, width = 2) xPos = newX yPos = newY #Something like the below will probably work, but as of now #these lines break the computer because the mouse becomes unusable #ctypes.windll.user32.SetCursorPos(100, 20) #ctypes.windll.user32.mouse_event(2, 0, 0, 0,0) # left down #ctypes.windll.user32.mouse_event(4, 0, 0, 0,0) # left up root.after(10, runLoop) runLoop() root.mainloop()