Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte: goodbye, Bob....

17
Interface graphique en Python 4V748 Enseignement Supérieur Public 26/02/2013 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 1 / 17

Transcript of Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte: goodbye, Bob....

Page 1: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Interface graphiqueen Python

4V748

Enseignement Supérieur Public

26/02/2013

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 1 / 17

Page 2: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Introduction

Définition

Une interface graphique, c’est :

une interface un dispositif destiné à permettre les interactionsentre programme et utilisateur,

graphique qui présente des informations de façon visuelleet reçoit des messages via une souris (ou autre pointeur).

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 2 / 17

Page 3: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Introduction

Pourquoi faire ?

Plusieurs raisons pour écrire des programmes graphiques :utilisation agréablerésultats plus immédiatement parlantsmise en évidence de l’effet d’un paramètre, ...accessible aux personnes peu familières avec les ordinateurs

Mais aussi des inconvénients :répétition d’une même séquence d’actionsapprentissage peu génériquedifficile à adapter à un besoin particulier

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 3 / 17

Page 4: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Introduction

Modes de fonctionnement

Non interactif Interactif en terminal Interactif graphique

NB On trouve des programmes mixtesex. interactif avec lecture de données dans un fichier,graphique avec paramètres lors du lancement, etc.

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 4 / 17

Page 5: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Architecture

Architecture d’un programme graphique

Au lancement du programme :construction de l’interfaceboucle d’attente

Dans la boucle :chaque action de l’utilisateur appelle une fonction

I réglage d’un paramètre (valeur, choix binaire / multiple, ...)I lecture de données (fichier, réseau, capteur, ...)I traitement (calculs, ...)I écriture de résultats (fichier, réseau, affichage, ...)

une fonction appelée peut modifier l’affichageune fonction permet de quitter le programme

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 5 / 17

Page 6: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Architecture

Élements d’une interface

Bouton avec texte Curseur

Bouton radio Liste déroulante

Menu Espace dessinable

etc.

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 6 / 17

Page 7: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Architecture

Rassurez-vous...

... nous n’allons pas programmer le dessin de ces objets,ni leur comportement lorsqu’on interagit avec eux !

Nous utiliserons :une bibliothèque graphiqueadaptée à notre langage de programmationle gestionnaire graphique du systèmepour une meilleure intégration (thème, langue,...)

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 7 / 17

Page 8: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Architecture

Architecture en couches

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 8 / 17

Page 9: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Architecture

Architecture en couches : construction

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 9 / 17

Page 10: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Architecture

Architecture en couches : fonctionnement

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 10 / 17

Page 11: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Architecture

Bibliothèques graphiques

PyGTK

PyQt

Tkinter

wxPython

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 11 / 17

Page 12: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Tkinter

Caractéristiques de Tkinter

Libre, gratuitePortable : Unix, Windows, Mac...Bibliothèque Orientée Objets

I Un élément graphique ("Widget") = un instance d’une classeI Hiérarchie d’objetsI On peut dériver ses propres objets

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 12 / 17

Page 13: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Tkinter

Hiérarchie de classes de Tkinter (extrait)

http://knipknap.github.io/exscript/api/class-tree.html

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 13 / 17

Page 14: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Tkinter

Le programme Tkinter minimal

helloMV448.py

from Tkinter import *

root = Tk()

w = Label(root, text="Hello, MV448 world!")w.pack()

root.mainloop()

> python helloMV448.py

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 14 / 17

Page 15: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Tkinter

Construire son interfacePrincipe : les éléments sont ajoutés un à un avec la méthode pack()

MV448_3Butt.pyimport Tkinterfenetre=Tkinter.Tk()fenetre.title(":-)")zone=Tkinter.Frame(fenetre)boutG=Tkinter.Button(zone,text="Gauche")boutH=Tkinter.Button(zone,text="Haut")boutB=Tkinter.Button(zone,text="Bas")boutG.pack(side=Tkinter.LEFT)boutH.pack(side=Tkinter.TOP)boutB.pack()zone.pack()fenetre.mainloop()

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 15 / 17

Page 16: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Tkinter

Callback avec paramètresPrincipe : on crée un objet

MV448_QuitButton.py ...from Tkinter import *

class AuRevoir:def __init__(self):

self.nam=’inconnu’def fetch(self):

self.nam=zt.get()print ’Texte: <%s>’ % self.nam

def bye(self):print ’goodbye, ’+self.nam+’.’import syssys.exit()

ciao=AuRevoir()

... MV448_QuitButton.pyracine=Tk()

zt = Entry(racine)zt.pack(side=TOP, fill=X)

bf = Button(racine, text=’Fetch’,command=ciao.fetch)

bf.pack(side=LEFT)

bq = Button(racine, text="Quitter",command=ciao.bye)

bq.pack(side=RIGHT)

racine.mainloop()

> python MV448_QuitButton.pyTexte: <Bob>goodbye, Bob.

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17

Page 17: Interface graphique en Python · 2014-02-26 · > python MV448_QuitButton.py Texte:  goodbye, Bob. 4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 16 / 17. Tkinter

Tkinter

4V748 (Ens Sup Pub) 4V748 : Python Graphique 26/02/2013 17 / 17