Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente Prochaine révisionLes deux révisions suivantes | ||
teaching:progappchim:tkinter_gui_simple [2013/11/21 14:37] – créée villersd | teaching:progappchim:tkinter_gui_simple [2017/03/07 06:59] – villersd | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
===== Quelques références de base pour utiliser Tkinter ===== | ===== Quelques références de base pour utiliser Tkinter ===== | ||
- | * Chapitre 8 | + | * [[http:// |
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
- | ===== Un Label affichant " | + | <note warning> |
- | <sxh python; title : Tk-00.py> | + | |
+ | ===== Une étiquette (Label) affichant " | ||
+ | <code python | ||
# | # | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
- | from Tkinter | + | from tkinter |
root=Tk() | root=Tk() | ||
Ligne 16: | Ligne 24: | ||
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(): | ||
Ligne 37: | Ligne 45: | ||
root.mainloop() | root.mainloop() | ||
- | </sxh> | + | </code> |
- | ==== Champ d' | + | <note tip> |
+ | |||
+ | Pour le placement des composants dans la fenêtre, Tkinter utilise 3 méthodes (pack, grid, place) décrites [[http:// | ||
+ | |||
+ | ===== 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(): | ||
Ligne 52: | Ligne 64: | ||
root=Tk() | root=Tk() | ||
# | # | ||
- | #w.pack() | + | #w.grid(row=?) |
champ=Entry(root) | champ=Entry(root) | ||
- | champ.grid(row=0) | + | champ.grid(row=1) |
b=Button(root, | b=Button(root, | ||
- | b.grid(row=1) | + | b.grid(row=2) |
root.mainloop() | root.mainloop() | ||
- | </sxh> | + | </code> |
+ | <note warning> | ||
===== 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(): | ||
Ligne 88: | Ligne 101: | ||
# é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): | ||
Ligne 126: | Ligne 139: | ||
# é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): | ||
Ligne 166: | Ligne 179: | ||
root.mainloop() | root.mainloop() | ||
- | # éliminer la fenêtre : | + | # éliminer la fenêtre |
root.destroy() | root.destroy() | ||
+ | </ | ||
+ | |||
+ | Pour d' | ||
+ | * [[http:// | ||
+ | ===== Canvas : des rectangles et des mouvements ===== | ||
+ | <code python tk_canvas_rectangles_move.py> | ||
+ | #! / | ||
+ | # -*- coding: utf-8 -*- | ||
+ | # Exemple utilisation du Canvas Tk pour gérer une boite avec couvercle mobile | ||
+ | |||
+ | from tkinter import * | ||
+ | |||
+ | def move(): | ||
+ | " | ||
+ | global hauteur,v | ||
+ | hauteur = hauteur + v | ||
+ | if hauteur > 250 or hauteur < 130: | ||
+ | v = -v | ||
+ | can.coords(couvercle, | ||
+ | flag=1 | ||
+ | root.after(1, | ||
+ | |||
+ | root = Tk() | ||
+ | can = Canvas( root, width=500, height=400 ) | ||
+ | can.pack() | ||
+ | |||
+ | can.create_rectangle( 95,100, 100, 355, | ||
+ | can.create_rectangle( 300,100, 305, 355, | ||
+ | can.create_rectangle( 100,350, 300, 355, | ||
+ | hauteur = 150 | ||
+ | couvercle = can.create_rectangle( 100, | ||
+ | |||
+ | # animation simple: | ||
+ | v = 0.1 # incrément/ | ||
+ | move() | ||
+ | |||
+ | can.mainloop() | ||
+ | </ | ||
+ | |||
+ | Pour d' | ||
+ | * [[http:// | ||
+ | |||
+ | ===== Une étiquette dynamique ===== | ||
+ | <sxh python; title : 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 ===== | ||
+ | <sxh python; title : 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) ===== | ||
+ | <sxh python; title : 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) ===== | ||
+ | <sxh python; title : checkbuttons-03.py> | ||
+ | #! / | ||
+ | # -*- coding: utf-8 -*- | ||
+ | # Exemple d' | ||
+ | |||
+ | import Tkinter as tk | ||
+ | |||
+ | def affiche_choix(): | ||
+ | print zip(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) ===== | ||
+ | FIXME (à écrire) | ||
+ | |||
+ | ===== 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:// | ||