issue53:tutopython
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 | ||
issue53:tutopython [2011/11/13 19:23] – fredphil91 | issue53:tutopython [2011/11/17 16:00] (Version actuelle) – majordom | ||
---|---|---|---|
Ligne 10: | Ligne 10: | ||
Si vous avez déjà fait la queue pour acheter un billet de cinéma, vous avez été dans une file d' | Si vous avez déjà fait la queue pour acheter un billet de cinéma, vous avez été dans une file d' | ||
- | Dans le monde des ordinateurs, | + | Dans le monde des ordinateurs, |
**There are multiple types of queues. The most common ones are FIFO (First In, First Out), LIFO (Last In, First Out), Priority, and Ring. We'll talk about ring queues another time. | **There are multiple types of queues. The most common ones are FIFO (First In, First Out), LIFO (Last In, First Out), Priority, and Ring. We'll talk about ring queues another time. | ||
Ligne 18: | Ligne 18: | ||
LIFO Queues are less common in life, but there are still real-world examples. The one that comes to mind most quickly is a stack of dishes in your kitchen cabinet. When the dishes are washed and dryed, they get stacked in the cabinet. The last one in on the stack is the first one that comes out to be used. All the rest have to wait, maybe for days, to be used. It's a good thing that the movie ticket queue is FIFO, isn't it? Like the FIFO queue, within reason, there is no limit to the size of a LIFO queue. The first item in the queue has to wait as newer items are pulled out of the buffer (plates pulled off the stack) until it's the only one left.** | LIFO Queues are less common in life, but there are still real-world examples. The one that comes to mind most quickly is a stack of dishes in your kitchen cabinet. When the dishes are washed and dryed, they get stacked in the cabinet. The last one in on the stack is the first one that comes out to be used. All the rest have to wait, maybe for days, to be used. It's a good thing that the movie ticket queue is FIFO, isn't it? Like the FIFO queue, within reason, there is no limit to the size of a LIFO queue. The first item in the queue has to wait as newer items are pulled out of the buffer (plates pulled off the stack) until it's the only one left.** | ||
+ | Il existe plusieurs types de files d' | ||
+ | Les files d' | ||
+ | |||
+ | Les files d' | ||
**Priority queues are a bit harder for many people to imagine right off the bat. Think of a company that has one printer. Everyone uses that one printer. The print jobs are handled by department priority. Payroll has a higher priority (and thankfully so) than say, you, a programmer. You have a higher priority (and thankfully so) than the receptionist. So in short, the data that has a higher priority gets handled, and gets out of the queue, before data that has a lower priority. | **Priority queues are a bit harder for many people to imagine right off the bat. Think of a company that has one printer. Everyone uses that one printer. The print jobs are handled by department priority. Payroll has a higher priority (and thankfully so) than say, you, a programmer. You have a higher priority (and thankfully so) than the receptionist. So in short, the data that has a higher priority gets handled, and gets out of the queue, before data that has a lower priority. | ||
Ligne 28: | Ligne 32: | ||
[1, | [1, | ||
+ | Les files d' | ||
+ | FIFO | ||
+ | |||
+ | Les files d' | ||
+ | |||
+ | [1, | ||
**There are 10 items in the list. As a list, you access them by index. However, in a queue, you can't access the items by index. You have to deal with the next one in the line and the list isn't static. It's VERY dynamic. As we request the next item in the queue, it gets removed. So using the example above, you request one item from the queue. It returns the first item (1) and the queue then looks like this. | **There are 10 items in the list. As a list, you access them by index. However, in a queue, you can't access the items by index. You have to deal with the next one in the line and the list isn't static. It's VERY dynamic. As we request the next item in the queue, it gets removed. So using the example above, you request one item from the queue. It returns the first item (1) and the queue then looks like this. | ||
Ligne 40: | Ligne 50: | ||
I'm sure you get the idea. Python provides a simple library, surprisingly enough, called Queue, that works well for small-to-medium sized queues, up to about 500 items. Here's a simple example to show it.** | I'm sure you get the idea. Python provides a simple library, surprisingly enough, called Queue, that works well for small-to-medium sized queues, up to about 500 items. Here's a simple example to show it.** | ||
+ | Il y a 10 articles dans la liste. En tant que liste, vous y accédez par l' | ||
+ | | ||
+ | |||
+ | Demandez-en deux de plus et vous obtenez 2, puis 3, et la file d' | ||
+ | |||
+ | | ||
+ | |||
+ | Je suis sûr que vous voyez l' | ||
**import Queue | **import Queue | ||
Ligne 54: | Ligne 72: | ||
fifo = Queue.Queue(300)** | fifo = Queue.Queue(300)** | ||
+ | import Queue | ||
+ | fifo = Queue.Queue() | ||
+ | for i in range(5): | ||
+ | fifo.put(i) | ||
+ | |||
+ | while not fifo.empty(): | ||
+ | print fifo.get() | ||
+ | |||
+ | Dans cet exemple, on initialise la file d' | ||
+ | fifo = Queue.Queue(300) | ||
**Once the maximum number of items have been loaded, the Queue blocks any additional entries going into the queue. This has a side effect of making the program look like it's " | **Once the maximum number of items have been loaded, the Queue blocks any additional entries going into the queue. This has a side effect of making the program look like it's " | ||
Ligne 68: | Ligne 96: | ||
print fifo.get()** | print fifo.get()** | ||
+ | Une fois le nombre maximum d' | ||
+ | import Queue | ||
+ | |||
+ | fifo = Queue.Queue(12) | ||
+ | for i in range(13): | ||
+ | if not fifo.full(): | ||
+ | fifo.put(i) | ||
+ | |||
+ | while not fifo.empty(): | ||
+ | print fifo.get() | ||
**In this case, the queue is set for a maximum of 12 items. As we put items into the queue, we start with ' | **In this case, the queue is set for a maximum of 12 items. As we put items into the queue, we start with ' | ||
Ligne 83: | Ligne 121: | ||
[1, | [1, | ||
+ | Ici, la file d' | ||
+ | Il existe d' | ||
+ | |||
+ | LIFO | ||
+ | |||
+ | La bibliothèque « Queue » prend également en charge les files d' | ||
+ | | ||
+ | |||
+ | Si on retire trois éléments de la file d' | ||
+ | |||
+ | | ||
**Remember that in a LIFO queue, items are removed in a LAST-in FIRST-out order. Here's the simple example modified for a LIFO queue... | **Remember that in a LIFO queue, items are removed in a LAST-in FIRST-out order. Here's the simple example modified for a LIFO queue... | ||
Ligne 98: | Ligne 147: | ||
As with the FIFO queue, you have the ability to set the size of the queue, and you can use the .full() check.** | As with the FIFO queue, you have the ability to set the size of the queue, and you can use the .full() check.** | ||
+ | N' | ||
+ | |||
+ | import Queue | ||
+ | lifo = Queue.LifoQueue() | ||
+ | for i in range(5): | ||
+ | lifo.put(i) | ||
+ | while not lifo.empty(): | ||
+ | print lifo.get() | ||
+ | |||
+ | Lorsqu' | ||
+ | Comme pour la file FIFO,vous pouvez régler la taille maximum de la file d' | ||
**PRIORITY | **PRIORITY | ||
Ligne 117: | Ligne 177: | ||
print nex[1]** | print nex[1]** | ||
+ | PRIORITÉ | ||
+ | Même si elle n'est pas souvent utilisée, une file de priorité peut parfois être utile. C'est à peu près la même structure que pour les autres files d' | ||
+ | |||
+ | pq = Queue.PriorityQueue() | ||
+ | pq.put((3,' | ||
+ | pq.put((4,' | ||
+ | pq.put((10,' | ||
+ | pq.put((1,' | ||
+ | |||
+ | while not pq.empty(): | ||
+ | suivant = pq.get() | ||
+ | print suivant | ||
+ | print suivant[1] | ||
**First, we initialize the queue. Then we put four items into the queue. Notice we use the format (priority, data) to put our data. The library sorts our data in a ascending order based on the priority value. When we pull the data, it comes back as a tuple, just like we put it in. You can address by index the data. What we get back is... | **First, we initialize the queue. Then we put four items into the queue. Notice we use the format (priority, data) to put our data. The library sorts our data in a ascending order based on the priority value. When we pull the data, it comes back as a tuple, just like we put it in. You can address by index the data. What we get back is... | ||
Ligne 132: | Ligne 205: | ||
In our first two examples, we simply printed the data that comes out of our queue. That's fine for these examples, but in real-world programming, | In our first two examples, we simply printed the data that comes out of our queue. That's fine for these examples, but in real-world programming, | ||
+ | D' | ||
+ | (1, ' | ||
+ | Haute | ||
+ | (3, ' | ||
+ | Moyenne 1 | ||
+ | (4, ' | ||
+ | Moyenne 2 | ||
+ | (10, ' | ||
+ | Basse | ||
+ | |||
+ | Dans nos deux premiers exemples, nous avons simplement affiché les données qui sortent de notre file d' | ||
**Now let's use some of what we've already learned about tkinter to create a queue demo program. This demo will have two frames. The first will contain (to the user) three buttons. One for a FIFO queue, one for a LIFO queue, and one for a PRIORITY queue. The second frame will contain an entry widget, two buttons, one for adding to the queue, and one for pulling from the queue, and three labels, one showing when the queue is empty, one showing when the queue is full, and one to display what has been pulled from the queue. We'll also be writing some code to automatically center the window within the screen. Here's the beginning of the code. | **Now let's use some of what we've already learned about tkinter to create a queue demo program. This demo will have two frames. The first will contain (to the user) three buttons. One for a FIFO queue, one for a LIFO queue, and one for a PRIORITY queue. The second frame will contain an entry widget, two buttons, one for adding to the queue, and one for pulling from the queue, and three labels, one showing when the queue is empty, one showing when the queue is full, and one to display what has been pulled from the queue. We'll also be writing some code to automatically center the window within the screen. Here's the beginning of the code. | ||
Ligne 149: | Ligne 233: | ||
self.ShowStatus()** | self.ShowStatus()** | ||
+ | Maintenant, nous allons utiliser une partie de ce que nous avons déjà appris sur Tkinter pour créer un programme de démo de file d' | ||
+ | |||
+ | import sys | ||
+ | from Tkinter import * | ||
+ | import ttk | ||
+ | import tkMessageBox | ||
+ | import Queue | ||
+ | class TestFiles: | ||
+ | def __init__(self, | ||
+ | self.DefinirVariables() | ||
+ | f = self.ConstruireWidgets(principale) | ||
+ | self.PlacerWidgets(f) | ||
+ | self.AfficherStatut() | ||
**Here we have our imports and the beginning of our class. As before, we create the __init__ routine with the DefineVars, BuildWidgets, | **Here we have our imports and the beginning of our class. As before, we create the __init__ routine with the DefineVars, BuildWidgets, | ||
Ligne 166: | Ligne 263: | ||
</ | </ | ||
+ | Ici, nous avons nos importations et le début de notre classe. Comme précédemment, | ||
+ | |||
+ | def DefinirVariables(self): | ||
+ | self.TypeDeFile = '' | ||
+ | self.StatutPlein = StringVar() | ||
+ | self.StatutVide = StringVar() | ||
+ | self.Element = StringVar() | ||
+ | self.Sortie = StringVar() | ||
+ | # Definit les files | ||
+ | self.fifo = Queue.Queue(10) | ||
+ | self.lifo = Queue.LifoQueue(10) | ||
+ | self.pq = Queue.PriorityQueue(10) | ||
+ | self.obj = self.fifo | ||
**We now create our DefineVars routine. We have four StringVar() objects, an empty variable called QueueType, and three queue objects - one for each of the types of queues that we are going to play with. We have set the maximum size of the queues at 10 for the purposes of the demo. We also have created an object called obj, and assigned it to the FIFO queue. When we select a queue type from the buttons, we will set this object to the queue that we want. This way, the queue is maintained when we switch to another queue type. | **We now create our DefineVars routine. We have four StringVar() objects, an empty variable called QueueType, and three queue objects - one for each of the types of queues that we are going to play with. We have set the maximum size of the queues at 10 for the purposes of the demo. We also have created an object called obj, and assigned it to the FIFO queue. When we select a queue type from the buttons, we will set this object to the queue that we want. This way, the queue is maintained when we switch to another queue type. | ||
Ligne 198: | Ligne 308: | ||
)** | )** | ||
+ | Nous allons maintenant créer notre routine DefinirVariables. Nous avons quatre objets StringVar(), | ||
+ | def ConstruireWidgets(self, | ||
+ | # Definit nos widgets | ||
+ | fenetre = Frame(principale) | ||
+ | self.f1 = Frame(fenetre, | ||
+ | relief = SUNKEN, | ||
+ | borderwidth=2, | ||
+ | width = 300, | ||
+ | padx = 3, | ||
+ | pady = 3 | ||
+ | ) | ||
+ | self.btnFifo = Button(self.f1, | ||
+ | text = " | ||
+ | ) | ||
+ | | ||
+ | self.btnFifo.bind('< | ||
+ | lambda e: self.btnMain(1) | ||
+ | ) | ||
+ | self.btnLifo = Button(self.f1, | ||
+ | text = " | ||
+ | ) | ||
+ | self.btnLifo.bind('< | ||
+ | lambda e: self.btnMain(2) | ||
+ | ) | ||
+ | self.btnPriority = Button(self.f1, | ||
+ | text = " | ||
+ | ) | ||
+ | self.btnPriority.bind('< | ||
+ | lambda e: self.btnMain(3) | ||
+ | ) | ||
**Here we start the widget definitions. We create our first frame, the three buttons, and their bindings. Notice we are using the same routine to handle the binding callback. Each button sends a value to the callback routine to denote which button was clicked. We could just as easily have created a dedicated routine for each button. However, since all three buttons are dealing with a common task, I thought it would be good to work them as a group. | **Here we start the widget definitions. We create our first frame, the three buttons, and their bindings. Notice we are using the same routine to handle the binding callback. Each button sends a value to the callback routine to denote which button was clicked. We could just as easily have created a dedicated routine for each button. However, since all three buttons are dealing with a common task, I thought it would be good to work them as a group. | ||
Ligne 227: | Ligne 367: | ||
self.btnGet.bind('< | self.btnGet.bind('< | ||
+ | Ici nous commençons la définition des widgets. Nous créons notre premier cadre, les trois boutons et leurs fonctions de rappel. Notez que nous utilisons la même routine pour gérer les fonctions de rappel. Chaque bouton envoie une valeur à la routine de rappel pour indiquer quel bouton a été cliqué. Nous pourrions tout aussi bien pu créer une routine dédiée pour chaque bouton. Cependant, puisque les trois boutons gèrent une tâche commune, j'ai pensé qu'il serait bon de les considérer comme un groupe. | ||
+ | self.f2 = Frame(fenetre, | ||
+ | relief = SUNKEN, | ||
+ | borderwidth=2, | ||
+ | width = 300, | ||
+ | padx = 3, | ||
+ | pady = 3 | ||
+ | ) | ||
+ | self.txtAdd = Entry(self.f2, | ||
+ | width=5, | ||
+ | textvar=self.Element | ||
+ | ) | ||
+ | self.txtAdd.bind('< | ||
+ | self.btnAdd = Button(self.f2, | ||
+ | text=' | ||
+ | padx = 3, | ||
+ | pady = 3 | ||
+ | ) | ||
+ | self.btnAdd.bind('< | ||
+ | self.btnGet = Button(self.f2, | ||
+ | text=' | ||
+ | padx = 3, | ||
+ | pady = 3 | ||
+ | ) | ||
+ | self.btnGet.bind('< | ||
**Next, we set up the second frame, the entry widget, and the two buttons. The only thing here that is out of the ordinary is the binding for the entry widget. Here we bind the self.AddToQueue routine to the < | **Next, we set up the second frame, the entry widget, and the two buttons. The only thing here that is out of the ordinary is the binding for the entry widget. Here we bind the self.AddToQueue routine to the < | ||
Ligne 248: | Ligne 413: | ||
return frame** | return frame** | ||
+ | Ensuite nous mettons en place le second cadre, le widget de saisie et les deux boutons. La seule chose ici qui sort de l' | ||
+ | self.lblEmpty = Label(self.f2, | ||
+ | textvariable=self.StatutVide, | ||
+ | relief=FLAT | ||
+ | ) | ||
+ | self.lblFull = Label(self.f2, | ||
+ | textvariable=self.StatutPlein, | ||
+ | relief=FLAT | ||
+ | ) | ||
+ | self.lblData = Label(self.f2, | ||
+ | textvariable=self.Sortie, | ||
+ | relief = FLAT, | ||
+ | font=(" | ||
+ | padx = 5 | ||
+ | ) | ||
+ | |||
+ | return fenetre | ||
**Here' | **Here' | ||
Ligne 270: | Ligne 452: | ||
</ | </ | ||
+ | Voici les trois dernières définitions de widgets. Toutes les trois sont des étiquettes. Nous réglons l' | ||
+ | |||
+ | def PlacerWidgets(self, | ||
+ | fenetre = principale | ||
+ | # Place les widgets | ||
+ | fenetre.grid(column = 0, row = 0) | ||
+ | l = Label(fenetre, | ||
+ | l = Label(fenetre, | ||
+ | l = Label(fenetre, | ||
+ | l = Label(fenetre, | ||
+ | l = Label(fenetre, | ||
+ | |||
+ | self.f1.grid(column = 0,row = 1, | ||
+ | l = Label(self.f1, | ||
+ | self.btnFifo.grid(column = 1,row = 0,padx = 4) | ||
+ | self.btnLifo.grid(column = 2,row = 0,padx = 4) | ||
+ | self.btnPriority.grid(column = 3, row = 0, padx = 4) | ||
**This is the beginning of the PlaceWidgets routine. Notice here that we put five empty labels at the very top of the root window. I'm doing this to set spacing. This is an easy way to “cheat” and make your window placement much easier. We then set the first frame, then another “cheater” label, then the three buttons. | **This is the beginning of the PlaceWidgets routine. Notice here that we put five empty labels at the very top of the root window. I'm doing this to set spacing. This is an easy way to “cheat” and make your window placement much easier. We then set the first frame, then another “cheater” label, then the three buttons. | ||
Ligne 287: | Ligne 486: | ||
sys.exit()** | sys.exit()** | ||
+ | C'est le début de la routine PlacerWidgets. Remarquez que nous avons mis ici cinq étiquettes vides tout en haut de la fenêtre racine. Je fais cela pour régler l' | ||
+ | |||
+ | self.f2.grid(column = 0,row = 2, | ||
+ | l = Label(self.f2, | ||
+ | self.txtAdd.grid(column=1, | ||
+ | self.btnAdd.grid(column=2, | ||
+ | self.btnGet.grid(column=3, | ||
+ | self.lblEmpty.grid(column=2, | ||
+ | self.lblFull.grid(column=3, | ||
+ | self.lblData.grid(column = 4,row = 0) | ||
+ | |||
+ | Nous plaçons maintenant le deuxième cadre, encore une étiquette « de triche » puis le reste de nos widgets. | ||
+ | |||
+ | def Quitter(self): | ||
+ | sys.exit() | ||
- | | ||
**Next we have our “standard” quit routine which simply calls sys.exit(). | **Next we have our “standard” quit routine which simply calls sys.exit(). | ||
Ligne 308: | Ligne 521: | ||
self.ShowStatus()** | self.ShowStatus()** | ||
+ | Ensuite nous avons notre routine « standard » pour quitter l' | ||
+ | def btnMain(self, | ||
+ | if p1 == 1: | ||
+ | self.TypeDeFile = ' | ||
+ | self.obj = self.fifo | ||
+ | root.title(' | ||
+ | elif p1 == 2: | ||
+ | self.TypeDeFile = ' | ||
+ | self.obj = self.lifo | ||
+ | root.title(' | ||
+ | elif p1 == 3: | ||
+ | self.TypeDeFile = ' | ||
+ | self.obj = self.pq | ||
+ | root.title(' | ||
+ | |||
+ | print self.TypeDeFile | ||
+ | self.AfficherStatut() | ||
**Now our main button callback routine, btnMain. Remember we are sending in (through the p1 parameter) which button was clicked. We use the self.QueueType variable as a reference to which queue type we are dealing with, then we assign self.obj to the proper queue, and finally change the title of our root window to display the queue type we are using. After that, we print the queue type to the terminal window (you don't really have to do that), and call the ShowStatus routine. Next we'll make the ShowStatus routine. | **Now our main button callback routine, btnMain. Remember we are sending in (through the p1 parameter) which button was clicked. We use the self.QueueType variable as a reference to which queue type we are dealing with, then we assign self.obj to the proper queue, and finally change the title of our root window to display the queue type we are using. After that, we print the queue type to the terminal window (you don't really have to do that), and call the ShowStatus routine. Next we'll make the ShowStatus routine. | ||
Ligne 324: | Ligne 554: | ||
self.FullStatus.set('' | self.FullStatus.set('' | ||
+ | Maintenant, notre routine principale de rappel pour les boutons, btnMain. Rappelez-vous que nous lui envoyons (via le paramètre p1) quel bouton a été cliqué. Nous utilisons la variable self.TypeDeFile en référence au type de file d' | ||
+ | def AfficherStatut(self): | ||
+ | # verifie si vide | ||
+ | if self.obj.empty() == True: | ||
+ | self.StatutVide.set(' | ||
+ | else: | ||
+ | self.StatutVide.set('' | ||
+ | # verifie si plein | ||
+ | if self.obj.full() == True: | ||
+ | self.StatutPlein.set(' | ||
+ | else: | ||
+ | self.StatutPlein.set('' | ||
**As you can see, it's pretty simple. We set the label variables to their proper state so they display if the queue we are using is either full, empty, or somewhere in between. | **As you can see, it's pretty simple. We set the label variables to their proper state so they display if the queue we are using is either full, empty, or somewhere in between. | ||
+ | < | ||
def AddToQueue(self, | def AddToQueue(self, | ||
temp = self.Item.get() | temp = self.Item.get() | ||
Ligne 341: | Ligne 584: | ||
self.obj.put(self.Item.get()) | self.obj.put(self.Item.get()) | ||
self.Item.set('' | self.Item.set('' | ||
- | self.ShowStatus()** | + | self.ShowStatus()</ |
+ | Comme vous pouvez le voir, c'est assez simple. Nous réglons les variables d' | ||
+ | |||
+ | def AjouterALaFile(self, | ||
+ | temp = self.Element.get() | ||
+ | if self.TypeDeFile == ' | ||
+ | commapos = temp.find(',' | ||
+ | if commapos == -1: | ||
+ | print " | ||
+ | tkMessageBox.showerror(' | ||
+ | 'Un element Priority doit etre au format\r(priorite, | ||
+ | else: | ||
+ | self.obj.put(self.Element.get()) | ||
+ | elif not self.obj.full(): | ||
+ | self.obj.put(self.Element.get()) | ||
+ | self.Element.set('' | ||
+ | self.AfficherStatut() | ||
- | | ||
**The AddToQueue routine is also fairly straight-forward. We get the data from the entry box using the .get() function. We then check to see if the current queue type is a priority queue. If so, we need to make sure it's in the correct format. We do that by checking for the presence of a comma. If it isn't, we complain to the user via an error message box. If everything seems correct, we then check to see if the queue that we are currently using is full. Remember, if the queue is full, the put routine is blocked and the program will hang. If everything is fine, we add the item to the queue and update the status. | **The AddToQueue routine is also fairly straight-forward. We get the data from the entry box using the .get() function. We then check to see if the current queue type is a priority queue. If so, we need to make sure it's in the correct format. We do that by checking for the presence of a comma. If it isn't, we complain to the user via an error message box. If everything seems correct, we then check to see if the queue that we are currently using is full. Remember, if the queue is full, the put routine is blocked and the program will hang. If everything is fine, we add the item to the queue and update the status. | ||
+ | < | ||
def GetFromQueue(self, | def GetFromQueue(self, | ||
self.Output.set('' | self.Output.set('' | ||
Ligne 352: | Ligne 611: | ||
temp = self.obj.get() | temp = self.obj.get() | ||
self.Output.set(" | self.Output.set(" | ||
- | self.ShowStatus()** | + | self.ShowStatus()</ |
+ | La routine AjouterALaFile est également assez simple. Nous récupérons les données du champ de saisie en utilisant la fonction .get(). Nous vérifions ensuite si le type courant de file d' | ||
+ | def RecupererDansFile(self, | ||
+ | self.Sortie.set('' | ||
+ | if not self.obj.empty(): | ||
+ | temp = self.obj.get() | ||
+ | self.Sortie.set(" | ||
+ | self.AfficherStatut() | ||
**The GetFromQueue routine is even easier. We check to see if the queue is empty so as not to run into a blocking issue, and, if not, we pull the data from the queue, show the data, and update the status. | **The GetFromQueue routine is even easier. We check to see if the queue is empty so as not to run into a blocking issue, and, if not, we pull the data from the queue, show the data, and update the status. | ||
Ligne 371: | Ligne 637: | ||
window.deiconify()** | window.deiconify()** | ||
+ | La routine RecupererDansFile est encore plus facile. Nous vérifions si la file est vide afin de ne pas nous heurter à un problème de blocage et, si ce n'est pas le cas, nous retirons les données de la file d' | ||
+ | |||
+ | if __name__ == ' | ||
+ | def Centrer(window): | ||
+ | # recupere largeur et hauteur de l' | ||
+ | largeurE = window.winfo_screenwidth() | ||
+ | hauteurE = window.winfo_screenheight() | ||
+ | # recupere largeur et hauteur de la fenetre | ||
+ | largeurF = window.winfo_reqwidth() | ||
+ | hauteurF = window.winfo_reqheight() | ||
+ | xc = (largeurE-largeurF)/ | ||
+ | yc = (hauteurE-hauteurF)/ | ||
+ | window.geometry(" | ||
+ | window.deiconify() | ||
- | | ||
**We are getting to the end of our application. Here is the center window routine. We first get the screen width and screen height of the screen we are on. We then get the width and height of the root window by using the winfo_reqwidth() and winfo_reqheight() routines built into tkinter. These routines, when called at the right time, will return the width and height of the root window based on the widget placement. If you call it too early, you'll get data, but it won't be what you really need. We then subtract the required window width from the screen width, and divide it by two, and do the same thing for the height information. We then use that information to set the geometry call. In MOST instances, this works wonderfully. However, there might be times that you need to set the required width and height by hand. | **We are getting to the end of our application. Here is the center window routine. We first get the screen width and screen height of the screen we are on. We then get the width and height of the root window by using the winfo_reqwidth() and winfo_reqheight() routines built into tkinter. These routines, when called at the right time, will return the width and height of the root window based on the widget placement. If you call it too early, you'll get data, but it won't be what you really need. We then subtract the required window width from the screen width, and divide it by two, and do the same thing for the height information. We then use that information to set the geometry call. In MOST instances, this works wonderfully. However, there might be times that you need to set the required width and height by hand. | ||
Ligne 381: | Ligne 660: | ||
root.mainloop()** | root.mainloop()** | ||
+ | Nous arrivons à la fin de notre application. Voici la routine de centrage de fenêtre. Nous récupérons d' | ||
+ | root = Tk() | ||
+ | root.title(' | ||
+ | demo = TestFiles(root) | ||
+ | root.after(3, | ||
+ | root.mainloop() | ||
**Finally, we instantiate the root window, set the base title, instantiate the QueueTest class. We then call root.after, which waits x number of milliseconds (in this case 3) after the root window is instantiated, | **Finally, we instantiate the root window, set the base title, instantiate the QueueTest class. We then call root.after, which waits x number of milliseconds (in this case 3) after the root window is instantiated, | ||
Ligne 389: | Ligne 674: | ||
Well, that's it for this time. Have fun with your queues. The QueueTest code can be found at http:// | Well, that's it for this time. Have fun with your queues. The QueueTest code can be found at http:// | ||
- | Enfin, nous instancions la fenêtre racine, définissons le titre de base, et instancions la class TestFiles. Nous appelons ensuite root.after, qui attend un nombre x de millisecondes (dans ce cas 3) après que la fenêtre racine soit instanciée, | + | Enfin, nous instancions la fenêtre racine, définissons le titre de base et instancions la class TestFiles. Nous appelons ensuite root.after, qui attend un nombre x de millisecondes (dans ce cas 3), après que la fenêtre racine soit instanciée, |
- | Pendant que vous jouez avec les files d' | + | Pendant que vous jouez avec les files d' |
- | Eh bien, c'est tout pour cette fois. Amusez-vous avec vos files d' | + | Eh bien, c'est tout pour cette fois-ci. Amusez-vous avec vos files d' |
issue53/tutopython.1321208596.txt.gz · Dernière modification : 2011/11/13 19:23 de fredphil91