issue51:python_partie_25_pp._7-14
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue51:python_partie_25_pp._7-14 [2011/08/28 15:37] – andre_domenech | issue51:python_partie_25_pp._7-14 [2011/08/31 21:35] (Version actuelle) – fredphil91 | ||
---|---|---|---|
Ligne 289: | Ligne 289: | ||
**The “funcNumButton routine receives the value we passed from the button press. The only thing that is different from the example above is what if the user pressed the decimal button (“.”). Below, you'll see that we use a boolean variable to hold the fact they pressed the decimal button, and, on the next click, we deal with it. That's what the “if self.DecimalNext == True:” line is all about. Let's walk through it. | **The “funcNumButton routine receives the value we passed from the button press. The only thing that is different from the example above is what if the user pressed the decimal button (“.”). Below, you'll see that we use a boolean variable to hold the fact they pressed the decimal button, and, on the next click, we deal with it. That's what the “if self.DecimalNext == True:” line is all about. Let's walk through it. | ||
- | The user clicks 3, then 2, then the decimal, then 4, to create “32.4”. We handle the 3 and 2 clicks through the “funcNumButton” routine. We check to see if self.DecimalNext is True (which in this case it isn't until the user clicks the “.” button). If not, we simply multiply the held value (self.CurrentValue) by 10 and add the incoming value. When the user clicks the “.”, the callback “funcFuncButton” is called with the “Dec” value. All we do is set the boolean variable “self.DecimalNext” to True. When the user clicks the 4, we will test the “self.DecimalNext” value and, since it's true, we play some magic. First, we increment the self.DecimalCount variable. This tells us how many decimal places we are working with. We then take the incoming value, multiply it by (10< | + | The user clicks 3, then 2, then the decimal, then 4, to create “32.4”. We handle the 3 and 2 clicks through the “funcNumButton” routine. We check to see if self.DecimalNext is True (which in this case it isn't until the user clicks the “.” button). If not, we simply multiply the held value (self.CurrentValue) by 10 and add the incoming value. When the user clicks the “.”, the callback “funcFuncButton” is called with the “Dec” value. All we do is set the boolean variable “self.DecimalNext” to True. When the user clicks the 4, we will test the “self.DecimalNext” value and, since it's true, we play some magic. First, we increment the self.DecimalCount variable. This tells us how many decimal places we are working with. We then take the incoming value, multiply it by (10< |
La routine « foncBoutonNumerique » reçoit la valeur que nous lui passons en appuyant sur un bouton. La seule chose qui diffère de l' | La routine « foncBoutonNumerique » reçoit la valeur que nous lui passons en appuyant sur un bouton. La seule chose qui diffère de l' | ||
- | L' | + | L' |
**The “funcClear” routine simply clears the two holding variables, then sets the display. | **The “funcClear” routine simply clears the two holding variables, then sets the display. | ||
Ligne 356: | Ligne 356: | ||
Le mois prochain, nous allons continuer à explorer Tkinter et la richesse de ses widgets. Dans un prochain article, nous verrons un concepteur d' | Le mois prochain, nous allons continuer à explorer Tkinter et la richesse de ses widgets. Dans un prochain article, nous verrons un concepteur d' | ||
+ | |||
+ | ====== CODE PAGE 7 ====== | ||
+ | remplacer l' | ||
+ | |||
+ | ====== CODE PAGE 8 ====== | ||
+ | class App: | ||
+ | def __init__(self, | ||
+ | cadre = Frame(principale) | ||
+ | self.lblTexte = Label(cadre, | ||
+ | self.btnQuitter = Button(cadre, | ||
+ | self.btnBonjour = Button(cadre, | ||
+ | cadre.grid(column = 0, row = 0) | ||
+ | self.lblTexte.grid(column = 0, row = 0, columnspan = 2) | ||
+ | self.btnBonjour.grid(column = 0, row = 1) | ||
+ | self.btnQuitter.grid(column = 1, row = 1) | ||
+ | |||
+ | |||
+ | ====== CODE PAGE 9 ====== | ||
+ | en haut à droite : remplacer CLEAR par EFFACER | ||
+ | |||
+ | juste en dessous : | ||
+ | |||
+ | from Tkinter import * | ||
+ | def Demarrage(): | ||
+ | global val, calc, racine | ||
+ | racine = Tk() | ||
+ | racine.title(' | ||
+ | racine.geometry(' | ||
+ | calc = Calculatrice(racine) | ||
+ | racine.mainloop() | ||
+ | |||
+ | en bas à gauche : | ||
+ | |||
+ | class Calculatrice(): | ||
+ | def __init__(self, | ||
+ | principale = Frame(racine) | ||
+ | self.ValeurCourante = 0 | ||
+ | self.ValeurAncienne = 0 | ||
+ | self.FonctionCourante = '' | ||
+ | self.AffichageCourant = StringVar() | ||
+ | self.AffichageCourant.set(' | ||
+ | self.PartieDecimale = False | ||
+ | self.CompteDecimales = 0 | ||
+ | self.DefinirWidgets(principale) | ||
+ | self.PlacerWidgets(principale) | ||
+ | |||
+ | ====== CODE PAGE 10 ====== | ||
+ | self.btn1 = Button(principale, | ||
+ | self.btn1.bind('< | ||
+ | self.btn2 = Button(principale, | ||
+ | self.btn2.bind('< | ||
+ | self.btn3 = Button(principale, | ||
+ | self.btn3.bind('< | ||
+ | self.btn4 = Button(principale, | ||
+ | self.btn4.bind('< | ||
+ | |||
+ | ====== CODE PAGE 11 ====== | ||
+ | self.btnDash = Button(principale, | ||
+ | self.btnDash.bind('< | ||
+ | self.btnDot = Button(principale, | ||
+ | self.btnDot.bind('< | ||
+ | |||
+ | Le bouton « btnDash » change le signe de la valeur affichée. 523 devient -523 et -523 devient 523. Le bouton btnDot saisit un point décimal. Ces exemples, ainsi que les suivants, utilisent la fonction foncBoutonFonction. | ||
+ | |||
+ | self.btnPlus = Button(principale, | ||
+ | self.btnPlus.bind('< | ||
+ | self.btnMinus = Button(principale, | ||
+ | self.btnMinus.bind('< | ||
+ | self.btnStar = Button(principale, | ||
+ | self.btnStar.bind('< | ||
+ | self.btnDiv = Button(principale, | ||
+ | self.btnDiv.bind('< | ||
+ | self.btnEqual = Button(principale, | ||
+ | self.btnEqual.bind('< | ||
+ | |||
+ | Voici les quatre boutons pour les fonctions mathématiques. | ||
+ | |||
+ | self.btnClear = Button(principale, | ||
+ | self.btnClear.bind('< | ||
+ | |||
+ | Enfin, voici le bouton Effacer. Il efface bien sûr les variables et l' | ||
+ | |||
+ | def PlacerWidgets(self, | ||
+ | principale.grid(column=0, | ||
+ | self.lblAffichage.grid(column=0, | ||
+ | self.btn1.grid(column = 0, row = 1) | ||
+ | self.btn2.grid(column = 1, row = 1) | ||
+ | self.btn3.grid(column = 2, row = 1) | ||
+ | self.btn4.grid(column = 0, row = 2) | ||
+ | self.btn5.grid(column = 1, row = 2) | ||
+ | self.btn6.grid(column = 2, row = 2) | ||
+ | self.btn7.grid(column = 0, row = 3) | ||
+ | self.btn8.grid(column = 1, row = 3) | ||
+ | self.btn9.grid(column = 2, row = 3) | ||
+ | self.btn0.grid(column = 1, row = 4) | ||
+ | |||
+ | ====== CODE PAGE 12 ====== | ||
+ | self.btnDash.grid(column = 0, row = 4) | ||
+ | self.btnDot.grid(column = 2, row = 4) | ||
+ | self.btnPlus.grid(column = 3,row = 1) | ||
+ | self.btnMinus.grid(column = 3, row = 2) | ||
+ | self.btnStar.grid(column = 3, row = 3) | ||
+ | self.btnDiv.grid(column=3, | ||
+ | self.btnEqual.grid(column=0, | ||
+ | self.btnClear.grid(column=0, | ||
+ | |||
+ | et en dessous : | ||
+ | |||
+ | def foncBoutonNumerique(self, | ||
+ | if self.PartieDecimale == True: | ||
+ | self.CompteDecimales += 1 | ||
+ | self.ValeurCourante = self.ValeurCourante + (val * (10**-self.CompteDecimales)) | ||
+ | else: | ||
+ | self.ValeurCourante = (self.ValeurCourante * 10) + val | ||
+ | self.Rafraichir()f.Rafraichir() | ||
+ | |||
+ | ====== CODE PAGE 13 ====== | ||
+ | def foncBoutonFonction(self, | ||
+ | if fonction == ' | ||
+ | self.PartieDecimale = True | ||
+ | else: | ||
+ | self.PartieDecimale = False | ||
+ | self.CompteDecimales = 0 | ||
+ | if fonction == ' | ||
+ | self.ValeurCourante *= -1 | ||
+ | self.Rafraichir() | ||
+ | |||
+ | la fonction SIGNE multiplie simplement la valeur courante par -1. | ||
+ | |||
+ | elif fonction == ' | ||
+ | self.ValeurAncienne = self.ValeurCourante | ||
+ | self.ValeurCourante = 0 | ||
+ | self.FonctionCourante = ' | ||
+ | |||
+ | La fonction Ajouter recopie « self.ValeurCourante » dans « self.ValeurAncienne », efface « self.ValeurCourante », et règle « self.FonctionCourante » à « Ajouter ». Les fonctions Soustraire, Multiplier et Diviser font la même chose avec les mots-clés appropriés. | ||
+ | |||
+ | elif fonction == ' | ||
+ | self.ValeurAncienne = self.ValeurCourante | ||
+ | self.ValeurCourante = 0 | ||
+ | self.FonctionCourante = ' | ||
+ | elif fonction == ' | ||
+ | self.ValeurAncienne = self.ValeurCourante | ||
+ | self.ValeurCourante = 0 | ||
+ | self.FonctionCourante = ' | ||
+ | elif fonction == ' | ||
+ | self.ValeurAncienne = self.ValeurCourante | ||
+ | self.ValeurCourante = 0 | ||
+ | self.FonctionCourante = ' | ||
+ | |||
+ | La fonction Egal est l' | ||
+ | |||
+ | elif fonction == ' | ||
+ | if self.FonctionCourante == ' | ||
+ | self.ValeurCourante += self.ValeurAncienne | ||
+ | elif self.FonctionCourante == ' | ||
+ | self.ValeurCourante = self.ValeurAncienne - self.ValeurCourante | ||
+ | elif self.FonctionCourante == ' | ||
+ | self.ValeurCourante *= self.ValeurAncienne | ||
+ | elif self.FonctionCourante == ' | ||
+ | self.ValeurCourante = self.ValeurAncienne / self.ValeurCourante | ||
+ | self.Rafraichir() | ||
+ | self.ValeurCourante = 0 | ||
+ | self.ValeurAncienne = 0 | ||
issue51/python_partie_25_pp._7-14.1314538670.txt.gz · Dernière modification : 2011/08/28 15:37 de andre_domenech