How to do a callback from externally triggered event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Stevens

    How to do a callback from externally triggered event

    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),(2 4,24,24))
    draw = ImageDraw.Draw( img)
    draw.rectangle( (400,400,600,60 0), fill=(R,G,B))

    root = Tkinter.Tk()
    photo = ImageTk.PhotoIm age(img)
    label = Tkinter.Label(r oot, 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),(2 4,24,24))
    draw = ImageDraw.Draw( img)
    draw.rectangle( (400,400,600,60 0), fill=(R,G,B))

    root = Tkinter.Tk()
    photo = ImageTk.PhotoIm age(img)
    label = Tkinter.Label(r oot, image=photo)
    label.pack()
    root.mainloop()


    def setup_comm():
    ser = serial.Serial('/dev/tty.KeySerial1' )
    ser.write("PR70 1\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()
Working...