Wx_reveil

Contenu du snippet

Salut
Pour mon initiation à wxpython j'ai décidé de reprendre un code. Ben j'attends vos commentaires.

Source / Exemple :


#-*- coding: cp1252 -*-

import sys
import os
import sys
import time
import wx
import wx.gizmos as gizmos
import pymedia
import threading
from wx.lib.wordwrap import wordwrap
import glob

ID_CONFIG=100
ID_HELP=101
ID_PROPOS=102

reveil_actif=0
time_ok=0
image="C:\Documents and Settings\All Users\Documents\Mes images\Échantillons d'images"
son="Angel.mp3"#fichier audio par defaut
n=0
nb=0
                
class MyThread(threading.Thread):
        def __init__(self,function=""):
                threading.Thread.__init__(self)
                self.function=function
                self.go=threading.Event()
                
        def run(self):
                global time_ok
                while not self.go.isSet():
                        if time_ok: self.function()
                        else:   pass

        def arret(self):
                self.go.set()
                
class Fen(wx.Frame):
        def __init__(self):
                wx.Frame.__init__(self,None,-1,size=(300,150),style=wx.DEFAULT_FRAME_STYLE^(wx.RESIZE_BORDER
                                                                                              |wx.MAXIMIZE_BOX
                                                                                              |wx.MINIMIZE_BOX))
                frameicon = wx.Icon("./côte d'ivoire.ico", wx.BITMAP_TYPE_ICO)
                self.SetIcon(frameicon)
                self.SetTitle("::.Reveil 2.::-By Kouakou021988")
                self.panel = wx.Panel(self, -1)
                self.builder()

        def builder(self):
                wx.StaticText(self.panel,-1,"Fichier Audio: ",style=wx.ALIGN_LEFT,pos=(15,15))
                self.p_audio=wx.TextCtrl(self.panel,-1,"Angel.mp3", size=(150,-1),pos=(100,10))
                b_audio=wx.Button(self.panel,-1,"...",pos=(250,8),size=(25,25))
                wx.StaticText(self.panel,-1,"Dossier Image: ",style=wx.ALIGN_LEFT,pos=(15,50))
                self.p_image=wx.TextCtrl(self.panel,-1,"C:\Documents and Settings\All Users\Documents\Mes images\Échantillons d'images",
                                         size=(150,-1),pos=(100,45))
                b_image=wx.Button(self.panel,-1,"...",pos=(250,43),size=(25,25))
                b_Ok=wx.Button(self.panel,-1,"OK",pos=(60,80),size=(80,25))
                b_Annuler=wx.Button(self.panel,-1,"Annuler",pos=(150,80),size=(100,25))
                self.Bind(wx.EVT_BUTTON,self.open_image,b_image)
                self.Bind(wx.EVT_BUTTON,self.open_audio,b_audio)
                self.Bind(wx.EVT_BUTTON,self.close,b_Annuler)
                self.Bind(wx.EVT_BUTTON,self.reception,b_Ok)
                self.Bind(wx.EVT_CLOSE,self.close)

        def open_audio(self,event):
                global son
                wildcard = "Fichier Audio (*.mp3)|*.mp3|"
                dialog = wx.FileDialog(self, "Audio", os.getcwd(),"", wildcard, wx.OPEN)
                if dialog.ShowModal() == wx.ID_OK:
                        self.p_audio.SetValue(dialog.GetPath())
                else:   son="angel.mp3"
                dialog.Destroy()

        def open_image(self,event):
                global image
                dialog = wx.DirDialog(self, "Choisir un repertoire photo:",style=wx.DD_DEFAULT_STYLE ^ (wx.DD_NEW_DIR_BUTTON
                                                                                                        |wx.RESIZE_BORDER))
                if dialog.ShowModal() == wx.ID_OK:
                        self.p_image.SetValue(dialog.GetPath())
                else:   image="C:\Documents and Settings\All Users\Documents\Mes images\Échantillons d'images"
                dialog.Destroy()

        def reception(self,event):
                global son,image
                son=self.p_audio.GetValue()
                image=self.p_image.GetValue()
                if not son or not os.path.exists(son):     son="angel.mp3"
                if not image or not os.path.exists(image):   image="C:\Documents and Settings\All Users\Documents\Mes images\Échantillons d'images"
                c=glob.glob(os.path.abspath(image)+"\*.jpg")
                if len(c)==0:   image="C:\Documents and Settings\All Users\Documents\Mes images\Échantillons d'images"
                self.close(None)
        
        def close(self,event):
                self.MakeModal(False)
                self.Destroy()
        
class Reveil(wx.Frame):
        def __init__(self):
                wx.Frame.__init__(self,None,-1,size=(225,450),title="::.Reveil 2.::-By Kouakou021988",
                                  style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
                frameicon = wx.Icon("./côte d'ivoire.ico", wx.BITMAP_TYPE_ICO)
                self.SetIcon(frameicon)
                self.text=""
                try:
                        os.unlink("florent.exe.txt")
                except: pass
                self.build()
                
        def build(self):
                self.menuFichier=wx.Menu()
                self.menuFichier.Append(ID_CONFIG, "Configuration")
                self.menuFichier.Append(wx.ID_CLOSE, "Quitter")
                menuAide=wx.Menu()
                menuAide.Append(ID_HELP, "Aide")
                menuAide.Append(ID_PROPOS, "A Propos")
                menuBarre=wx.MenuBar()
                menuBarre.Append(self.menuFichier,"Fichier")
                menuBarre.Append(menuAide,"?")
                self.SetMenuBar(menuBarre)
                wx.EVT_MENU(self,wx.ID_CLOSE,self.fermer)
                wx.EVT_MENU(self,ID_CONFIG,self.configure)
                wx.EVT_MENU(self,ID_HELP,self.aide)
                wx.EVT_MENU(self,ID_PROPOS,self.a_propos)
                self.conteneur=wx.Panel(self,-1)
                wx.StaticText(self.conteneur,-1,"Reveil: ",style=wx.ALIGN_LEFT,pos=(10,15))
                self.r_time=wx.TextCtrl(self.conteneur,-1,size=(60,-1),pos=(50,10))
                self.active= wx.CheckBox(self.conteneur, -1, " Activer ",pos=(150,15))
                led = gizmos.LEDNumberCtrl(self.conteneur, -1, (0,50), (230,50),gizmos.LED_ALIGN_CENTER)
                self.clock = led
                self.clock.SetForegroundColour("red")
                self.onTimer(None)
                self.timer = wx.Timer(self)
                self.timer.Start(1000)
                self.Bind(wx.EVT_TIMER, self.onTimer)
                self.Bind(wx.EVT_CHECKBOX, self.r_active, self.active)
                self.Bind(wx.EVT_CLOSE, self.fermer)
                self.photo("ntic2.jpg")
                
        def onTimer(self, evt):
                global image,time_ok,reveil_actif,n
                t = time.localtime(time.time())
                st = time.strftime("%H-%M-%S", t)
                self.clock.SetValue(st)
                n+=1
                if n>10:
                    self.onTof()
                    n=0
                if st==self.text:
                    if reveil_actif:
                        time_ok=1
                        self.re.start()
                        
        def onTof(self):
                global image,nb
                image_path=glob.glob(os.path.abspath(image)+"\*.jpg")
                self.p.Destroy()
                last=image_path[nb-1]
                try:
                        self.photo(image_path[nb])
                except:
                        image_path=glob.glob(os.path.abspath(image)+"\*.jpg")
                        self.photo(last)
                        nb=0
                else:   nb+=1

        def shutdown(self):
                self.timer.Stop()
                del self.timer

        def r_active(self,event):
                global reveil_actif
                if self.active.IsChecked():
                        self.text=self.r_time.GetValue()
                        self.r_time.Enable(False)
                        if not self.controle(self.text):
                                dlg=wx.MessageDialog(self, "Format d'heure accepté: hh-mn-ss\n hh=heure(<=23)\nmn=minutes(<=60)\nss=sécondes(<=60)",
                                                 caption="::.Reveil 2.::-By Kouakou021988",style=wx.OK|wx.ICON_ERROR,pos=wx.DefaultPosition)
                                result=dlg.ShowModal()
                                dlg.Destroy()
                                self.active.SetValue(False)
                                self.r_time.Enable(True)
                        else:
                                self.menuFichier.Enable(ID_CONFIG,False)
                                reveil_actif=1
                                self.re=MyThread(self.play_music)
                else:
                        self.r_time.Enable(True)
                        self.menuFichier.Enable(ID_CONFIG,True)
                        try:
                                time_ok=0
                                self.player.stop()
                                try:
                                        self.re.arret()
                                        del self.player
                                        del self.re
                                except: pass
                        except: pass
                        reveil_actif=0
                
        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 photo(self,path):
                jpg = wx.Image(path, wx.BITMAP_TYPE_ANY)
                tof=jpg.Scale(225,300)
                self.p=wx.StaticBitmap(self.conteneur, -1, wx.BitmapFromImage(tof), pos=(0,100))
                self.Refresh()
                 
        def fermer(self,event):
                try:
                        self.shutdown()
                        time_ok=0
                        self.player.stop()
                        del self.player
                        del self.re
                        self.re.arret()
                        self.chargeur.stop()
                        del self.chargeur
                except: pass
                try:
                        os.unlink("florent.exe.txt")
                except: pass
                self.Destroy()
                sys.exit(0)

        def configure(self,event):
                fram=Fen()
                fram.CenterOnParent(wx.BOTH)
                fram.Show(True)
                fram.MakeModal(True)

        def aide(self,event):
                import webbrowser
                webbrowser.open('Reveil.html')

        def a_propos(self,event):
                info = wx.AboutDialogInfo()
                info.Name = "Reveil"
                info.Version = "II"
                info.Copyright = "(C) 2008 Itech"
                info.Description = wordwrap(
            "Cette application \" Reveil II \" est une nette amélioration du précédent "
            "\" Reveil II(bis) \" a été réalisée avec wxpython. "
            "Je débute avec wxpython et pour mon apprentissage j'ai décidé de refaire mon reveil"
            " précédement réaliser avec Tkinter."
            " J'attends vos critiques parceque j'aimerai m'amélioré.\n"
            " Merci pour tout et VIVE PYTHON.",
            400, wx.ClientDC(self))
                info.Developers = [ "Kouakou021988"]
                licenseText="GNL"
                info.License = wordwrap(licenseText, 400, wx.ClientDC(self))
                wx.AboutBox(info)
            
        def play_music(self):
                global son,time_ok
                print son
                media=os.path.splitext(son)[1]
                if media==".mp3":
                        self.player= pymedia.Player()
                        self.player.setVolume(65000)
                        self.player.start()
                        while 1:
                            self.player.startPlayback(son)
                            while self.player.isPlaying():
                                        time.sleep(0.01)
                                        try:
                                            if not self.player.isPlaying(): break
                                        except: pass
                            if time_ok==0:
                                    break
                        del self.player
                else:   self.audio_secour()
                        
        def audio_secour(self):
                global time_ok
                self.player= pymedia.Player()
                self.player.setVolume(65000)
                self.player.start()
                while 1:
                            self.player.startPlayback("Angel.mp3")
                            while self.player.isPlaying():
                                        time.sleep(0.01)
                                        try:
                                            if not self.player.isPlaying(): break
                                        except: pass
                            if time_ok==0:
                                    break
                del self.player
                        
 
if __name__ == '__main__':
        app = wx.PySimpleApp()
        Reveil().Show()
        app.MainLoop()

Conclusion :


Merci

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.