Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
|
tkinter [Le 07/04/2013, 16:19] 88.172.93.113 Modification du liens vers ma page personnelle |
tkinter [Le 25/11/2023, 14:36] (Version actuelle) Amiralgaby [PyConnect] ne pas utiliser la commande ifconfig mais ip addr show |
||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | {{tag> programmation}} | + | {{tag> programmation python}} |
| ---- | ---- | ||
| ====== Créer des interfaces graphiques avec Tkinter ====== | ====== Créer des interfaces graphiques avec Tkinter ====== | ||
| Ligne 5: | Ligne 5: | ||
| ===== Présentation ===== | ===== Présentation ===== | ||
| - | **Tkinter** est une librairie basique mais très simple d'utilisation pour construire rapidement des interfaces graphiques avec [[:python|Python]]. | + | **Tkinter** est une bibliothèque basique mais très simple d'utilisation pour construire rapidement des interfaces graphiques avec [[:python|Python]]. |
| - | Le style de widgets n'est pas très esthétique (question de goût) mais ça reste tout de même une bonne base pour commencer dans le développement d'interface graphique (GUI). | + | Le style de widgets n'est pas très esthétique (question de goût) mais ça reste tout de même une bonne base pour commencer dans le développement d'interface graphique (GUI). |
| ===== Installation ===== | ===== Installation ===== | ||
| Ligne 13: | Ligne 13: | ||
| [[:tutoriel:comment_installer_un_paquet|Installez les paquets]]: | [[:tutoriel:comment_installer_un_paquet|Installez les paquets]]: | ||
| * **[[apt>python-tk]]** | * **[[apt>python-tk]]** | ||
| - | * **[[apt>python-imaging-tk]]** pour la gestion des images sous tkinter | + | * **[[apt>python-imaging-tk]]** pour la gestion des images sous tkinter |
| - | * **[[apt>python3-tk]]** pour la version 3.x de python. | + | * **[[apt>python3-tk]]** pour la version 3.x de python. (La version 3.x comprend les widgets ttk) |
| - | Ceci peut se résumer avec l'outil [[:apt-get]] en saisissant dans un [[:terminal]] les [[:commande_shell|commandes]] suivantes: | + | |
| - | <code>sudo apt-get install python-tk | + | |
| - | sudo apt-get install python-imaging-tk | + | |
| - | sudo apt-get install python3-tk | + | |
| - | </code> | + | |
| ===== Étude d'un programme simple ===== | ===== Étude d'un programme simple ===== | ||
| Ligne 32: | Ligne 27: | ||
| - | from Tkinter import * #Pour python3.x Tkinter devient tkinter | + | from Tkinter import * #Pour python3.x Tkinter devient tkinter |
| class ApplicationBasic(): | class ApplicationBasic(): | ||
| Ligne 49: | Ligne 44: | ||
| self.bou_quitter.pack() | self.bou_quitter.pack() | ||
| | | ||
| - | self.fen.mainloop() | + | def run(self): |
| + | self.fen.mainloop() | ||
| def action(self): | def action(self): | ||
| Ligne 60: | Ligne 56: | ||
| if __name__ == '__main__': | if __name__ == '__main__': | ||
| app = ApplicationBasic() | app = ApplicationBasic() | ||
| + | app.run() | ||
| </file> | </file> | ||
| chaque partie correspond à : | chaque partie correspond à : | ||
| * importation de la librairie : <file python>from Tkinter import *</file> | * importation de la librairie : <file python>from Tkinter import *</file> | ||
| * création d'une classe : <file python>class ApplicationBasic():</file> | * création d'une classe : <file python>class ApplicationBasic():</file> | ||
| - | * création d'une méthode constructrice : <file python>def __init__(self):</file> | + | * création d'une méthode constructrice : <file python>def __init__(self):</file> |
| * instancier une fenêtre Tk() : <file python>self.fen = Tk()</file> | * instancier une fenêtre Tk() : <file python>self.fen = Tk()</file> | ||
| * définition du titre de cette fenêtre : <file python>self.fen.title('Tkinter')</file> | * définition du titre de cette fenêtre : <file python>self.fen.title('Tkinter')</file> | ||
| Ligne 72: | Ligne 69: | ||
| * mise en place de celui-ci dans la fenêtre avec une méthode de placement : <file python>self.bou_action.pack()</file> | * mise en place de celui-ci dans la fenêtre avec une méthode de placement : <file python>self.bou_action.pack()</file> | ||
| * définition de la fonction qui sera connectée au bouton //Action// : <file python> | * définition de la fonction qui sera connectée au bouton //Action// : <file python> | ||
| - | ef action(self) : | + | def action(self) : |
| '''Action sur un bouton''' | '''Action sur un bouton''' | ||
| self.lab = Label(self.fen) | self.lab = Label(self.fen) | ||
| Ligne 93: | Ligne 90: | ||
| {{:terminal:programmetkinter01.png?direct&100|Fenêtre Tkinter après appui sur le bouton //Action//}} | {{:terminal:programmetkinter01.png?direct&100|Fenêtre Tkinter après appui sur le bouton //Action//}} | ||
| + | ===== Exemple de programmes ===== | ||
| + | ==== PyConnect ==== | ||
| + | La structure du code est un peu différente car j'utilise une classe. | ||
| + | |||
| + | <file python> | ||
| + | #!/usr/bin/env python | ||
| + | # -*- coding: utf-8 -*- | ||
| + | # | ||
| + | # PyConnect.py | ||
| + | # | ||
| + | # Vérification de la connexion internet avec interface et ping | ||
| + | # | ||
| + | |||
| + | #Importation des librairies nécéssaire au bon fonctionnement du programme. | ||
| + | #Tkinter pour l'interface graphique | ||
| + | #urllib pour les schémas internet | ||
| + | #os pour dialoguer avec le systeme | ||
| + | from tkinter import * | ||
| + | from urllib import request | ||
| + | import os | ||
| + | class Application(Frame): | ||
| + | def __init__(self,parent): | ||
| + | Frame.__init__(self) | ||
| + | self.parent = parent | ||
| + | self.etat = Label(self, text='',font='Times 28 italic bold') | ||
| + | self.etat.grid(row=0, column=0, columnspan=4, sticky=NSEW) | ||
| + | |||
| + | self.lab_iface = Label(self, text='Interfaces:',font='Times',underline=0) | ||
| + | self.lab_iface.grid(row=1,column=0,sticky=NSEW) | ||
| + | |||
| + | self.iface = Text(self, font='Times 10') | ||
| + | self.iface.grid(row=2, column=0, sticky=NSEW) | ||
| + | |||
| + | self.lab_ping = Label(self, text='Ping:',font='Times',underline=0) | ||
| + | self.lab_ping.grid(row=1,column=2,sticky=NSEW) | ||
| + | |||
| + | self.ping = Text(self, font='Times',state='disabled') | ||
| + | self.ping.grid(row=2, column=1, columnspan=3, sticky=NSEW) | ||
| + | |||
| + | self.recharger = Button(self, text='Recharger', font='Times', command=self.checkIface) | ||
| + | self.recharger.grid(row=3, column=0, sticky=NSEW) | ||
| + | |||
| + | self.quitter = Button(self, text='Quitter', font='Times', command=self.leave) | ||
| + | self.quitter.grid(row=3, column=1, columnspan=3,sticky=NSEW) | ||
| + | |||
| + | self.checkIface() | ||
| + | |||
| + | def checkIface(self): | ||
| + | self.iface.config(state='normal') | ||
| + | self.iface.delete(1.0,END) | ||
| + | self.listing = os.popen('ip addr show', 'r').read() | ||
| + | self.iface.insert(END, self.listing) | ||
| + | self.iface.config(state='disabled') | ||
| + | self.checkInternet() | ||
| + | |||
| + | def checkInternet(self): | ||
| + | try: | ||
| + | request.urlopen('http://www.google.com') | ||
| + | self.etat.config(text='Connexion internet active') | ||
| + | self.checkPing() | ||
| + | except Exception as e: | ||
| + | print(e) | ||
| + | self.etat.config(text='Connexion internet inactive') | ||
| + | self.ping.config(state='normal') | ||
| + | self.ping.delete(1.0,END) | ||
| + | self.ping.insert(END, 'Ping impossible...') | ||
| + | self.ping.config(state='disabled') | ||
| + | |||
| + | def checkPing(self): | ||
| + | self.ping.config(state='normal') | ||
| + | self.ping.delete(1.0,END) | ||
| + | c = 3 | ||
| + | while c != 0: | ||
| + | self.pingPacket = os.popen('ping -c 1 google.com').read() | ||
| + | self.ping.insert(END, self.pingPacket+'\n') | ||
| + | self.parent.after(1,self.parent.update()) | ||
| + | c = c-1 | ||
| + | |||
| + | self.ping.config(state='disabled') | ||
| + | |||
| + | def leave(self): | ||
| + | quit() | ||
| + | |||
| + | if __name__ == '__main__': | ||
| + | fen = Tk() | ||
| + | fen.title('Connexion Internet') | ||
| + | fen.resizable(False,False) | ||
| + | |||
| + | app = Application(fen) | ||
| + | app.grid(row=0, column=0, sticky=NSEW) | ||
| + | |||
| + | fen.mainloop() | ||
| + | </file> | ||
| + | {{:pyconnect.png?800|}} | ||
| ===== Liens ===== | ===== Liens ===== | ||
| - | * [[http://wiki.python.org/moin/TkInter|Tkinter]] (En) | + | * [[https://wiki.python.org/moin/TkInter|Tkinter]] (En) |
| + | * [[http://effbot.org/tkinterbook/|Tkinter]] (En) | ||
| + | * [[https://github.com/tarball69/tkRAD/wiki/Accueil|tkRAD: Tkinter XML widget builder]] (Fr) - génération facile de widgets Tkinter grâce à un fichier source XML. | ||
| * [[:python]] | * [[:python]] | ||
| * [[:glade]] : pour créer des GUI facilement | * [[:glade]] : pour créer des GUI facilement | ||