from Tkinter import Tk,Canvas,Frame,Label,Button,PhotoImage,BOTTOM,BOTH,Y,NW,SUNKEN import threading from time import sleep,strftime from datetime import datetime,timedelta,time,date import tkFont import os from urllib2 import urlopen from PIL import Image, ImageTk class Objetvideo_live(threading.Thread): def __init__(self,controle): threading.Thread.__init__(self) self.controle= controle self._fin = threading.Event() ip_query = '/snapshot.cgi?user=demo&pwd=demo' self.ip = 'http://as6084.myfoscam.org:8904' + ip_query picLocation = './images/' picName = 'camera' self.jpg = picLocation + picName + '.jpg' photo = Image.open(self.jpg) rdim = photo.resize((480,360), Image.ANTIALIAS) tk_photo = ImageTk.PhotoImage(rdim) self.cameraPhoto=self.controle.create_image(0, 0, image=tk_photo,anchor=NW) def run(self): while not self._fin.isSet(): #if not self._fin.isSet(): response = urlopen(self.ip) file = open(self.jpg, 'wb') file.write(response.read()) file.close() print "url ok" #if not self._fin.isSet(): photo = Image.open(self.jpg) print "Image open" #if not self._fin.isSet(): rdim = photo.resize((480,360), Image.ANTIALIAS) print "image redim" #if not self._fin.isSet(): tk_photo = ImageTk.PhotoImage(rdim) print "tk_photo en memoire" #if not self._fin.isSet(): self.controle.itemconfig(self.cameraPhoto, image = tk_photo) print "image update" #self._fin.wait(2.0) def arretetoi(self): self._fin.set() print "stop video" class CanvasVideo(Canvas): def __init__(self, parent): global threads self.largeurScreen=parent.winfo_screenwidth() self.hauteurScreen=parent.winfo_screenheight() frameVideo = Frame(parent, bg="black", width=self.largeurScreen/2,height=400, padx=1, pady=1, bd = 2, relief = SUNKEN) frameVideo.pack(side=BOTTOM,fill=BOTH) frameVideo.pack_propagate(0) Canvas.__init__(self, frameVideo,bg='black',bd=0,highlightthickness=0,width=self.largeurScreen/2,height=400) self.objvideo_live = Objetvideo_live(self) threads.append(self.objvideo_live) class Application: def __init__(self): global root,threads, rep_images rep_images ="./images" threads = [] root=Tk() root.configure(bg='black') self.largeurScreen=root.winfo_screenwidth() frameAction = Frame(root, bg="black", height=50, width=self.largeurScreen, padx=5, pady=5, bd = 2) frameAction.pack(side=BOTTOM) self.canvas_video = CanvasVideo(root) self.canvas_video.pack(fill=Y) self.BoutonQuitter=Button(frameAction, bg='black',fg='orange',text="Quitter",activebackground='black',activeforeground='orange', command=self.stopapli) self.BoutonQuitter.pack() self.canvas_video.objvideo_live.start() root.mainloop() def stopapli(self): for elements in threads: print elements.getName() elements.arretetoi() elements.join(10) if elements.isAlive(): elements._Thread__stop() del elements root.destroy() if __name__ == "__main__": app = Application()