teaching:progappchim:tkinter_gui_simple

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révisionLes deux révisions suivantes
teaching:progappchim:tkinter_gui_simple [2017/03/07 06:59] villersdteaching:progappchim:tkinter_gui_simple [2017/03/07 09:47] 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://docs.python.org/3/library/tk.html|Les interfaces graphiques TK]]
 +      * [[https://docs.python.org/3/library/tkinter.html|tkinter — interface Python à Tcl/Tk]], reprenant quelques références recommandées
 +      * Python 3 avec Tk intègre également les extensions [[https://docs.python.org/3/library/tkinter.ttk.html|ttk]] et [[https://docs.python.org/3/library/tkinter.tix.html|tix]], ainsi que l'IDE [[https://docs.python.org/3/library/idle.html|Idle]]
   * [[http://inforef.be/swi/download/apprendre_python.pdf|Chapitre 8 du livre ”apprendre à programmer avec Python”, de Gérard Swinnen]]   * [[http://inforef.be/swi/download/apprendre_python.pdf|Chapitre 8 du livre ”apprendre à programmer avec Python”, de Gérard Swinnen]]
     * [[http://fr.wikibooks.org/wiki/Apprendre_%C3%A0_programmer_avec_Python/Utilisation_de_fen%C3%AAtres_et_de_graphismes|version en wiki]]     * [[http://fr.wikibooks.org/wiki/Apprendre_%C3%A0_programmer_avec_Python/Utilisation_de_fen%C3%AAtres_et_de_graphismes|version en wiki]]
Ligne 19: Ligne 23:
 from tkinter import * from tkinter import *
  
-root=Tk()+root = Tk()
 w=Label(root, text="Bonjour !") w=Label(root, text="Bonjour !")
 w.pack() w.pack()
Ligne 35: Ligne 39:
  
 def action(): def action():
-    print "Yes, we can !"+    print("Yes, we can !")
  
-root=Tk() +root = Tk() 
-#w=Label(root, text="Bonjour!")+#w = Label(root, text="Bonjour!")
 #w.pack() #w.pack()
  
-b=Button(root,text="Click here !",command=action)+b = Button(root,text="Click here !",command=action)
 b.pack() b.pack()
  
Ligne 60: Ligne 64:
  
 def action(): def action():
-    print "Yes, we can !"+    print("Yes, we can !")
  
-root=Tk() +root = Tk() 
-#w=Label(root, text="Bonjour!")+#w = Label(root, text="Bonjour!")
 #w.grid(row=?) #w.grid(row=?)
  
-champ=Entry(root) +champ = Entry(root) 
-champ.grid(row=1)+champ.grid(row=0)
  
-b=Button(root,text="Click here !",command=action) +b = Button(root,text="Click here !",command=action) 
-b.grid(row=2)+b.grid(row=1)
 root.mainloop() root.mainloop()
 </code> </code>
Ligne 84: Ligne 88:
  
 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, text="Bonjour!")+w = Label(root, text="Bonjour!"
 +w.grid(row=0)
  
-champ=Entry(root) +champ = Entry(root) 
-champ.grid(row=0)+champ.grid(row=1) 
 + 
 +b = Button(root,text="Click here !",command=action) 
 +b.grid(row=2) 
 +c = Button(root,text="Quit",command=root.quit) 
 +c.grid(row=3)
  
-b=Button(root,text="Click here !",command=root.quit) 
-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()
 +
 </code> </code>
  
Ligne 113: Ligne 122:
 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<=argu:  # on arrêtera lorsque a sera > argu +    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()
 #w=Label(root, text="Bonjour!") #w=Label(root, text="Bonjour!")
  
-champ=Entry(root)+champ = Entry(root)
 champ.grid(row=0) champ.grid(row=0)
  
-b=Button(root,text="Click here !",command=root.quit)+b = Button(root,text="Click here !",command=root.quit)
 b.grid(row=1) b.grid(row=1)
 root.mainloop() root.mainloop()
Ligne 135: Ligne 144:
 # 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()
Ligne 151: Ligne 160:
 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<=argu:  # on arrêtera lorsque a sera > argu     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():
-    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,text="Calcule la factorielle",command=action)+b = Button(root,text="Calcule la factorielle",command=action)
 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,text="Terminer",command=root.quit)+bfin = Button(root,text="Terminer",command=root.quit)
 bfin.grid(row=3) bfin.grid(row=3)
  
Ligne 185: Ligne 194:
 Pour d'autres exemples, voir par exemple : Pour d'autres exemples, voir par exemple :
   * [[http://www.python-course.eu/tkinter_entry_widgets.php]]   * [[http://www.python-course.eu/tkinter_entry_widgets.php]]
 +
 ===== Canvas : des rectangles et des mouvements ===== ===== Canvas : des rectangles et des mouvements =====
 <code python tk_canvas_rectangles_move.py> <code python tk_canvas_rectangles_move.py>
Ligne 224: Ligne 234:
  
 ===== Une étiquette dynamique ===== ===== Une étiquette dynamique =====
-<sxh python; title : compteur-01.py>+<code python compteur-01.py>
 #! /usr/bin/env python #! /usr/bin/env python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
 # Exemple d'une étiquette dynamique par récursion # Exemple d'une étiquette dynamique par récursion
  
-import Tkinter as tk+import tkinter as tk
  
 def compteur_label(lab): def compteur_label(lab):
Ligne 249: Ligne 259:
 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>
 #!/usr/bin/env python #!/usr/bin/env python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
Ligne 259: Ligne 269:
 # http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm # http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
  
-from Tkinter import *+from tkinter import *
  
 def point(event): def point(event):
Ligne 273: Ligne 283:
 can.grid(row=0) can.grid(row=0)
 can.bind("<Button-1>", point) can.bind("<Button-1>", point)
-b=Button(root,text="Quitter",command=root.destroy)+b = Button(root,text="Quitter",command=root.destroy)
 b.grid(row=1) b.grid(row=1)
 root.mainloop() root.mainloop()
-print points +print(points) 
-</sxh>+</code>
  
 Pour la gestion des événements, leur déclenchement, voir par exemple [[http://www.python-course.eu/tkinter_events_binds.php|cette page]]. Pour la gestion des événements, leur déclenchement, voir par exemple [[http://www.python-course.eu/tkinter_events_binds.php|cette page]].
  
 ===== Utiliser des boutons radio (radiobuttons) ===== ===== Utiliser des boutons radio (radiobuttons) =====
-<sxh python; title : radiobuttons.py>+<code python radiobuttons.py>
 #! /usr/bin/env python #! /usr/bin/env python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
 # Exemple d'utilisation des boutons radio # Exemple d'utilisation des boutons radio
  
-import Tkinter as tk+import tkinter as tk
  
 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 299: Ligne 309:
 positions = [("ortho",1),("meta",2),("para",3)] positions = [("ortho",1),("meta",2),("para",3)]
  
-lab=tk.Label(root, text="Choix de la position", fg="dark blue")+lab = tk.Label(root, text="Choix de la position", fg="dark blue")
 lab.pack() lab.pack()
  
 for txt, val in positions: for txt, val in positions:
-    b=tk.Radiobutton(root, text=txt, padx = 10, variable=v, command=affiche_choix, value=val)+    b = tk.Radiobutton(root, text=txt, padx = 10, variable=v, command=affiche_choix, value=val)
     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>
 #! /usr/bin/env python #! /usr/bin/env python
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
 # Exemple d'utilisation des cases à cocher # Exemple d'utilisation des cases à cocher
  
-import Tkinter as tk+import tkinter as tk
  
 def affiche_choix(): def affiche_choix():
-    print zip(elements, [etats[i].get() for i in range(len(elements))])+    print(zip(elements, [etats[i].get() for i in range(len(elements))]))
  
 root = tk.Tk() root = tk.Tk()
-lab=tk.Label(root, text="Cochez les éléments présents", bg="red", fg="dark blue")+lab = tk.Label(root, text="Cochez les éléments présents", bg="red", fg="dark blue")
 lab.grid(row = 0) lab.grid(row = 0)
  
-elements=['C','H','O','N','P','S',u'éléments métalliques',u'halogénures',u'autres'+elements = ['C','H','O','N','P','S',u'éléments métalliques',u'halogénures',u'autres'
-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, text=elements[i], variable=etat,width = 20,padx=50,anchor = tk.W)     caco = tk.Checkbutton(root, text=elements[i], variable=etat,width = 20,padx=50,anchor = tk.W)
     caco.grid(row = i+1)     caco.grid(row = i+1)
Ligne 338: Ligne 348:
  
 tk.mainloop() tk.mainloop()
-</sxh>+</code>
  
 ===== Les listes de choix (spinbox, listbox) ===== ===== Les listes de choix (spinbox, listbox) =====
  • teaching/progappchim/tkinter_gui_simple.txt
  • Dernière modification : 2023/01/19 15:46
  • de villersd