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/19 17:44] – [Canvas : des rectangles et des mouvements] villersd | teaching:progappchim:tkinter_gui_simple [2023/01/19 15:46] (Version actuelle) – villersd | ||
|---|---|---|---|
| Ligne 2: | Ligne 2: | ||
| ===== 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 8: | Ligne 12: | ||
| * [[http:// | * [[http:// | ||
| * [[http:// | * [[http:// | ||
| + | * [[http:// | ||
| <note warning> | <note warning> | ||
| + | |||
| + | <note warning> | ||
| + | |||
| + | Vérifier le comportement en utilisant Idle et la version de base de Python ! | ||
| + | </ | ||
| ===== Une étiquette (Label) affichant " | ===== Une étiquette (Label) affichant " | ||
| - | <sxh python; title : Tk-00.py> | + | <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 (Button) avec une action pour écrire ====== | ===== Un bouton (Button) avec une action pour écrire ====== | ||
| 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> | <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=" |
| # | # | ||
| - | champ=Entry(root) | + | champ = Entry(root) |
| - | champ.grid(row=1) | + | champ.grid(row=0) |
| - | b=Button(root, | + | b = Button(root, |
| - | b.grid(row=2) | + | b.grid(row=1) |
| root.mainloop() | root.mainloop() | ||
| - | </sxh> | + | </code> |
| <note warning> | <note warning> | ||
| ===== Utiliser le texte rentré ===== | ===== Utiliser le texte rentré ===== | ||
| En cliquant, on quitte et on écrit le texte rentré (on n' | En cliquant, on quitte et on écrit le texte rentré (on n' | ||
| - | <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 131: | 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) | ||
| Ligne 177: | Ligne 195: | ||
| # éliminer la fenêtre après avoir quitté : | # éliminer la fenêtre après avoir quitté : | ||
| root.destroy() | root.destroy() | ||
| - | </sxh> | + | </code> |
| Pour d' | Pour d' | ||
| * [[http:// | * [[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 214: | Ligne 233: | ||
| can.mainloop() | can.mainloop() | ||
| - | </sxh> | + | </code> |
| Pour d' | Pour d' | ||
| Ligne 220: | Ligne 239: | ||
| ===== Une étiquette dynamique ===== | ===== Une étiquette dynamique ===== | ||
| - | <sxh python; title : compteur-01.py> | + | <code python compteur-01.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| # Exemple d'une étiquette dynamique par récursion | # Exemple d'une étiquette dynamique par récursion | ||
| - | import | + | import |
| def compteur_label(lab): | def compteur_label(lab): | ||
| Ligne 245: | Ligne 264: | ||
| button.pack() | button.pack() | ||
| root.mainloop() | root.mainloop() | ||
| - | </sxh> | + | </code> |
| ===== Créer des points avec la souris ===== | ===== Créer des points avec la souris ===== | ||
| - | <sxh python; title : points_souris-02.py> | + | <code python points_souris-02.py> |
| # | # | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| Ligne 255: | Ligne 274: | ||
| # http:// | # http:// | ||
| - | from Tkinter | + | from tkinter |
| def point(event): | def point(event): | ||
| Ligne 269: | Ligne 288: | ||
| can.grid(row=0) | can.grid(row=0) | ||
| can.bind("< | can.bind("< | ||
| - | b=Button(root, | + | b = Button(root, |
| b.grid(row=1) | b.grid(row=1) | ||
| root.mainloop() | root.mainloop() | ||
| - | print points | + | print(points) |
| - | </sxh> | + | </code> |
| + | |||
| + | Pour la gestion des événements, | ||
| ===== Utiliser des boutons radio (radiobuttons) ===== | ===== Utiliser des boutons radio (radiobuttons) ===== | ||
| - | <sxh python; title : radiobuttons.py> | + | <code python radiobuttons.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| # Exemple d' | # Exemple d' | ||
| - | import | + | import |
| def affiche_choix(): | def affiche_choix(): | ||
| - | i=v.get() | + | i = v.get() |
| - | print i, positions[i-1][0] | + | print(i, positions[i-1][0]) |
| root = tk.Tk() | root = tk.Tk() | ||
| Ligne 293: | Ligne 314: | ||
| positions = [(" | positions = [(" | ||
| - | lab=tk.Label(root, | + | lab = tk.Label(root, |
| lab.pack() | lab.pack() | ||
| for txt, val in positions: | for txt, val in positions: | ||
| - | b=tk.Radiobutton(root, | + | b = tk.Radiobutton(root, |
| b.pack() | b.pack() | ||
| tk.mainloop() | tk.mainloop() | ||
| - | </sxh> | + | </code> |
| ===== Utiliser des cases à cocher (checkbuttons) ===== | ===== Utiliser des cases à cocher (checkbuttons) ===== | ||
| - | <sxh python; title : checkbuttons-03.py> | + | <code python checkbuttons-03.py> |
| #! / | #! / | ||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
| # Exemple d' | # Exemple d' | ||
| - | import | + | import |
| def affiche_choix(): | def affiche_choix(): | ||
| - | print zip(elements, | + | print(zip(elements, [etats[i].get() for i in range(len(elements))])) |
| + | print(elements, [etats[i].get() for i in range(len(elements))]) | ||
| root = tk.Tk() | root = tk.Tk() | ||
| - | lab=tk.Label(root, | + | lab = tk.Label(root, |
| lab.grid(row = 0) | lab.grid(row = 0) | ||
| - | elements=[' | + | elements = [' |
| - | etats=[] | + | etats = [] |
| - | nelem=len(elements) | + | nelem = len(elements) |
| for i in range(nelem): | for i in range(nelem): | ||
| - | etat=tk.IntVar() | + | etat = tk.IntVar() |
| caco = tk.Checkbutton(root, | caco = tk.Checkbutton(root, | ||
| caco.grid(row = i+1) | caco.grid(row = i+1) | ||
| Ligne 332: | Ligne 354: | ||
| tk.mainloop() | tk.mainloop() | ||
| - | </sxh> | + | </code> |
| + | |||
| + | ===== 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:// | ||