Quantcast
Viewing all articles
Browse latest Browse all 2703

how to display irrespective length of serial data on Tkinter Window.

This code is to receive serial data and to print on the TKinter window. The received data prints on the window from top to bottom.

I want 1 line of received data to print at the middle of the window, instead of printing from top. Irrespective of the length of serial data, it should print on the window by leaving equal size of the border space on the window.



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):
self.text.delete(1.0, 'end')
while self.queue.qsize():
try:
self.text.insert('end', self.queue.get())
except Queue.Empty:
pass
self.after(100, self.process_serial)

app = App()
app.mainloop()






Viewing all articles
Browse latest Browse all 2703

Trending Articles