17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

342<br />

La bibliothèque standard<br />

TROISIÈME PARTIE<br />

Classe Application<br />

from Tkinter import *<br />

class Application(object):<br />

""" classe application """<br />

def __init__(self):<br />

self._tk = Tk()<br />

def mainloop(self):<br />

self._tk.mainloop()<br />

if __name__ == '__main__':<br />

Application().mainloop()<br />

En général, pour des mises en page élaborées, les widgets sont regroupés dans des<br />

classes dérivées de Frame. Chacune des instances de Frame gère ses widgets comme<br />

attributs et se positionne sur la fenêtre principale.<br />

Application peut aussi proposer une méthode d’ajout de Frame pour associer l’instance<br />

à un nom d’attribut. La manière la plus élégante est de fournir la classe de<br />

Frame à la méthode, et la laisser gérer l’instanciation.<br />

Exemple de création de frames<br />

from Tkinter import *<br />

class Application(object):<br />

""" classe application """<br />

def __init__(self):<br />

self._tk = Tk()<br />

def mainloop(self):<br />

self._tk.mainloop()<br />

def add_frame(self, name, class_, **pack_options):<br />

instance = class_(self._tk)<br />

setattr(self, name, instance)<br />

instance.pack(**pack_options)<br />

class ButtonFrame(Frame):<br />

""" barre de boutons """<br />

def __init__(self, racine=None):<br />

Frame.__init__(self, racine)<br />

self.boutton_quitter = Button(self, text="Quitter",<br />

command=self.quit)<br />

self.boutton_quitter.pack(side=LEFT)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!