/exercice/python2/chap14/boutons_radio.py

https://github.com/widowild/messcripts
Python | 38 lines | 26 code | 5 blank | 7 comment | 1 complexity | 4052661b3311d38266c7b9dd5c8b1657 MD5 | raw file
  1. #! /usr/bin/env python
  2. # -*- coding: Latin-1 -*-
  3. # Utilisation de boutons radio
  4. from Tkinter import *
  5. class RadioDemo(Frame):
  6. """Démo : utilisation de widgets 'boutons radio'"""
  7. def __init__(self, master=None):
  8. """Création d'un champ d'entrée avec 4 boutons radio"""
  9. Frame.__init__(self)
  10. self.pack()
  11. # Champ d'entrée contenant un petit texte :
  12. self.texte = Entry(self, width =28, font ="Arial 14")
  13. self.texte.insert(END,"La programmation, c'est génial")
  14. self.texte.pack(padx =8, pady =8)
  15. # Nom français et nom technique des quatre styles de police :
  16. stylePoliceFr =["Normal", "Gras", "Italique", "Gras/Italique"]
  17. stylePoliceTk =["normal", "bold", "italic", "bold italic"]
  18. # Le style actuel est mémorisé dans une 'variable Tkinter' ;
  19. self.choixPolice = StringVar()
  20. self.choixPolice.set(stylePoliceTk[0])
  21. # Création des quatre 'boutons radio' :
  22. for n in range(4):
  23. bout = Radiobutton(self,
  24. text = stylePoliceFr[n],
  25. variable = self.choixPolice,
  26. value = stylePoliceTk[n],
  27. command = self.changePolice)
  28. bout.pack(side =LEFT, padx =5)
  29. def changePolice(self):
  30. """Remplacement de la police actuelle"""
  31. police = "Arial 15 " + self.choixPolice.get()
  32. self.texte.configure(font =police)
  33. RadioDemo().mainloop()