problem to display serial data on Tkinter window

Post here if you need help with creating a Graphical User Interface in Python.

problem to display serial data on Tkinter window

Postby srinivas.rambha » Mon Jun 17, 2013 11:59 am

This code is to receive serial data and print on the Tkinter window. This code is working fine for receiving single line of serial data and printing on the Tkinter window.When it receive serial data again, the displaying data will wipe out and newly received serial data will print on the Tkinter window.This should happen every time it receives serial data.But the problem is, if it receives multiple lines( for example: 5 lines) of serial data, it prints entire 5 lines of received serial data at first line on the Tkinter window, and just displays left over few characters on the Tkinter window.

IF any one had two pc's, please run the code in 1 pc, and receive serial data coming from other pc.
please help me out, breaking my head with this from few days, i hope some one will help me out, THANK YOU.


Code: Select all
import serial
import threading
import Queue
import Tkinter as tk


class SerialThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        s = serial.Serial('COM10',9600, timeout=10)
        s.bytesize = serial.EIGHTBITS #number of bits per bytes
        s.parity = serial.PARITY_NONE #set parity check: no parity
        s.stopbits = serial.STOPBITS_ONE #number of stop bits

        while True:
            if s.inWaiting():
                text = s.readline(s.inWaiting())
                self.queue.put(text)

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("1360x750")
        frameLabel = tk.Frame(self, padx=40, pady =40)
        self.text = tk.Text(frameLabel, wrap='word', font='TimesNewRoman 37',
                            bg=self.cget('bg'), relief='flat')
        frameLabel.pack()
        self.text.pack()
        self.queue = Queue.Queue()
        thread = SerialThread(self.queue)
        thread.start()
        self.process_serial()

    def process_serial(self):
        while self.queue.qsize():
            try:
                self.text.delete(1.0, 'end')  # to delete previous content on window
                self.text.insert('end', self.queue.get())
            except Queue.Empty:
                pass
        self.after(100, self.process_serial)

app = App()
app.mainloop()
Last edited by micseydel on Mon Jun 17, 2013 12:32 pm, edited 1 time in total.
Reason: Added code tags.
srinivas.rambha
 
Posts: 1
Joined: Mon Jun 17, 2013 11:38 am

Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests