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/31 21:33] – effacée fredphil91 | issue51:python_partie_25_pp._7-14 [2011/08/31 21:35] (Version actuelle) – fredphil91 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | **A number of you have commented about the GUI programming articles and how much you've enjoyed them. In response to that, we will start taking a look at a different GUI toolkit called Tkinter. This is the “official” way to do GUI programming in Python. Tkinter has been around for a long time, and has gotten a pretty bad rap for looking “old fashioned”. This has changed recently, so I thought we'd fight that bad thought process. | ||
+ | PLEASE NOTE – All of the code presented here is for Python 2.x only. In an upcoming article, we'll discuss how to use tkinter in Python 3.x. If you MUST use Python 3.x, change the import statements to “from tkinter import *”.** | ||
+ | |||
+ | Un certain nombre d' | ||
+ | NOTE : Tout le code présenté ici est pour Python 2.x seulement. Dans un prochain article, nous allons discuter de la façon d' | ||
+ | |||
+ | **A Little History And A Bit Of Background | ||
+ | |||
+ | Tkinter stands for “Tk interface”. Tk is a programming language all on its own, and the Tkinter module allows us to use the GUI functions there. There are a number of widgets that come natively with the Tkinter module. Some of them are Toplevel (main window) container, Buttons, Labels, Frames, Text Entry, CheckButtons, | ||
+ | |||
+ | Basically, we have the Toplevel container widget which contains (holds) other widgets. This is the root or master window. Within this root window, we place the widgets we want to use within our program. Each widget, other than the Toplevel root widget container, has a parent. The parent doesn' | ||
+ | |||
+ | Un peu d' | ||
+ | |||
+ | Tkinter est l' | ||
+ | |||
+ | Fondamentalement, | ||
+ | |||
+ | **In order to place and display the child widgets, we have to use what's called “geometry management”. It's how things get put into the main root window. Most programmers use one of three types of geometry management, either Packer, Grid, or Place management. In my humble opinion, the Packer method is very clumsy. I'll let you dig into that on your own. The Place management method allows for extremely accurate placement of the widgets, but can be complicated. We'll discuss the Place method in a future article set. For this time, we'll concentrate on the Grid method. | ||
+ | |||
+ | Think of a spreadsheet. There are rows and columns. Columns are vertical, rows are horizontal. Here's a simple text representation of the cell addresses of a simple 5-column by 4-row grid (above right). | ||
+ | So parent has the grid, the widgets go into the grid positions. At first glance, you might think that this is very limiting. However, widgets can span multiple grid positions in either the column direction, the row direction, or both.** | ||
+ | |||
+ | Afin de placer et d' | ||
+ | |||
+ | Pensez à un tableur. Il y a des lignes et des colonnes. Les colonnes sont verticales, les lignes sont horizontales. Voici une représentation texte simple des adresses de cellule d'une grille simple de 5 colonnes sur 4 lignes (en haut à droite). | ||
+ | Le parent possède la grille, les widgets vont dans les positions de la grille. Au premier regard, vous pourriez penser que cela est très limitatif. Toutefois, les widgets peuvent s' | ||
+ | |||
+ | **Our First Example | ||
+ | |||
+ | Our first example is SUPER simple (only four lines), but shows a good bit. | ||
+ | |||
+ | from Tkinter import * | ||
+ | |||
+ | root = Tk() | ||
+ | |||
+ | button = Button(root, | ||
+ | |||
+ | root.mainloop() | ||
+ | |||
+ | Now, what's going on here? Line one imports the Tkinter library. Next, we instantiate the Tk object using root. (Tk is part of Tkinter). Here's line three. | ||
+ | |||
+ | button = Button(root, | ||
+ | |||
+ | Notre premier exemple | ||
+ | |||
+ | Notre premier exemple est SUPER simple (seulement quatre lignes), mais explicite. | ||
+ | |||
+ | from Tkinter import * | ||
+ | racine = Tk() | ||
+ | bouton = Button(racine, | ||
+ | racine.mainloop() | ||
+ | |||
+ | Bon, qu' | ||
+ | |||
+ | bouton = Button(racine, | ||
+ | |||
+ | **We create a button called button, set its parent to the root window, set its text to “Hello FullCircle, | ||
+ | |||
+ | Run the program and let's see what happens. On my machine the main window shows up at the lower left of the screen. It might show up somewhere else on yours. Clicking the button doesn' | ||
+ | |||
+ | Our Second Example | ||
+ | |||
+ | This time, we'll create a class called App. This will be the class that actually holds our window. Let's get started. | ||
+ | |||
+ | from Tkinter import * | ||
+ | |||
+ | This is the import statement for the Tkinter library.** | ||
+ | |||
+ | Nous créons un bouton appelé bouton, définissons son parent à la fenêtre racine, réglons son texte à « Bonjour FullCircle » et le plaçons dans la grille. Enfin, nous appelons la boucle principale de la fenêtre. Ça paraît très simple quand on regarde le code, mais beaucoup de choses se passent dans les coulisses. Heureusement, | ||
+ | |||
+ | Exécutez le programme et nous allons voir ce qui se passe. Sur ma machine, la fenêtre principale apparaît en bas à gauche de l' | ||
+ | |||
+ | Notre deuxième exemple | ||
+ | |||
+ | Cette fois, nous allons créer une classe appelée App. Ce sera la classe qui détient effectivement notre fenêtre. Commençons. | ||
+ | |||
+ | from Tkinter import * | ||
+ | |||
+ | C'est la déclaration d' | ||
+ | |||
+ | **We define our class, and, in the __init__ routine, we set up our widgets and place them into the grid. | ||
+ | |||
+ | The first line in the __init__ routine creates a frame that will be the parent of all of our other widgets. The parent of the frame is the root window (Toplevel widget). Next we define a label, and two buttons. Let's look at the label creation line. | ||
+ | |||
+ | self.lblText = Label(frame, | ||
+ | |||
+ | We create the label widget and call it self.lblText. That's inherited from the Label widget object. We set its parent (frame), and set the text that we want it to display (text = “this is a label widget”). It's that simple. Of course we can do much more than that, but for now that's all we need. Next we set up the two Buttons we will use: | ||
+ | |||
+ | self.btnQuit = Button(frame, | ||
+ | |||
+ | self.btnHello = Button(frame, | ||
+ | |||
+ | Nous définissons notre classe, et dans la routine < | ||
+ | |||
+ | La première ligne dans la routine < | ||
+ | |||
+ | self.lblTexte = Label(cadre, | ||
+ | |||
+ | Nous créons le widget étiquette et l' | ||
+ | |||
+ | self.btnQuitter = Button(cadre, | ||
+ | self.btnBonjour = Button(cadre, | ||
+ | |||
+ | **We name the widgets, set their parent (frame), and set the text we want them to show. Now btnQuit has an attribute marked fg which we set to “red”. You might have guessed this sets the foreground color or text color to the color red. The last attribute is to set the callback command we want to use when the user clicks the button. In the case of btnQuit, it's frame.quit, which ends the program. This is a built in function, so we don't need to actually create it. In the case of btnHello, it's a routine called self.SaySomething. This we have to create, but we have a bit more to go through first. | ||
+ | |||
+ | We need to put our widgets into the grid. Here's the lines again: | ||
+ | |||
+ | frame.grid(column = 0, row = 0) | ||
+ | |||
+ | self.lblText.grid(column = 0, row = 0, columnspan = 2) | ||
+ | |||
+ | self.btnHello.grid(column = 0, row = 1) | ||
+ | |||
+ | self.btnQuit.grid(column = 1, row = 1)** | ||
+ | |||
+ | Nous nommons les widgets, fixons leur parent (cadre) et définissons le texte à afficher. Maintenant btnQuitter a un attribut marqué fg que nous avons réglé à « red ». Vous avez deviné que cela définit la couleur d' | ||
+ | |||
+ | Nous devons placer nos widgets dans la grille. Voici les lignes à nouveau : | ||
+ | |||
+ | 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) | ||
+ | |||
+ | **First, we assign a grid to the frame. Next, we set the grid attribute of each widget to where we want the widget to go. Notice the columnspan line for the label (self.lblText). This says that we want the label to span across two grid columns. Since we have only two columns, that's the entire width of the application. Now we can create our callback function: | ||
+ | |||
+ | def SaySomething(self): | ||
+ | |||
+ | print "Hello to FullCircle Magazine Readers!!" | ||
+ | |||
+ | This simply prints in the terminal window the message “Hello to FullCircle Magazine Readers!!” | ||
+ | Finally, we instantiate the Tk class - our App class - and run the main loop. | ||
+ | |||
+ | root = Tk() | ||
+ | |||
+ | app = App(root) | ||
+ | |||
+ | root.mainloop()** | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | def DitUnTruc(self): | ||
+ | print " | ||
+ | |||
+ | Cela affiche simplement dans la fenêtre du terminal le message " | ||
+ | Enfin, on instancie la classe Tk - notre classe App - et exécutons la boucle principale. | ||
+ | |||
+ | racine = Tk() | ||
+ | app = App(racine) | ||
+ | racine.mainloop() | ||
+ | |||
+ | **Give it a try. Now things actually do something. But again, the window position is very inconvenient. Let's fix that in our next example. | ||
+ | |||
+ | Our Third Example | ||
+ | |||
+ | Save the last example as example3.py. Everything is exactly the same except for one line. It's at the bottom in our main routine calls. I'll show you those lines with our new one: | ||
+ | |||
+ | root = Tk() | ||
+ | |||
+ | root.geometry(' | ||
+ | |||
+ | app = App(root) | ||
+ | |||
+ | root.mainloop()** | ||
+ | |||
+ | Essayez le programme. Maintenant, il fait vraiment quelque chose. Mais là encore, la position de la fenêtre est très gênante. Corrigeons cela dans notre prochain exemple. | ||
+ | |||
+ | Notre troisième exemple | ||
+ | |||
+ | Enregistrez l' | ||
+ | |||
+ | racine = Tk() | ||
+ | racine.geometry(' | ||
+ | app = App(racine) | ||
+ | racine.mainloop() | ||
+ | |||
+ | **What this does is force our initial window to be 150 pixels wide and 75 pixels high. We also want the upper left corner of the window to be placed at X-pixel position 550 (right and left) and the Y-pixel position at 150 (top to botton). How did I come up with these numbers? I started with some reasonable values and tweaked them from there. It's a bit of a pain in the neck to do it this way, but the results are better than not doing it at all. | ||
+ | |||
+ | Our Fourth Example - A Simple Calculator | ||
+ | |||
+ | Now, let's look at something a bit more complicated. This time, we'll create a simple “4 banger” calculator. If you don't know, the phrase “4 banger” means four functions: Add, Subtract, Multiply, and Divide. Right is what it looks like in simple text form. | ||
+ | |||
+ | We'll dive right into it and I'll explain the code (middle right) as we go. | ||
+ | |||
+ | Outside of the geometry statement, this (left) should be pretty easy for you to understand by now. Remember, pick some reasonable values, tweak them, and then move on.** | ||
+ | |||
+ | Ceci force notre fenêtre initiale à une taille de 150 pixels de large sur 75 pixels de haut. Nous voulons aussi que le coin supérieur gauche de la fenêtre soit placé à une position horizontale de 550 pixels (depuis la droite) et à une position verticale de 150 pixels (depuis le haut). Comment suis-je arrivé à ces chiffres ? J'ai commencé avec des valeurs raisonnables et les ai peaufiné à partir de là. C'est un peu difficile de faire de cette façon, mais les résultats sont meilleurs que si on ne fait rien du tout. | ||
+ | |||
+ | Notre Quatrième exemple - Une calculatrice simple | ||
+ | |||
+ | Maintenant, regardons quelque chose d'un peu plus compliqué. Cette fois, nous allons créer une calculatrice simple à 4 boutons, pour les 4 opérations : addition, soustraction, | ||
+ | |||
+ | Nous allons y plonger tout de suite et je vous expliquerai le code (au milieu à droite) au fur et à mesure. | ||
+ | |||
+ | À part la déclaration de la géométrie, | ||
+ | |||
+ | **We begin our class definition and set up our __init__ function. We set up three variables as follows: | ||
+ | • CurrentValue – Holds the current value that has been input into the calculator. | ||
+ | • HolderValue – Holds the value that existed before the user clicks a function key. | ||
+ | • CurrentFunction – This is simply a “bookmark” to note what function is being dealt with. | ||
+ | |||
+ | Next, we define the CurrentDisplay variable and assign it to the StringVar object. This is a special object that is part of the Tkinter toolkit. Whatever widget you assign this to automatically updates the value within the widget. In this case, we will be using this to hold whatever we want the display label widget to... er... well... display. We have to instantiate it before we can assign it to the widget. Then we use the built in ' | ||
+ | |||
+ | Nous commençons notre définition de la classe en mettant en place notre fonction < | ||
+ | |||
+ | • ValeurCourante - Contient la valeur actuelle qui a été entrée dans la calculatrice. | ||
+ | |||
+ | • ValeurAncienne - Contient la valeur qui existait avant que l' | ||
+ | |||
+ | • FonctionCourante - C'est tout simplement pour se souvenir quelle fonction est traitée. | ||
+ | |||
+ | Ensuite, nous définissons la variable AffichageCourant et l' | ||
+ | |||
+ | **def DefineWidgets(self, | ||
+ | |||
+ | self.lblDisplay = Label(master, | ||
+ | |||
+ | Now, we have already defined a label earlier. However, this time we are adding a number of other attributes. Notice that we aren't using the ' | ||
+ | |||
+ | def DefinirWidgets(self, | ||
+ | self.lblAffichage = Label(principale, | ||
+ | |||
+ | Bon, nous avons déjà défini un label auparavant. Cependant, cette fois, nous ajoutons un certain nombre d' | ||
+ | |||
+ | **Next, we set the relief or visual style of the label. The “legal” options here are FLAT, SUNKEN, RAISED, GROOVE, and RIDGE. The default is FLAT if you don't specify anything. Feel free to try the other combinations on your own after we're done. Next, we set the background (bg) to white in order to set it off from the rest of the window a bit. We set the height to 2 (which is two text lines high, not in pixels), and finally assign the variable we just defined a moment ago (self.CurrentDisplay) to the textvariable attribute. Whenever the value of self.CurrentDisplay changes, the label will change its text to match automatically. | ||
+ | |||
+ | Shown above, we'll create some of the buttons. | ||
+ | |||
+ | I've shown only 4 buttons here. That's because, as you can see, the code is almost exactly the same. Again, we've created buttons earlier in this tutor, but let's take a closer look at what we are doing here.** | ||
+ | |||
+ | Ensuite, nous réglons le relief, qui est le style visuel de l' | ||
+ | |||
+ | Ci-dessus, nous allons créer quelques-uns des boutons. | ||
+ | |||
+ | J'ai montré seulement 4 boutons ici. C'est parce que, comme vous pouvez le voir, le code est presque exactement le même. Encore une fois, nous avons créé des boutons plus tôt dans ce tutoriel, mais nous allons regarder de plus près ce que nous faisons ici. | ||
+ | |||
+ | **We start by defining the parent (master), the text that we want on the button, and the width and height. Notice that the width is in characters and the height is in text lines. If you were doing a graphic in the button, you would use pixels to define the height and width. This can become a bit confusing until you get your head firmly wrapped around it. Next, we are setting the bind attribute. When we did the buttons in the previous examples, we used the ' | ||
+ | |||
+ | In Python, we use Lambda to define anonymous functions that will appear to interpreter as a valid statement. This allows us to put multiple segments into a single line of code. Think of it as a mini function. In this case, we are setting up the name of the callback function and the value we want to send as well as the event tag (e:). We'll talk more about Lambda in a later article. For now, just follow the example.** | ||
+ | |||
+ | Nous commençons par définir le parent (la fenêtre principale), | ||
+ | |||
+ | En Python, nous utilisons Lambda pour définir des fonctions anonymes qui apparaîtront à l' | ||
+ | |||
+ | **I've given you the first four buttons. Copy and paste the above code for buttons 5 through 9 and button 0. They are all the same with the exception of the button name and the value we send the callback. Next steps are shown right. | ||
+ | |||
+ | The only thing that hasn't been covered before are the columnspan and sticky attributes. As I mentioned before, a widget can span more than one column or row. In this case, we are “stretching” the label widget across all four columns. That's what the “columnspan” attribute does. There' | ||
+ | |||
+ | Before we go any further let's take a look at how things will work when the user presses buttons. | ||
+ | |||
+ | Let's say the user wants to enter 563 + 127 and get the answer. They will press or click (logically) 5, then 6, then 3, then the “+,” then 1, then 2, then 7, then the “=” buttons. How do we handle this in code? We have already set the callbacks for the number buttons to the funcNumButton function. There' | ||
+ | |||
+ | Je vous ai donné les quatre premiers boutons. Copiez et collez le code ci-dessus pour les boutons de 5 à 9 et pour le bouton 0. Ils sont tous identiques, à l' | ||
+ | |||
+ | La seule chose dont nous n' | ||
+ | |||
+ | Avant d' | ||
+ | |||
+ | Disons que l' | ||
+ | |||
+ | **User clicks 5 – 0 * 10 + 5 (5) | ||
+ | |||
+ | User clicks 6 – 5 * 10 + 6 (56) | ||
+ | |||
+ | User clicks 3 – 56 * 10 + 3 (563) | ||
+ | |||
+ | Of course we then display the “self.CurrentValue” variable in the label. | ||
+ | |||
+ | Next, the user clicks the “+” key. We take the value in “self.CurrentValue” and place it into the variable “self.HolderValue, | ||
+ | |||
+ | Above is the code to start defining our callbacks.** | ||
+ | |||
+ | L' | ||
+ | |||
+ | L' | ||
+ | |||
+ | L' | ||
+ | |||
+ | Bien sûr, nous devons ensuite afficher la variable « self.ValeurCourante » dans l' | ||
+ | |||
+ | Ensuite, l' | ||
+ | |||
+ | Ci-dessus, voici le code pour commencer à définir nos fonctions de rappel. | ||
+ | |||
+ | **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< | ||
+ | |||
+ | 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' | ||
+ | |||
+ | **The “funcClear” routine simply clears the two holding variables, then sets the display. | ||
+ | |||
+ | def funcClear(self): | ||
+ | |||
+ | self.CurrentValue = 0 | ||
+ | |||
+ | self.HolderValue = 0 | ||
+ | |||
+ | self.DisplayIt() | ||
+ | |||
+ | Now the functions. We've already discussed what happens with the function ' | ||
+ | |||
+ | La routine « foncEffacer » efface simplement les deux variables mémoire, puis rafraîchit l' | ||
+ | |||
+ | def foncEffacer(self): | ||
+ | self.ValeurCourante = 0 | ||
+ | self.ValeurAncienne = 0 | ||
+ | self.Rafraichir() | ||
+ | |||
+ | Maintenant les fonctions. Nous avons déjà discuté de ce qui se passe avec la fonction « Dec ». Nous l' | ||
+ | |||
+ | **The next set of steps are shown on the previous page (right hand box). | ||
+ | |||
+ | The DisplayIt routine simply sets the value in the display label. Remember we told the label to “monitor” the variable “self.CurrentDisplay”. Whenever it changes, the label automatically changes the display to match. We use the “.set” method to change the value. | ||
+ | |||
+ | def DisplayIt(self): | ||
+ | |||
+ | print(' | ||
+ | |||
+ | self.CurrentDisplay.set(self.CurrentValue) | ||
+ | |||
+ | Finally we have our startup lines.** | ||
+ | |||
+ | Les prochaines étapes sont indiquées sur la page précédente (encadré de droite). | ||
+ | |||
+ | La routine « Rafraichir » règle simplement la valeur de l' | ||
+ | |||
+ | def Rafraichir(self): | ||
+ | print(' | ||
+ | self.AffichageCourant.set(self.ValeurCourante) | ||
+ | |||
+ | Enfin, nous avons nos lignes de démarrage. | ||
+ | |||
+ | **if __name__ == ' | ||
+ | |||
+ | StartUp() | ||
+ | |||
+ | Now you can run the program and give it a test. | ||
+ | |||
+ | As always, the code for this article can be found at PasteBin. Examples 1, 2 and 3 are at: http:// | ||
+ | |||
+ | Next month, we will continue looking at Tkinter and its wealth of widgets. In a future article, we'll look at a GUI designer for tkinter called PAGE. In the meantime, have fun playing. I think you'll enjoy Tkinter.** | ||
+ | |||
+ | if __name__ == ' | ||
+ | Demarrage() | ||
+ | |||
+ | Maintenant, vous pouvez exécuter le programme et l' | ||
+ | |||
+ | Comme toujours, le code de cet article peut être trouvé sur PasteBin. Les exemples 1, 2 et 3 sont ici : http:// | ||
+ | |||
+ | 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.1314819219.txt.gz · Dernière modification : 2011/08/31 21:33 de fredphil91