"""
Hi all,

I am relatively new to Tk and after following some examples from various
sources, I have managed to write the (drastically simplified for posting
purposes) code below.  I'm sure that there are some glaring issues with the
code and I'm not quite in touch with what I'm doing, but all I need to do is
to get a prototype working.

My problem is that I need to start a server which a client will connect to
and send some image data over tcp. These images are sent at a rate of say 10 
frames per second and I want to write the data to file and display it on a
Tk
window. I also want to show multiple frames as the data arrives without 
opening a new window each time.

So, I have three classes: 1)ThreadedClient which launches the gui and starts
a
server thread 2)TCPServer which is the server whose handle() method is
called
when there's data to be processed and 3)Window which is the gui.

The server thread works fine i.e it receives image data, writes the data to
a file
and puts a number on the queue to identify the image file. Note that the
queue is global,I haven't yet figured out how to pass it to the server but
it works fine for now, I think.
 
Also, if I click on the main window using a mouse button, the event handler
paint()
is called fine and it loads the image every time I make a mouse click. 

Now I don't want the handler to be called only when the user clicks on the
window,
I would like it to be called periodically to check if there's something in
the queue. 
That's what I intended to do with the periodicCall() method in the
ThreadedClient class. The problem is, when I attempt to generate the mouse
event, it blocks and so it is called only once. I'm not really sure how to
get the mouse event generated periodically.

I have run out of ideas about how to do this, so I would appreciate any
help.

Cheers,
Mezesh

---
"""

import Tkinter,ImageTk,Image
import threading,os,sys,Queue





class Window:
    def __init__(self,master,queue):
        self.master = master
        self.master.bind("<Button>", self.button_click_exit_mainloop)
        self.master.geometry('+%d+%d' % (100,100))
        self.queue = queue
        self.old_label_image = None
    
    def button_click_exit_mainloop(event):
        event.widget.quit() # this will cause mainloop to unblock

    def paint(self):
        # proceed only if the queue is not empty
        if self.queue.qsize(): 
            frame = Image.open("frame%i.pgm" % self.queue.get(0))
            self.master.geometry('%dx%d' % (frame.size[0],frame.size[1]))
            photoImage = ImageTk.PhotoImage(frame)
            label_image = Tkinter.Label(self.master,image = photoImage)
           
label_image.place(x=0,y=0,width=frame.size[0],height=frame.size[1])
            self.master.title('Frame')
            if self.old_label_image is not None:
                self.old_label_image.destroy()
            self.old_label_image = label_image
            self.master.mainloop()

globalQueue = Queue.Queue()

class TCPServer(SocketServer.BaseRequestHandler):
    def setup(self):
        self.count = 0
    def handle(self):
        data = self.request.recv(1024)
        self.writeImageFileFromData(data)
        
    def writeImageFileFromData(self.data):
        #parse data and write image file
        global globalQueue
        globalQueue.put(self.count)
        self.count += 1
        
class ThreadedClient:
    def __init__(self,master):
        global globalQueue
        self.gui = Window(master,globalQueue)
        self.thread1 = threading.Thread(target=self.serverThread)
        self.thread1.start()

    def serverThread(self):
         # SERVER_ADDRESS and SERVER_PORT are globally defined variables
         server = SocketServer.TCPServer((SERVER_ADDRESS, SERVER_PORT),
TCPServer)
         # blocking method
         server.handle_request()

    def periodicCall(self):
        self.master.event_generate("<Button>")
        self.master.after(10,self.periodicCall)

if __name__== "__main__":
    root == Tkinter.Tk()
    client == ThreadedClient(root)
    root.mainloop()

-- 
View this message in context: 
http://www.nabble.com/Displaying-multiple-image-frames-sequentially-on-the-same-window-tp18711958p18711958.html
Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to