Reveil en python

Contenu du snippet

Salut
c'est mon deuxieme code.Ben là c'est juste un reveil qui fonctionne correctement chez moi(lol).

Source / Exemple :


# -*- coding: iso8859-15 -*-

from Tkinter import*
import tkFileDialog,tkMessageBox
import threading
import os.path
import time
import sys

#dic_jour et dic_mois: pour juste avoir une date au format jj/mm/aaaa (pour les francophones)
dic_jour={'Mon':'Lundi','Tue':'Mardi','Wed':'Mercredi','Thu':'Jeudi','Fri':'Vendredi','Sat':'Samedi','Sun':'Dimanche'}
dic_mois={'Jan':'Janvier','Feb':'Février','Mar':'Mars','Apr':'Avril','May':'Mai',
          'Jun':'Juin','Jul':'Juillet','Aug':'Août','Sep':'Septembre','Oct':'Octobre',
          'Nov':'Novembre','Dec':'Décembre'}

def get_time():
        """Fonction qui renvoie la date du jour et l'heure"""
        d=time.asctime()
        d=d.split()
        date=dic_jour[d[0]]+" "+d[2]+" "+dic_mois[d[1]]+" "+d[4]
        heure=d[3]
        return date,heure

class Reveil(threading.Thread):
        """Gestion du reveil"""
        def __init__(self,time="",function=""):
                threading.Thread.__init__(self)
                self.time=time
                self.function=function
                self.go=threading.Event()

        def run(self):
                while not self.go.isSet():
                        b=get_time()
                        b=b[1]
                        if b==self.time:
                                print "c'est l'heure"
                                self.function()
                        else:   pass

        def arret(self):
                self.go.set()
                        
class Time(threading.Thread):
        """Affichage de l'heure et de la date"""
        def __init__(self,canev,interval=1.0):
                threading.Thread.__init__(self)
                self.canev=canev
                self.interval=interval
                self.finished=threading.Event()
                self.dic={}
                
        def run(self):
                while not self.finished.isSet():
                        self.canev.delete(ALL)
                        b=get_time()
                        x=self.canev.winfo_width()
                        y=self.canev.winfo_height()
                        self.canev.create_text(x/2,y/3,text=b[0],font='arial 12 bold',fill='red')
                        self.canev.create_text(x/2,y/2,text=b[1],font='arial 14 bold',fill='red')
                        self.finished.wait(self.interval)
                self.finished.set()
                
        def cancel(self):
                """Arrête mon Timer"""
                self.finished.set()
                
class Application(Frame):
        """Mise en place de l'interface"""
        def __init__(self):
                Frame.__init__(self)
                self.master.title("::.Reveil.::-By Kouakou021988")
		self.master.iconbitmap("c:/Mon Reveil/côte d'ivoire.ico")
                self.configure(bg="grey40",bd=0,relief=FLAT)
                self.master.resizable(width=False, height=False)
                self.pack(padx =8, pady =8)
                self.option=['Activer','Désactiver']
                self.choixOption=StringVar()
                self.path_odio=""                                           #Chemin des fichiers audio
                self.liste=[]                                               #Enregistre les chemins des fichiers audio
                self.build()
                
        def build(self):
                """Pour l'interface"""
                Label(self,text="Reveil: ",relief=FLAT,font='arial 8 bold').grid(row=0,column=0,padx=2,pady=5)
                self.ent_reveil=Entry(self,width=10,font='arial 10 bold',relief=FLAT,fg='black')
                self.ent_reveil.grid(row=0,column=1,padx=4,pady=5)
                self.bout1 = Radiobutton(self,text = self.option[0],variable = self.choixOption,value = self.option[0],
                                         relief=FLAT,command = self.optionReveil)
                self.bout1.grid(row=0,column=2,pady=5,padx=1)
                self.bout = Radiobutton(self,text = self.option[1],variable = self.choixOption,value = self.option[1],
                                         relief=FLAT,command = self.optionReveil)
                self.bout.grid(row=0,column=3,pady=5,padx=1)
                self.bout.invoke()     
                Button(self,text="Stop reveil",relief=FLAT,font='arial 6',
                       command= lambda r=1: self.zikquit(r)).grid(row=4,column=3)
                self.b=Button(self,text="Fichier Audio",relief=FLAT,font='arial 6',
                       command= lambda o=1: self.choix(o))
                self.b.grid(row=4,column=0)
                self.t_can=Canvas(self,bg='black',height=80)
                self.t_can.grid(row=5,column=0,columnspan=4)
                self.master.protocol("WM_DELETE_WINDOW",self.quitter)
                self.a=Time(self.t_can)
                self.a.start()
                
        def optionReveil(self):
                if self.choixOption.get()=='Activer':
                        self.ent_reveil.configure(state=DISABLED)
                        if self.controle(self.ent_reveil.get()):
                                self.re=Reveil(self.ent_reveil.get(),self.zikselect)
                                self.re.start()
                                self.bout1.configure(state=DISABLED)
                        else:
                                tkMessageBox.showerror("::.Reveil.::","Format d'heure accepté: hh:mn:ss\n hh=heure(<=23)\nmn=minutes(<=60)\nss=sécondes(<=60)")
                                self.bout.invoke() 
                                self.ent_reveil.delete(0,END)
                if self.choixOption.get()=='Désactiver':
                        self.ent_reveil.configure(state=NORMAL)
                        self.bout1.configure(state=NORMAL)
                        self.zikquit(1)
                        try:
                                self.re.arret()
                        except: pass
                      
        def controle(self,txt=""):
                """Controle du format attendu: hh:mn:ss"""
                lst=txt.split(":")
                if len(lst)==3:
                        if int(lst[0])>23 or int(lst[1])>60 or int(lst[2])>60:  return 0
                        else:   return 1
                else:
                        return 0
                

        def choix(self,o=0):
                """Choix du fichier à utiliser pour le reveil"""
                if o:
                        self.liste=[]
                        self.path_odio=tkFileDialog.askopenfilename(filetypes=[("Fichier audio",".mp3")])
                        if self.path_odio:
                                self.liste.append(self.path_odio)
                else:   pass

        def zikselect(self):
                """Lecture du fichier audio.Ici lecture en boucle jusqu'a ce que zikquit(1)"""
                if len(self.liste):
                        d=os.path.split(self.liste[0])
                        d=d[1]
                        d=os.path.splitext(d)
                        s=d[0]
                        d=d[1]
                        if d=='.mp3':
                                import pymedia
                                self.player= pymedia.Player()
                                print self.player
                                self.player.setVolume(65000)
                                self.player.start()
                                while 1:
                                        self.player.startPlayback(s+d)
                                        while self.player.isPlaying():
                                                if not self.player.isPlaying(): break
                                                time.sleep( 0.01 )
                else:
                        import pymedia
                        self.player= pymedia.Player()
                        self.player.setVolume(65000)
                        self.player.start()
                        while 1:
                                self.player.startPlayback("Maggie.mp3")                 #Utiliser un fichier .mp3 par defaut
                                while self.player.isPlaying():
                                        time.sleep( 0.01 )
                                        if not self.player.isPlaying(): break
                
        def zikquit(self,r=0):
                """Pour arrêter la lecture du fichier audio"""
                if r:
                        try:
                                self.player.stop()
                        except: pass
                else:   pass
                
        def quitter(self):
                self.a.cancel()
                try:    self.re.arret()
                except: pass
                self.zikquit(1)
                self.master.destroy()
                sys.exit()
                
###--------------------------------------------------------------##############
if __name__=='__main__':
        app=Application()
        app.mainloop()

Conclusion :


Merci

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.