Update text in TkInter GUI?
by RonHof from LinuxQuestions.org on (#4YF6C)
I'm trying to read a serial data stream and display it in a tkinter GUI on a Raspberry Pi. The following code seems to work properly.
Code:#!/usr/bin/env python
import time
import serial
import string
ser = serial.Serial (port='/dev/ttyUSB0',
baudrate = 4800,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1)
while 1:
Str = ser.read_until(b'\r')
print (Str)and produces the followng typical output
Quote:
I would like to display that text in a gui as it updates. The following code produces a window with the first line of text but it doesn't update with each new line of text.
Code:#!/usr/bin/env python3
from tkinter import *
from tkinter import Label
import serial
#root = Tk()
ser = serial.Serial (port='/dev/ttyUSB0',
baudrate = 4800,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1)
while TRUE:
Str = ser.read_until(b'\r')
#print (Str)
labelfont = ('times', 350, 'bold')
widget = Label(None, text = Str)
widget.config(bg='black', fg='red')
widget.config(font=labelfont)
widget.config(height=3, width=20)
widget.pack(expand=YES, fill=BOTH)
widget.mainloop()What am I missing?
Thanks


Code:#!/usr/bin/env python
import time
import serial
import string
ser = serial.Serial (port='/dev/ttyUSB0',
baudrate = 4800,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1)
while 1:
Str = ser.read_until(b'\r')
print (Str)and produces the followng typical output
Quote:
b' 1.36\r' b' 1.36\r' b' 1.36\r' b' 1.36\r' b' 1.36\r' b' 1.36\r' b' 1.36\r' b' 1.36\r' b' 0.06\r' b' 0.10\r' b' 0.15\r' |
Code:#!/usr/bin/env python3
from tkinter import *
from tkinter import Label
import serial
#root = Tk()
ser = serial.Serial (port='/dev/ttyUSB0',
baudrate = 4800,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1)
while TRUE:
Str = ser.read_until(b'\r')
#print (Str)
labelfont = ('times', 350, 'bold')
widget = Label(None, text = Str)
widget.config(bg='black', fg='red')
widget.config(font=labelfont)
widget.config(height=3, width=20)
widget.pack(expand=YES, fill=BOTH)
widget.mainloop()What am I missing?
Thanks