I'm trying to make a program that reads the serial of my arduino and shows the information in real time in a GUI. The problem is that I don't know how to make the data update in real time.
This is my current code:
import re
import serial
import PySimpleGUI as sg
class InterfaceGrafica:
def __init__(self):
layout = [[sg.Text('Interface gráfica para o controle do secador.')],
[sg.Text('Valor do controle PWM:'), sg.Text(Dados().pwm, key = 'pwm')],
[sg.Text('Valor da temperatura: '), sg.Text(Dados().temp, key = 'temp')],
[sg.Text('Peso: '), sg.Text(Dados().peso, key = 'peso')],
[sg.Text('Valor da umidade: '), sg.Text(Dados().um, key = 'um')],
[sg.Text('Temperatura setpoint: '), sg.Text(Dados().set, key = 'set')],
[sg.Button('Exit')]]
self.janela = sg.Window('Versão final', layout=layout, size=(600, 600))
def Iniciar(self):
while True:
self.event, self.values = self.janela.Read()
if self.event == sg.WIN_CLOSED or self.event == 'Exit':
break
conecao = serial.Serial("COM3", 9600)
for i in range(20):
leitura = conecao.readline()
leiturad = leitura.decode()
var = re.split(',', leiturad)
print(leiturad)
self.janela.Element('pwm').Update(values=var[0])
janela.Refresh
self.janela.Element('temp').Update(values=var[1])
self.janela.Element('peso').Update(values=var[2])
self.janela.Element('um').Update(values=var[3])
self.janela.Element('set').Update(values=var[4])
tela = InterfaceGrafica()
tela.Iniciar()