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 01/09/2022, 00:02] moths-art Passage de http à https sur les liens externes (détecté et corrigé via le bot wiki-corrector (https://forum.ubuntu-fr.org/viewtopic.php?id=2067892) |
tkinter [Le 25/11/2023, 14:36] (Version actuelle) Amiralgaby [PyConnect] ne pas utiliser la commande ifconfig mais ip addr show |
||
---|---|---|---|
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. (La version 3.x comprend les widgets ttk) | * **[[apt>python3-tk]]** pour la version 3.x de python. (La version 3.x comprend les widgets ttk) | ||
Ligne 27: | 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 61: | Ligne 61: | ||
* 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 102: | Ligne 102: | ||
# Vérification de la connexion internet avec interface et ping | # Vérification de la connexion internet avec interface et ping | ||
# | # | ||
+ | |||
#Importation des librairies nécéssaire au bon fonctionnement du programme. | #Importation des librairies nécéssaire au bon fonctionnement du programme. | ||
#Tkinter pour l'interface graphique | #Tkinter pour l'interface graphique | ||
#urllib pour les schémas internet | #urllib pour les schémas internet | ||
#os pour dialoguer avec le systeme | #os pour dialoguer avec le systeme | ||
- | from Tkinter import * | + | from tkinter import * |
- | import urllib as url | + | from urllib import request |
- | import os | + | import os |
class Application(Frame): | class Application(Frame): | ||
def __init__(self,parent): | def __init__(self,parent): | ||
Ligne 116: | Ligne 116: | ||
self.etat = Label(self, text='',font='Times 28 italic bold') | self.etat = Label(self, text='',font='Times 28 italic bold') | ||
self.etat.grid(row=0, column=0, columnspan=4, sticky=NSEW) | 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 = Label(self, text='Interfaces:',font='Times',underline=0) | ||
self.lab_iface.grid(row=1,column=0,sticky=NSEW) | self.lab_iface.grid(row=1,column=0,sticky=NSEW) | ||
- | + | ||
self.iface = Text(self, font='Times 10') | self.iface = Text(self, font='Times 10') | ||
self.iface.grid(row=2, column=0, sticky=NSEW) | self.iface.grid(row=2, column=0, sticky=NSEW) | ||
- | + | ||
self.lab_ping = Label(self, text='Ping:',font='Times',underline=0) | self.lab_ping = Label(self, text='Ping:',font='Times',underline=0) | ||
self.lab_ping.grid(row=1,column=2,sticky=NSEW) | self.lab_ping.grid(row=1,column=2,sticky=NSEW) | ||
- | + | ||
self.ping = Text(self, font='Times',state='disabled') | self.ping = Text(self, font='Times',state='disabled') | ||
self.ping.grid(row=2, column=1, columnspan=3, sticky=NSEW) | self.ping.grid(row=2, column=1, columnspan=3, sticky=NSEW) | ||
- | + | ||
self.recharger = Button(self, text='Recharger', font='Times', command=self.checkIface) | self.recharger = Button(self, text='Recharger', font='Times', command=self.checkIface) | ||
self.recharger.grid(row=3, column=0, sticky=NSEW) | self.recharger.grid(row=3, column=0, sticky=NSEW) | ||
- | + | ||
self.quitter = Button(self, text='Quitter', font='Times', command=self.leave) | self.quitter = Button(self, text='Quitter', font='Times', command=self.leave) | ||
self.quitter.grid(row=3, column=1, columnspan=3,sticky=NSEW) | self.quitter.grid(row=3, column=1, columnspan=3,sticky=NSEW) | ||
- | + | ||
self.checkIface() | self.checkIface() | ||
- | + | ||
def checkIface(self): | def checkIface(self): | ||
self.iface.config(state='normal') | self.iface.config(state='normal') | ||
self.iface.delete(1.0,END) | self.iface.delete(1.0,END) | ||
- | self.listing = os.popen('ifconfig', 'r').read() | + | self.listing = os.popen('ip addr show', 'r').read() |
self.iface.insert(END, self.listing) | self.iface.insert(END, self.listing) | ||
self.iface.config(state='disabled') | self.iface.config(state='disabled') | ||
self.checkInternet() | self.checkInternet() | ||
- | + | ||
def checkInternet(self): | def checkInternet(self): | ||
try: | try: | ||
- | url.urlopen('http://www.google.com') | + | request.urlopen('http://www.google.com') |
self.etat.config(text='Connexion internet active') | self.etat.config(text='Connexion internet active') | ||
self.checkPing() | self.checkPing() | ||
- | except: | + | except Exception as e: |
- | self.etat.config(text='Connexion internet inactive') | + | print(e) |
- | self.ping.config(state='normal') | + | self.etat.config(text='Connexion internet inactive') |
- | self.ping.delete(1.0,END) | + | self.ping.config(state='normal') |
- | self.ping.insert(END, 'Ping impossible...') | + | self.ping.delete(1.0,END) |
- | self.ping.config(state='disabled') | + | self.ping.insert(END, 'Ping impossible...') |
- | + | self.ping.config(state='disabled') | |
+ | |||
def checkPing(self): | def checkPing(self): | ||
self.ping.config(state='normal') | self.ping.config(state='normal') | ||
Ligne 166: | Ligne 167: | ||
self.parent.after(1,self.parent.update()) | self.parent.after(1,self.parent.update()) | ||
c = c-1 | c = c-1 | ||
- | + | ||
self.ping.config(state='disabled') | self.ping.config(state='disabled') | ||
- | + | ||
def leave(self): | def leave(self): | ||
quit() | quit() | ||
+ | |||
if __name__ == '__main__': | if __name__ == '__main__': | ||
fen = Tk() | fen = Tk() | ||
fen.title('Connexion Internet') | fen.title('Connexion Internet') | ||
fen.resizable(False,False) | fen.resizable(False,False) | ||
- | + | ||
app = Application(fen) | app = Application(fen) | ||
app.grid(row=0, column=0, sticky=NSEW) | app.grid(row=0, column=0, sticky=NSEW) | ||
- | + | ||
fen.mainloop() | fen.mainloop() | ||
</file> | </file> |