How to do a callback from externally triggered event

John Stevens js at 75andsunny.com
Sun Feb 17 18:16:43 EST 2008


I am using PIL to make images that I need to display in a sequence.
The image needs to change when a an event happens from a serial port.

I call the following function to display the image, but then the
application is waiting for an event.  I need to return to the  main
code which is sending commands to a serial port and waiting for data
to return then displaying the next chart.

def display(R,G,B):
        img = Image.new('RGB',(1000,1000),(24,24,24))
        draw = ImageDraw.Draw(img)
        draw.rectangle((400,400,600,600), fill=(R,G,B))

        root = Tkinter.Tk()
        photo = ImageTk.PhotoImage(img)
        label = Tkinter.Label(root, image=photo)
        label.pack()
        root.mainloop()


Here is the (very)rough program
#!/usr/local/bin/pythonw

# Import needed modules
import Tkinter, ImageTk
import Image, ImageDraw
import time, serial

def measure():
        ser.write("M\n")
        line = ser.readline()
        print line,

def display(R,G,B):
        img = Image.new('RGB',(1000,1000),(24,24,24))
        draw = ImageDraw.Draw(img)
        draw.rectangle((400,400,600,600), fill=(R,G,B))

        root = Tkinter.Tk()
        photo = ImageTk.PhotoImage(img)
        label = Tkinter.Label(root, image=photo)
        label.pack()
        root.mainloop()


def setup_comm():
        ser = serial.Serial('/dev/tty.KeySerial1')
        ser.write("PR701\n")
        time.sleep(.2)
        ser.flushOutput()
        line = ser.readline()
        print line


# Program Starts here

setup_comm()
display(0, 255, 255)
measure()
display(255, 0, 255)
measure()
display(255, 255, 0)
measure()



More information about the Python-list mailing list