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 | ||
| teaching:progappchim:tkinter_gui_simple [2015/03/18 10:19] – [Champ d'entrée] villersd | teaching:progappchim:tkinter_gui_simple [2023/01/19 15:46] (Version actuelle) – villersd | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Les bases d'un interface graphique avec Tkinter ====== | ====== Les bases d'un interface graphique avec Tkinter ====== | ||
| - | <note warning> | ||
| ===== Quelques références de base pour utiliser Tkinter ===== | ===== Quelques références de base pour utiliser Tkinter ===== | ||
| + | * Documentation officielle : | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * Python 3 avec Tk intègre également les extensions [[https:// | ||
| * [[http:// | * [[http:// | ||
| * [[http:// | * [[http:// | ||
| Ligne 9: | Ligne 12: | ||
| * [[http:// | * [[http:// | ||
| * [[http:// | * [[http:// | ||
| + | * [[http:// | ||
| - | ===== Un Label affichant " | + | <note warning> |
| - | <sxh python; title : Tk-00.py> | + | |
| + | <note warning> | ||
| + | |||
| + | Vérifier le comportement en utilisant Idle et la version de base de Python ! | ||
| + | </ | ||
| + | |||
| + | ===== Une étiquette (Label) affichant " | ||
| + | <code python | ||
| # | # | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| - | from Tkinter | + | from tkinter |
| - | root=Tk() | + | root = Tk() |
| w=Label(root, | w=Label(root, | ||
| w.pack() | w.pack() | ||
| root.mainloop() | root.mainloop() | ||
| - | </sxh> | + | </code> |
| - | ===== Un bouton avec une action pour écrire ====== | + | ===== Un bouton |
| L' | L' | ||
| - | <sxh python; title : Tk-01.py> | + | <code python |
| # | # | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| - | from Tkinter | + | from tkinter |
| def action(): | def action(): | ||
| - | print "Yes, we can !" | + | print("Yes, we can !") |
| - | root=Tk() | + | root = Tk() |
| - | # | + | #w = Label(root, text=" |
| #w.pack() | #w.pack() | ||
| - | b=Button(root, | + | b = Button(root, |
| b.pack() | b.pack() | ||
| root.mainloop() | root.mainloop() | ||
| - | </sxh> | + | </code> |
| + | |||
| + | <note tip> | ||
| + | |||
| + | Pour le placement des composants dans la fenêtre, Tkinter utilise 3 méthodes (pack, grid, place) décrites [[http:// | ||
| - | ===== Champ d' | + | ===== Champ d' |
| On peut mettre un champ d' | On peut mettre un champ d' | ||
| - | <sxh python; title : Tk-02.py> | + | <code python |
| # | # | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| - | from Tkinter | + | from tkinter |
| def action(): | def action(): | ||
| - | print "Yes, we can !" | + | print("Yes, we can !") |
| - | root=Tk() | + | root = Tk() |
| - | # | + | #w = Label(root, text=" |
| - | #w.pack() | + | #w.grid(row=?) |
| - | champ=Entry(root) | + | champ = Entry(root) |
| champ.grid(row=0) | champ.grid(row=0) | ||
| - | b=Button(root, | + | b = Button(root, |
| b.grid(row=1) | b.grid(row=1) | ||
| root.mainloop() | root.mainloop() | ||
| - | </sxh> | + | </code> |
| - | < | + | < |
| ===== Utiliser le texte rentré ===== | ===== Utiliser le texte rentré ===== | ||
| - | En cliquant, on quitte et on écrit le texte rentré | + | En cliquant, on quitte et on écrit le texte rentré |
| - | <sxh python; title : Tk-03.py> | + | <code python |
| # | # | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| - | from Tkinter | + | from tkinter |
| def action(): | def action(): | ||
| - | print "Yes, we can !" | + | print("Yes, we can !") |
| + | # impression de la valeur du champ | ||
| + | abcdef = champ.get() | ||
| + | print(abcdef) | ||
| - | root=Tk() | + | root = Tk() |
| - | #w=Label(root, | + | w = Label(root, text=" |
| + | w.grid(row=0) | ||
| - | champ=Entry(root) | + | champ = Entry(root) |
| - | champ.grid(row=0) | + | champ.grid(row=1) |
| + | |||
| + | b = Button(root, | ||
| + | b.grid(row=2) | ||
| + | c = Button(root, | ||
| + | c.grid(row=3) | ||
| - | b=Button(root, | ||
| - | b.grid(row=1) | ||
| root.mainloop() | root.mainloop() | ||
| - | # lecture de la valeur du champ | ||
| - | abcdef=champ.get() | ||
| - | print abcdef | ||
| # éliminer la fenêtre : | # éliminer la fenêtre : | ||
| root.destroy() | root.destroy() | ||
| - | </sxh> | + | |
| + | </code> | ||
| ===== Valeurs numériques et calcul ===== | ===== Valeurs numériques et calcul ===== | ||
| On fait un calcul avec la valeur rentrée, on quitte et on écrit | On fait un calcul avec la valeur rentrée, on quitte et on écrit | ||
| - | <sxh python; title : Tk-04.py> | + | <code python |
| # | # | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| - | from Tkinter | + | from tkinter |
| def factorielle(argu): | def factorielle(argu): | ||
| # calcul de la factorielle de argu | # calcul de la factorielle de argu | ||
| - | a=1 # a contient une valeur qui va être incrémentée d'une unité à la fois | + | a = 1 # a contient une valeur qui va être incrémentée d'une unité à la fois |
| - | b=1 # contient la factorielle de a-1 | + | b = 1 # contient la factorielle de a-1 |
| - | while a< | + | while a <= argu: # on arrêtera lorsque a sera > argu |
| - | b=b * a | + | b = b * a |
| - | a=a+1 | + | a = a + 1 |
| return b | return b | ||
| def action(): | def action(): | ||
| - | print "Yes, we can !" | + | print("Yes, we can !") |
| root=Tk() | root=Tk() | ||
| # | # | ||
| - | champ=Entry(root) | + | champ = Entry(root) |
| champ.grid(row=0) | champ.grid(row=0) | ||
| - | b=Button(root, | + | b = Button(root, |
| b.grid(row=1) | b.grid(row=1) | ||
| root.mainloop() | root.mainloop() | ||
| Ligne 128: | Ligne 149: | ||
| # lecture de la valeur du champ | # lecture de la valeur du champ | ||
| texte_n=champ.get() | texte_n=champ.get() | ||
| - | n=int(texte_n) | + | n = int(texte_n) |
| - | print n, factorielle(n) | + | print(n, factorielle(n)) |
| # éliminer la fenêtre : | # éliminer la fenêtre : | ||
| root.destroy() | root.destroy() | ||
| - | </sxh> | + | </code> |
| ===== Tout faire dans interface graphique ===== | ===== Tout faire dans interface graphique ===== | ||
| Ce programme utilise un Label pour afficher le résultat, on ne quitte plus et on peut recalculer sur d' | Ce programme utilise un Label pour afficher le résultat, on ne quitte plus et on peut recalculer sur d' | ||
| - | <sxh python; title : Tk-05.py> | + | <code python |
| # | # | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| - | from Tkinter | + | from tkinter |
| def factorielle(argu): | def factorielle(argu): | ||
| # calcul de la factorielle de argu | # calcul de la factorielle de argu | ||
| - | a=1 # a contient une valeur qui va être incrémentée d'une unité à la fois | + | a = 1 # a contient une valeur qui va être incrémentée d'une unité à la fois |
| - | b=1 # contient la factorielle de a-1 | + | b = 1 # contient la factorielle de a-1 |
| while a< | while a< | ||
| - | b=b * a | + | b = b * a |
| - | a=a+1 | + | a = a + 1 |
| return b | return b | ||
| def action(): | def action(): | ||
| - | texte_n=champ.get() | + | texte_n = champ.get() |
| - | n=int(texte_n) | + | n = int(texte_n) |
| affichefacto.configure(text =str(factorielle(n))) | affichefacto.configure(text =str(factorielle(n))) | ||
| root=Tk() | root=Tk() | ||
| - | champ=Entry(root) | + | champ = Entry(root) |
| champ.grid(row=0) | champ.grid(row=0) | ||
| - | b=Button(root, | + | b = Button(root, |
| b.grid(row=1) | b.grid(row=1) | ||
| - | affichefacto=Label(root) | + | affichefacto = Label(root) |
| affichefacto.grid(row=2) | affichefacto.grid(row=2) | ||
| - | bfin=Button(root, | + | bfin = Button(root, |
| bfin.grid(row=3) | bfin.grid(row=3) | ||
| root.mainloop() | root.mainloop() | ||
| - | # éliminer la fenêtre : | + | # éliminer la fenêtre |
| root.destroy() | root.destroy() | ||
| - | </sxh> | + | </code> |
| + | |||
| + | Pour d' | ||
| + | * [[http:// | ||
| ===== Canvas : des rectangles et des mouvements ===== | ===== Canvas : des rectangles et des mouvements ===== | ||
| - | <sxh python; title : Tk_canvas_rectangles_move.py> | + | <code python |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| # Exemple utilisation du Canvas Tk pour gérer une boite avec couvercle mobile | # Exemple utilisation du Canvas Tk pour gérer une boite avec couvercle mobile | ||
| - | from Tkinter | + | from tkinter |
| def move(): | def move(): | ||
| Ligne 209: | Ligne 233: | ||
| can.mainloop() | can.mainloop() | ||
| - | </sxh> | + | </code> |
| + | |||
| + | Pour d' | ||
| + | * [[http:// | ||
| + | |||
| + | ===== Une étiquette dynamique ===== | ||
| + | <code python compteur-01.py> | ||
| + | #! / | ||
| + | # -*- coding: utf-8 -*- | ||
| + | # Exemple d'une étiquette dynamique par récursion | ||
| + | |||
| + | import tkinter as tk | ||
| + | |||
| + | def compteur_label(lab): | ||
| + | def compte(): | ||
| + | global compteur | ||
| + | compteur += 1 | ||
| + | lab.config(text=str(compteur)) | ||
| + | lab.after(1000, | ||
| + | compte() | ||
| + | |||
| + | |||
| + | root = tk.Tk() | ||
| + | root.title(" | ||
| + | label = tk.Label(root, | ||
| + | label.pack() | ||
| + | compteur = -1 | ||
| + | compteur_label(label) | ||
| + | button = tk.Button(root, | ||
| + | button.pack() | ||
| + | root.mainloop() | ||
| + | </ | ||
| + | |||
| + | ===== Créer des points avec la souris ===== | ||
| + | <code python points_souris-02.py> | ||
| + | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | # créer des points à l'aide de la souris | ||
| + | # refs : | ||
| + | # http:// | ||
| + | |||
| + | from tkinter import * | ||
| + | |||
| + | def point(event): | ||
| + | can.create_oval(event.x-4, | ||
| + | points.append([event.x, | ||
| + | return | ||
| + | |||
| + | root = Tk() | ||
| + | root.title(" | ||
| + | points = [] | ||
| + | can = Canvas(root, | ||
| + | can.configure(cursor=" | ||
| + | can.grid(row=0) | ||
| + | can.bind("< | ||
| + | b = Button(root, | ||
| + | b.grid(row=1) | ||
| + | root.mainloop() | ||
| + | print(points) | ||
| + | </ | ||
| + | |||
| + | Pour la gestion des événements, | ||
| + | |||
| + | ===== Utiliser des boutons radio (radiobuttons) ===== | ||
| + | <code python radiobuttons.py> | ||
| + | #! / | ||
| + | # -*- coding: utf-8 -*- | ||
| + | # Exemple d' | ||
| + | |||
| + | import tkinter as tk | ||
| + | |||
| + | def affiche_choix(): | ||
| + | i = v.get() | ||
| + | print(i, positions[i-1][0]) | ||
| + | |||
| + | root = tk.Tk() | ||
| + | v = tk.IntVar() | ||
| + | v.set(1) | ||
| + | |||
| + | positions = [(" | ||
| + | |||
| + | lab = tk.Label(root, | ||
| + | lab.pack() | ||
| + | |||
| + | for txt, val in positions: | ||
| + | b = tk.Radiobutton(root, | ||
| + | b.pack() | ||
| + | |||
| + | tk.mainloop() | ||
| + | </ | ||
| + | |||
| + | ===== Utiliser des cases à cocher (checkbuttons) ===== | ||
| + | <code python checkbuttons-03.py> | ||
| + | #! / | ||
| + | # -*- coding: utf-8 -*- | ||
| + | # Exemple d' | ||
| + | |||
| + | import tkinter as tk | ||
| + | |||
| + | def affiche_choix(): | ||
| + | print(zip(elements, | ||
| + | print(elements, | ||
| + | |||
| + | root = tk.Tk() | ||
| + | lab = tk.Label(root, | ||
| + | lab.grid(row = 0) | ||
| + | |||
| + | elements = [' | ||
| + | etats = [] | ||
| + | nelem = len(elements) | ||
| + | |||
| + | for i in range(nelem): | ||
| + | etat = tk.IntVar() | ||
| + | caco = tk.Checkbutton(root, | ||
| + | caco.grid(row = i+1) | ||
| + | etats.append(etat) | ||
| + | |||
| + | button = tk.Button(root, | ||
| + | button.grid(row = nelem+1) | ||
| + | |||
| + | tk.mainloop() | ||
| + | </ | ||
| + | |||
| + | ===== Les listes de choix (spinbox, listbox) ===== | ||
| + | FIXME (à écrire) | ||
| + | |||
| + | ===== Insérer une image (photoimage) ===== | ||
| + | |||
| + | <note tip>Avec Spyder, sous Anaconda, l' | ||
| + | |||
| + | Solution : choisir dans les préférences de spyder la partie " | ||
| + | |||
| + | Autre solution : menu " | ||
| + | </ | ||
| + | |||
| + | Télécharger l' | ||
| + | <code python image_import-01.py> | ||
| + | # | ||
| + | # -*- coding: utf-8 -*- | ||
| + | """ | ||
| + | insert a PNG image into a python tkinter window | ||
| + | image png : https:// | ||
| + | cf. https:// | ||
| + | """ | ||
| + | |||
| + | import tkinter as tk | ||
| + | |||
| + | root = tk.Tk() | ||
| + | img = tk.PhotoImage(file = " | ||
| + | label = tk.Label(root, | ||
| + | # | ||
| + | label.grid() | ||
| + | root.mainloop() | ||
| + | </ | ||
| + | |||
| + | * Image utilisée historiquement fréquemment en traitement d' | ||
| + | * [[https:// | ||
| + | * Base d' | ||
| + | * [[http:// | ||
| + | * [[https:// | ||
| + | |||
| + | {{ https:// | ||
| + | |||
| + | ===== Autres composants logiciels (widgets) de Tkinter ===== | ||
| + | Voici une liste et des liens vers des exemples pour d' | ||
| + | |||
| + | ^Widgets ^ Exemples ^ | ||
| + | |Sliders (curseur de défilement) |[[http:// | ||
| + | |Texte |[[http:// | ||
| + | |Boites de dialogue |[[http:// | ||
| + | |Menus |[[http:// | ||
| + | |Barres de progression (progressbar) | | | ||
| + | |Échelles (scale) | | | ||
| + | |||
| + | Références et démonstrations : | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * | ||
| + | ===== Des exemples d' | ||
| + | * [[http:// | ||
| + | |||
| + | ===== CustomTkinter ===== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ===== ttkbootstrap ===== | ||
| + | * [[https:// | ||