import tkinter as tk root=tk.Tk() root.geometry("500x500+450+100") canvas1=tk.Canvas(root, width=500, heigh=500) canvas1.pack() command='M00' def keypressed(k): x=0 y=0 global command if k.keysym=='Up': y=-1 if k.keysym=='Down': y=1 if k.keysym=='Left': x=-1 if k.keysym=='Right': x=1 command='M'+str(x)+str(y) ser.write(command.encode('utf-8')) line=ser.readline().decode('utf-8').rstrip() print(line) root.bind('', keypressed) root.mainloop()