Langage Python - Cours 4/5 : Interfaces graphiques
Transcription
Langage Python - Cours 4/5 : Interfaces graphiques
Langage Python Cours 4/5 : Interfaces graphiques Hubert Godfroy 19 novembre 2015 1/11 La dernière fois. . . I Paradigmes objet I Paradigme fonctionnel Aujourd’hui I Interface graphique I Programmation événementielle ⇒ Utilise les deux paradigmes 2/11 Plan Programmation événementielle Application : interfaces graphiques 3/11 Plan Programmation événementielle Application : interfaces graphiques 4/11 Style habituel inst1 inst2 ... instn−1 instn I Lecture linéaire I De haut en bas I On voit “tout ce qui se passe”. I Rythmé par les appels de fonction/méthodes 5/11 Programmation par événements I Envoi de signaux I Un contrôleur s’occupe d’envoyer les signaux au bon destinataire 6/11 Programmation par événements I Envoi de signaux I Un contrôleur s’occupe d’envoyer les signaux au bon destinataire Contrôleur envoie un signal lance l’exécution f1 GUI f2 f3 f4 f5 6/11 Plan Programmation événementielle Application : interfaces graphiques 7/11 Modèle - Vue - Contrôleur Signaux Ils sont déclenchés par des instances extérieurs au programme (clique de l’utilisateur, branchement d’un périphérique, . . . ). 8/11 Modèle - Vue - Contrôleur Signaux Ils sont déclenchés par des instances extérieurs au programme (clique de l’utilisateur, branchement d’un périphérique, . . . ). Vue I Compose l’interface utilisateur I Différents éléments d’interaction (bouton, champs de texte, labels, . . . ) 8/11 GUI en Python I Environnement Tkinter I Quatre éléments principaux : Button, Label, Entry et Canvas. 9/11 Exemple fen = Tk() 10/11 Exemple fen = Tk() bouton = Button(fen, text="quitter", command=fen. destroy) lab = Label(fen, text="Trololo") champs = Entry(fen) bouton2 = Button(fen, text="the big red button", command=update) can = Canvas(fen, width =200, height =200, bg =’red ’) 10/11 Exemple fen = Tk() bouton = Button(fen, text="quitter", command=fen. destroy) lab = Label(fen, text="Trololo") champs = Entry(fen) bouton2 = Button(fen, text="the big red button", command=update) can = Canvas(fen, width =200, height =200, bg =’red ’) can.pack() bouton.pack() lab.pack() champs.pack() bouton2.pack() 10/11 Exemple fen = Tk() bouton = Button(fen, text="quitter", command=fen. destroy) lab = Label(fen, text="Trololo") champs = Entry(fen) bouton2 = Button(fen, text="the big red button", command=update) can = Canvas(fen, width =200, height =200, bg =’red ’) can.pack() bouton.pack() lab.pack() champs.pack() bouton2.pack() fen.mainloop() 10/11 Exemple fen = Tk() bouton = Button(fen, text="quitter", command=fen. destroy) lab = Label(fen, text="Trololo") champs = Entry(fen) bouton2 = Button(fen, text="the big red button", command=update) can = Canvas(fen, width =200, height =200, bg =’red ’) can.pack() bouton.pack() lab.pack() champs.pack() bouton2.pack() fen.mainloop() def update(): lab.config(text="lala") 10/11 Exécution ! 11/11