I have serial data that can come in at irregular intervals from a hand held barcode scanner. The scanner continuously looks for a barcode (no button or trigger is ever pressed) and when the scanner captures a valid barcode it sends the code to the PC over serial.

What I want to do is have a Tkinter GUI (Windows XP) where the serial codes are passed to the GUI when a barcode is read. What is the best (easy) way to do this?


Edited 6 Years Ago by kingmu: n/a

Here it is! http://code.activestate.com/recipes/82965-threads-tkinter-and-asynchronous-io/. Copied below:

import Tkinter
import time
import threading
import random
import Queue

class GuiPart:
    def __init__(self, master, queue, endCommand):
        self.queue = queue
        # Set up the GUI
        console = Tkinter.Button(master, text='Done', command=endCommand)
        # Add more GUI stuff here

    def processIncoming(self):
        Handle all the messages currently in the queue (if any).
        while self.queue.qsize():
                msg = self.queue.get(0)
                # Check contents of message and do what it says
                # As a test, we simply print it
                print msg
            except Queue.Empty:

class ThreadedClient:
    Launch the main part of the GUI and the worker thread. periodicCall and
    endApplication could reside in the GUI part, but putting them here
    means that you have all the thread controls in a single place.
    def __init__(self, master):
        Start the GUI and the asynchronous threads. We are in the main
        (original) thread of the application, which will later be used by
        the GUI. We spawn a new thread for the worker.
        self.master = master

        # Create the queue
        self.queue = Queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue, self.endApplication)

        # Set up the thread to do asynchronous I/O
        # More can be made if necessary
        self.running = 1        
        self.thread1 = threading.Thread(target=self.workerThread1)

        # Start the periodic call in the GUI to check if the queue contains
        # anything

    def periodicCall(self):
        Check every 100 ms if there is something new in the queue.
        if not self.running:
            # This is the brutal stop of the system. You may want to do
            # some cleanup before actually shutting it down.
            import sys
        self.master.after(100, self.periodicCall)

    def workerThread1(self):
        This is where we handle the asynchronous I/O. For example, it may be
        a 'select()'.
        One important thing to remember is that the thread has to yield
        while self.running:
            # To simulate asynchronous I/O, we create a random number at
            # random intervals. Replace the following 2 lines with the real
            # thing.
            time.sleep(rand.random() * 0.3)
            msg = rand.random()

    def endApplication(self):
        self.running = 0

rand = random.Random()
root = Tkinter.Tk()

client = ThreadedClient(root)

Edited 6 Years Ago by kingmu: n/a

This question has already been answered. Start a new discussion instead.