Как обновить объекты в Tkinter Canvas с поступающими последовательными данными? - PullRequest
0 голосов
/ 28 февраля 2020

Я создал этот полукруг, который принимает значения из Arduino (Serial), а затем вращается в соответствии с этими значениями. Проблема в том, что холст Tkinter также сохраняет старый круг. Я просто показываю мне новый и удаляю старый. Код прилагается ниже.

import serial
import time    
from tkinter import *
import math
#-----------------------------------------------
ser = serial.Serial('COM5',baudrate = 9600)
ser.flushInput()
#-----------------------------------------------   
def tilt():
    ser_bytes = int(ser.readline().decode('ascii'))
    adata = (ser.readline().strip())
    ser_bytes = str(adata.decode('utf-8'))
    angle = int(ser_bytes)-247
    arc = c.create_arc(50, 50, 200, 200,start =angle,extent=-180, fill="red")
    c.delete(arc)
    root.after(100,tilt)
    print('X: {}' .format(ser_bytes))

root = Tk()
root.title("Control Panel")
root.geometry('1200x750')
frame_1 = Frame(root)
frame_1.pack()
c = Canvas(frame_1,width = 1000, height = 1000, bg = '#5F9EA0')
c.pack()
tilt()    
root.mainloop()

Ответы [ 2 ]

0 голосов
/ 01 марта 2020

Вы должны создать arc один раз при запуске программы, а затем использовать canvas.itemconfig(), чтобы изменить угол start:

import serial
from tkinter import *
#-----------------------------------------------
ser = serial.Serial('COM5', baudrate=9600)
ser.flushInput()
#-----------------------------------------------
def con():
    ser_bytes = int(ser.readline().decode('ascii'))
    return ser_bytes

def tilt():
    angle = con()
    c.itemconfig(arc, start=angle)
    root.after(100, tilt)
    print('X: {}'.format(angle))

root = Tk()
root.title("Control Panel")
root.geometry('1200x750')
frame_1 = Frame(root)
frame_1.pack()
c = Canvas(frame_1, width=1000, height=1000, bg='#5F9EA0')
c.pack()
# create the arc
arc = c.create_arc(50, 50, 200, 200, start=0, extent=-180, fill="red")
# start updating the arc
tilt()    
root.mainloop()
0 голосов
/ 28 февраля 2020

Используя itemconfig я решил проблему. Вот окончательный рабочий код.

import serial
import time    
from tkinter import *
import math
##------------------------------------------##
##------------------------------------------##
ser = serial.Serial('COM5',baudrate = 9600)
ser.flushInput()
##------------------------------------------##
## This function retrieves values from Serial ##
##------------------------------------------##
def con():
    ser_bytes = str(ser.readline().decode('ascii'))
    datasplit = ser_bytes.split(',')
    x = int(datasplit[0])
    y = int(datasplit[1])
    z = int(datasplit[2])
    return x,y,z
##------------------------------------------##
## Creating and Updating arcs for animation ##
##------------------------------------------##
def tilt():
    x,y,z = con()
    c.itemconfig("rect",start = x) # Updates values of arcs start angle.
    c_1.itemconfig("rect",start = y)# "rect" acts as a comman tag
    c_2.itemconfig("rect",start = z)
    root.after(100,tilt)
    print(x,y,z)
##------------------------------------------##
## Main geometry and setting goes here##
##------------------------------------------##
root = Tk()
root.title("Control Panel")
root.geometry('1200x750')
frame_1 = Frame(root)
frame_2 = Frame(root)
frame_3 = Frame(root)
frame_1.grid(row = 0,column = 0)
frame_2.grid(row = 0,column = 1)
frame_3.grid(row = 0,column = 2)
c = Canvas(frame_1,width = 200, height = 200, bg = '#5F9EA0')
c_1 = Canvas(frame_2,width = 200, height = 200, bg = '#5F9EA0')
c_2 = Canvas(frame_3,width = 200, height = 200, bg = '#5F9EA0')
c.pack()
c_1.pack()
c_2.pack()
x = 0
y = 0
z = 0
arc = c.create_arc(1, 1, 200, 200,start = x,extent=-180, fill="red",tags="rect")   
arc_1 = c_1.create_arc(1, 1, 200, 200,start =y,extent=-180, fill="red",tags="rect")
arc_2 = c_2.create_arc(1, 1, 200, 200,start =z,extent=-180, fill="red",tags="rect")
tilt()    
root.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...