I am using Tkinter to visualize sensor data. I have serial data being read, and I want to use it to move a square on the screen.
from tkinter import Tk, Canvas
import tkinter
import serial
#GUI
root = Tk()
c = Canvas(root, width=1000, height=50)
c.configure(bg='grey', highlightthickness=0)
#Position Parameters
posX=10
posY=20
lenght=10
subject = c.create_rectangle(posX, posY, posX+lenght, posY+lenght, outline='black', fill='black')
#Serial Data Read
ser = serial.Serial('COM5', baudrate=9600, timeout=0.2) # open serial port
print(ser.name) # check which port was really used
#Failed Live Visual
while True:
while (ser.inWaiting()==0):
pass
dataPacket=ser.readline()
dataPacket=str(dataPacket, 'utf-8') #Incoming data from arduino into string
dataPacket=int(dataPacket.strip(' \r\n')) #Formatting
posX=dataPacket
c.coords(subject, posX, posY, posX+lenght, posY+lenght)
c.pack()
root.mainloop()
My issue is: that Tkinter will display but not update the position of the square. I've tried different approaches such as using the c.move() setter, as well as creating a function and calling it with the root.after() command but none seem to work. I don't think this should be so difficult, so that's why I ask.
Thank you for your time!