package python ************** l'import d'un package ce fait par .. code-block:: python import NameOfPackage ou avec un literal .. code-block:: python moduleOne = __import__('moduleOne') Présentation ============ Nous allons créer un package python fao. Le package sera composer ainsi : * un package fao * un module gui * deux classes contenues dans fao.gui * un répertoire de test (avec le module unnitest) package ======= * LICENSE.txt * MANIFEST.in * README.txt * setup.py * tests - pythonHand.gif - python-logo.svg - test_fao_gui.py * fao - {{{__init__.py}}} - gui.py liste de definition : terme défini liste de definition : description Contenu ======= LICENSE.txt ----------- contient votre license MANIFEST.in ----------- contient la liste des fichiers à incorporer dans le package .. code-block:: bash recursive-include fao *.py recursive-include tests *.* README.txt ---------- contient toutes les informations sur le package .. code-block:: bash Project ------- fao -- widget Advanced for ttk and tk Prerequisites: * Python 2.6 or higher. Whats new in version 0.1? * splashScreen Installation ------------ 1. unzip source 2. python setup.py install Authors ------- Aoustin Frederic License ------- LGPL, see http://www.opensource.org/licenses/lgpl-license.php Relase history -------------- 1.0 the first release setup.py -------- Le fichier sera utilisé comme argument pour créer le package. Ll contient les informations de version, d’auteur, ... . .. code-block:: python #!/usr/bin/env python # -*- encoding: utf8 -*- __author__ = "Frédéric Aoustin" __date__ = "29 September 2009" __version__ = "$Revision: 0.1 $" __credits__ = "" from distutils.core import setup PACKAGE = 'fao' VERSION = '0.1' MAIL='none@none.fr' URL='http://none.fr' setup(name=PACKAGE, url=URL, version=VERSION, author=__author__, author_email=MAIL, packages=['fao'] ) fao\__init__.py --------------- Il est lancé lors de l’importation du module fao .. code-block:: python #!/usr/bin/env python # -*- encoding: utf8 -*- __author__ = "Frédéric Aoustin" __date__ = "29 September 2009" __version__ = "$Revision: 0.1 $" __credits__ = "" from fao import * fao\gui.py ---------- Il s’agit du module a proprement parlé .. code-block:: python #!/usr/bin/env python # -*- encoding: utf8 -*- __author__ = "Frédéric Aoustin" __date__ = "29 September 2009" __version__ = "$Revision: 0.1 $" __credits__ = "" import os import tkinter as tk import time class SplashScreen(tk.Toplevel): """a splashscreen for application gui""" def __init__(self, master, image=None, timeout=1): """create a splash screen from a specified image file keep splash screen up for timeout milliseconds """ # test entry assert os.path.exists(image) tk.Toplevel.__init__(self, master, relief='flat', borderwidth=0) self.main = master self._minSplashTime = time.time() + timeout # don't show main window self.main.withdraw() self.overrideredirect(1) # use Tkinter's PhotoImage for .gif files self.image = tk.PhotoImage(file=image) self.after_idle(self.__centerOnScreen) self.update() def __centerOnScreen(self): self.update_idletasks() self.width, self.height = self.image.width(), self.image.height() xmax = self.winfo_screenwidth() ymax = self.winfo_screenheight() x0 = self.x0 = xmax/2 - self.width/2 y0 = self.y0 = ymax/2 - self.height/2 self.geometry("%dx%d+%d+%d" % (self.width, self.height, x0, y0)) self.createSplash() def createSplash(self): """show the splash image""" self.canvas = tk.Canvas(self, height=self.height, width=self.width) self.canvas.create_image(0,0, anchor='nw', image=self.image) self.canvas.pack() def destroySplash(self): """if Make sure the minimum splash time has elapsed the splash Screen is destroy """ timeNow = time.time() if timeNow < self._minSplashTime: time.sleep( self._minSplashTime - timeNow ) self.main.update() self.main.deiconify() self.withdraw() tests\test_fao_gui.py --------------------- Ce fichier comprend une classe héritante de la classe unittest.TestCase et une suite de fonction commançant par `test_` qui sont les tests. Pour effectuer le test il suffit de lancer .. code-block:: python #!/usr/bin/env python # -*- encoding: utf8 -*- __author__ = "Frédéric Aoustin" __date__ = "29 September 2009" __version__ = "$Revision: 0.1 $" __credits__ = "" import os import time import unittest import tkinter as tk from fao.gui import SplashScreen class FaoGuiTestCase(unittest.TestCase): def test_splash_screen(self): """test of fao.gui.SplashScreen""" # main window root = tk.Tk() # pick a file you have (has to be a GIF image file) ... # change your .jpg to a .gif with an image utility like xnview or irfanview image_file = "pythonHand.gif" s = SplashScreen(root, timeout=5, image=image_file) print('suite') lbl = tk.Label(root, text="This was a test of the gif image splash screen", bg='yellow') lbl.grid(row=0, column=0, padx=5, pady=5) time.sleep(10) s.destroySplash() root.mainloop() if __name__ == '__main__': unittest.main() .. code-block:: bash python test_fao_gui.py Création /installation des packages =================================== Il suffit de ce placer dans le repertoire package puis de lancer .. code-block:: bash python setup.py sdist cela va générer dans le répertoire build une archive ayant pour nom package_version.zip .. note:: il est possible d’avoir une archive avec les fichiers binaires (*.pyc) si on lance la commande python setup.py bdist Pour maintenant installer ce package il faut * décompresser l’archive * se mettre au niveau du fichier setup.py puis lancer .. code-block:: bash python setup.py install et voila le package est installé !!!