How to do a callback from externally triggered event

J

John Stevens

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top