串行数据显示在控制台中,但不显示GUI

时间:2019-08-11 02:13:45

标签: python-3.x arduino-uno pyserial

我正在尝试使用串行连接来在python GUI中显示arduino数据输出(按钮状态)。但我不可以得到gui

arduino代码将输出显示为“ E”和“ D”。借助这些数据,我需要在GUI上创建实时变色圈

from tkinter import *
window = Tk()
window.geometry("1000x600")
window.resizable(0, 0) 
window.title("Rectangle movement")
canvas = Canvas(window, width = 500, height = 500)
canvas.place(bordermode=OUTSIDE)

def grnCircle():#define green cicle
    canvas.create_oval(20, 20, 80, 80, width=0, fill='green')

def redCircle():#define red circle
    canvas.create_oval(20, 20, 80, 80, width=0, fill='red')

import serial
import time
ser = serial.Serial('COM9', 9600)#Open serial port to the board
time.sleep(2) # waiting for initialization...
##print("initialising")
while True:
 data = ser.readline().decode('ascii')#read the arduino output
 print(data)

 if data=='E':
  redCircle()#if data value is"E" create a red circle on canvas
 else:   
  grnCircle()#if data value is"D" create a green circle on canvas     
ser.close() #close serial port
window.mainloop()

0 个答案:

没有答案