issue65:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue65:python [2012/10/06 18:17] – créée andre_domenech | issue65:python [2012/10/21 15:29] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Titre : Programming in Python – Part 37 | + | **Titre : Programming in Python – Part 37** |
- | This month, we’ll finish up the transposer program that we wrote in Kivy. Hopefully, you saved the code from last time, because we’ll be building upon it. If not, grab the code from FCM#64. | + | Programmer en Python. Partie 37. |
+ | |||
+ | **This month, we’ll finish up the transposer program that we wrote in Kivy. Hopefully, you saved the code from last time, because we’ll be building upon it. If not, grab the code from FCM#64. | ||
Let’s start by recapping what we did last month. We created an application that allows for a guitarist to quickly transpose from one key to the other. The ultimate goal is to be able to run this app not only on your Linux or Windows box, but on an android device as well. I take mine on my tablet whenever I go to band practice. I was going to deal with packaging our project for Android, but some things have changed in the method to do that, so we’ll work on that next month. | Let’s start by recapping what we did last month. We created an application that allows for a guitarist to quickly transpose from one key to the other. The ultimate goal is to be able to run this app not only on your Linux or Windows box, but on an android device as well. I take mine on my tablet whenever I go to band practice. I was going to deal with packaging our project for Android, but some things have changed in the method to do that, so we’ll work on that next month. | ||
Ligne 7: | Ligne 9: | ||
The app, as we left it last time, looked like that shown below left. | The app, as we left it last time, looked like that shown below left. | ||
- | When we are done, it should look like the screen below right. | + | When we are done, it should look like the screen below right.** |
+ | |||
+ | Ce mois-ci, nous allons terminer le programme de transposition que nous avons écrit dans Kivy. J' | ||
+ | |||
+ | Commençons par récapituler ce que nous avons fait le mois dernier. Nous avons créé une application qui permet à un guitariste de transposer rapidement d'une clé à une autre. Le but ultime est de pouvoir exécuter cette application non seulement sous Linux ou Windows, mais également sur un appareil Android. Je l' | ||
+ | |||
+ | L' | ||
+ | |||
+ | Lorsque nous aurons terminé, elle devrait ressembler à l' | ||
- | The first thing you will notice is that there are blue labels rather than boring gray ones. The next is that there are three buttons. Finally the scrollable labels are closer to the entire width of the window. Other than that, it’s pretty much (visually) the same. One of the buttons is an “about” button that will pop up simple information, | + | **The first thing you will notice is that there are blue labels rather than boring gray ones. The next is that there are three buttons. Finally the scrollable labels are closer to the entire width of the window. Other than that, it’s pretty much (visually) the same. One of the buttons is an “about” button that will pop up simple information, |
Let’s get started by creating a .kv file (above right). This is what will give us the colored labels. It’s a very simple file. | Let’s get started by creating a .kv file (above right). This is what will give us the colored labels. It’s a very simple file. | ||
- | The first two lines are required. They basically say what version of Kivy to expect. Next we create a new type of label called ‘BoundedLabel’. The color is set with RGB values (between 0 and 1, which can be considered as 100 percent), and as you can see the blue value is set at 100 percent. We will also create a rectangle which is the actual label. Save this as “transpose.kv”. You must use the name of the class that will be using it. | + | The first two lines are required. They basically say what version of Kivy to expect. Next we create a new type of label called ‘BoundedLabel’. The color is set with RGB values (between 0 and 1, which can be considered as 100 percent), and as you can see the blue value is set at 100 percent. We will also create a rectangle which is the actual label. Save this as “transpose.kv”. You must use the name of the class that will be using it.** |
- | Now that you have that completed, add the following lines just before the transpose class to the source file from last time: | + | La première chose que vous remarquerez est qu'il y a des étiquettes bleues à la place de celles qui étaient grises et tristes. La suivante est qu'il y a trois boutons. Enfin, les étiquettes qui défilent sont plus proches de la largeur totale de la fenêtre. À part ça, c'est à peu près la même chose (visuellement). L'un des boutons est un bouton « à propos » qui affichera quelques informations simples, pour vous montrer comment faire un pop-up simple. Un autre bouton sert à quitter. Le troisième bouton va remplacer l' |
- | class BoundedLabel(Label): | + | Nous allons commencer par créer un fichier .kv (en haut à droite). C'est ce qui va nous donner les étiquettes colorées. C'est un fichier très simple. |
+ | Les deux premières lignes sont nécessaires. Elles indiquent simplement quelle version de Kivy est requise. Ensuite, nous créons un nouveau type d' | ||
+ | |||
+ | **Now that you have that completed, add the following lines just before the transpose class to the source file from last time: | ||
+ | |||
+ | class BoundedLabel(Label): | ||
pass | pass | ||
To make it work, all we need is a definition. Before we go any further, add the following line to the import section: | To make it work, all we need is a definition. Before we go any further, add the following line to the import section: | ||
- | from kivy.uix.popup import Popup | + | from kivy.uix.popup import Popup** |
+ | |||
+ | Maintenant que ceci est terminé, ajoutez les lignes suivantes juste avant la classe transpose dans le fichier source de la dernière fois : | ||
+ | |||
+ | class BoundedLabel(Label): | ||
+ | pass | ||
+ | |||
+ | Pour que cela fonctionne, il suffit d'une définition. Avant d' | ||
+ | |||
+ | | ||
- | This allows us to create the popup later on. Now, in the Transpose class, just inside the def build routine, place the code shown above right. | + | **This allows us to create the popup later on. Now, in the Transpose class, just inside the def build routine, place the code shown above right. |
The LoadLabels routine will give us the colored labels (BoundedLabel) and the swap ability. You saw most of this last time. We pass a value to the “w” parameter to determine which text is being displayed. The l=BoundedLabel line is pretty much the same line from last time, with the exception that, this time, we are using a BoundedLabel widget instead of a Button widget. The LoadLabels will mainly be called from the next routine, Swap. Place this code (shown right) below LoadLabels. | The LoadLabels routine will give us the colored labels (BoundedLabel) and the swap ability. You saw most of this last time. We pass a value to the “w” parameter to determine which text is being displayed. The l=BoundedLabel line is pretty much the same line from last time, with the exception that, this time, we are using a BoundedLabel widget instead of a Button widget. The LoadLabels will mainly be called from the next routine, Swap. Place this code (shown right) below LoadLabels. | ||
Ligne 31: | Ligne 55: | ||
You can see that this routine is pretty self explanatory. We use a variable (self.whichway) to determine “which way” the labels are displaying... from Guitar to Piano or Piano to Guitar. | You can see that this routine is pretty self explanatory. We use a variable (self.whichway) to determine “which way” the labels are displaying... from Guitar to Piano or Piano to Guitar. | ||
- | Be sure to save your work at this point, since we are going to be making a lot of changes from here on. | + | Be sure to save your work at this point, since we are going to be making a lot of changes from here on.** |
- | Replace the lines defining text1 and text two with the lines shown above. | + | Cela nous permet de créer la popup plus tard. Maintenant, dans la classe Transpose, juste à l' |
+ | |||
+ | La routine ChargeEtiquettes nous donnera les étiquettes de couleur (BoundedLabel) et la capacité d' | ||
+ | |||
+ | Vous pouvez voir que cette routine est assez explicite. Nous utilisons une variable (self.quelsens) pour déterminer « dans quel sens » les étiquettes s' | ||
+ | |||
+ | Assurez-vous de sauvegarder votre travail maintenant, car nous allons faire beaucoup de changements à partir de maintenant. | ||
+ | |||
+ | **Replace the lines defining text1 and text two with the lines shown above. | ||
We set self.whichway to 0 which will be our default for the swap procedure. Then we define four strings instead of the two we had last time. You might notice that strings text3 and text4 are simple reversals of text1 and text2. | We set self.whichway to 0 which will be our default for the swap procedure. Then we define four strings instead of the two we had last time. You might notice that strings text3 and text4 are simple reversals of text1 and text2. | ||
Ligne 45: | Ligne 77: | ||
root = GridLayout(orientation=' | root = GridLayout(orientation=' | ||
- | We’ve changed the spacing from 10 to 6 and set the default row height to 40 pixels. Change the text for the label (next line) to “text=' | + | We’ve changed the spacing from 10 to 6 and set the default row height to 40 pixels. Change the text for the label (next line) to “text=' |
- | Now change the button definition line from... | + | Remplacez les lignes définissant texte1 et texte2 avec les lignes ci-dessus. |
+ | |||
+ | Nous réglons self.quelsens à 0 qui sera notre valeur par défaut pour la procédure d' | ||
+ | |||
+ | Maintenant, nous allons adapter la définition de la ligne racine. Changez-le de : | ||
+ | |||
+ | root = GridLayout(orientation=' | ||
+ | |||
+ | à : | ||
+ | |||
+ | root = GridLayout(orientation=' | ||
+ | |||
+ | Nous avons changé l' | ||
+ | |||
+ | **Now change the button definition line from... | ||
btn1 = Button(text = " | btn1 = Button(text = " | ||
Ligne 67: | Ligne 113: | ||
font_name=' | font_name=' | ||
+ | padding=(20, | ||
+ | background_color=[0.39, | ||
+ | |||
+ | Maintenant changez la définition du bouton de : | ||
+ | |||
+ | btn1 = Button(text = " | ||
+ | size_hint=(None, | ||
+ | halign=' | ||
+ | font_name=' | ||
+ | padding=(20, | ||
+ | |||
+ | à : | ||
+ | |||
+ | btn1 = Button(text = " | ||
+ | size_hint=(None, | ||
+ | halign=' | ||
+ | font_name=' | ||
padding=(20, | padding=(20, | ||
background_color=[0.39, | background_color=[0.39, | ||
- | Notice that I’ve changed the formatting of the first definition for clarity. The big changes are the size change from 680,40 to 780,20 and the background color for the button. Remember, we can change the background color for buttons, not “standard” labels. | + | **Notice that I’ve changed the formatting of the first definition for clarity. The big changes are the size change from 680,40 to 780,20 and the background color for the button. Remember, we can change the background color for buttons, not “standard” labels. |
Next, we will define three AnchorLayout widgets for the three buttons that we will add in later. I named them al0 (AnchorLayout0), | Next, we will define three AnchorLayout widgets for the three buttons that we will add in later. I named them al0 (AnchorLayout0), | ||
Ligne 78: | Ligne 141: | ||
LoadLabels(0) | LoadLabels(0) | ||
- | This calls the LoadLabels routine with our default “which” of 0. | + | This calls the LoadLabels routine with our default “which” of 0.** |
- | Next, comment out the entire for loop code. This starts with “for i in range(0, | + | Remarquez que j'ai changé le format de la première définition pour plus de clarté. Les gros changements sont la taille qui passe de 680,40 à 780,20 et la couleur de fond du bouton. Rappelez-vous, |
+ | |||
+ | Ensuite, nous allons définir trois widgets AnchorLayout pour les trois boutons que nous ajouterons plus tard. Je les ai nommés al0 (AnchorLayout0), | ||
+ | |||
+ | Trouvez la ligne « s = GridLayout » et modifiez l' | ||
+ | |||
+ | ChargeEtiquettes(0) | ||
+ | |||
+ | Ceci appelle la routine ChargeEtiquettes avec notre « quelsens » par défaut qui vaut 0. | ||
+ | |||
+ | **Next, comment out the entire for loop code. This starts with “for i in range(0, | ||
Now, save your code and try to run it. You should see a deep purple button at the top, and our pretty blue BoundLabels. Plus, you will notice that the BoundLabels in the scroll window are closer together, which makes it much easier to read. | Now, save your code and try to run it. You should see a deep purple button at the top, and our pretty blue BoundLabels. Plus, you will notice that the BoundLabels in the scroll window are closer together, which makes it much easier to read. | ||
Ligne 86: | Ligne 159: | ||
We are almost through with our code, but we still have a few things to do. After the “sv = ScrollView” line add the following line... | We are almost through with our code, but we still have a few things to do. After the “sv = ScrollView” line add the following line... | ||
- | sv.size = (720, 320) | + | sv.size = (720, 320)** |
+ | |||
+ | Ensuite, commentez la totalité du code de la boucle for. Cela commence par « for i in range(0, | ||
+ | |||
+ | Maintenant, enregistrez votre code et essayez de l' | ||
+ | |||
+ | Nous sommes presque au bout de notre code, mais il nous reste quelques petites choses à faire. Après la ligne « sv = ScrollView », ajoutez la ligne suivante : | ||
+ | |||
+ | | ||
- | This sets the size of the ScrollView widget to 720 by 320 – which makes it wider within the root window. Now, before the “return root” line, add the code shown top right. | + | **This sets the size of the ScrollView widget to 720 by 320 – which makes it wider within the root window. Now, before the “return root” line, add the code shown top right. |
Here we add the three buttons to the AnchorLayout widgets, create a GridLayout to hold the AnchorLayouts, | Here we add the three buttons to the AnchorLayout widgets, create a GridLayout to hold the AnchorLayouts, | ||
Ligne 98: | Ligne 179: | ||
popup.open() | popup.open() | ||
- | That’s it. Save and run the code. If you click on the About button, you will see the simple popup. Just click anywhere outside of the popup to make it go away. | + | That’s it. Save and run the code. If you click on the About button, you will see the simple popup. Just click anywhere outside of the popup to make it go away.** |
- | Now our code is written. You can find the full code at http:// | + | Cela définit la taille du widget ScrollView à 720 sur 320, ce qui le rend plus large à l' |
+ | |||
+ | Ici nous ajoutons les trois boutons aux widgets AnchorLayout, | ||
+ | |||
+ | Retournez juste en dessous de la routine « def Echange » et ajoutez ce qui suit : | ||
+ | |||
+ | def AfficheAPropos(instance): | ||
+ | popup.open() | ||
+ | |||
+ | C'est tout. Enregistrez et exécutez le code. Si vous cliquez sur le bouton « À propos », vous verrez le popup tout simple. Cliquez n' | ||
+ | |||
+ | **Now our code is written. You can find the full code at http:// | ||
Next, we need to create our android package... but that will have to wait for next time. | Next, we need to create our android package... but that will have to wait for next time. | ||
Ligne 106: | Ligne 198: | ||
If you want to get set up and try packaging for Android before next month, you should go to http:// | If you want to get set up and try packaging for Android before next month, you should go to http:// | ||
- | See you next month. | + | See you next month.** |
+ | |||
+ | Maintenant, notre code est écrit. Vous pouvez trouver le code complet ici : http:// | ||
+ | |||
+ | Ensuite, nous devons créer notre paquet android... mais cela devra attendre la prochaine fois. | ||
+ | |||
+ | Si vous voulez vous préparer et essayer d' | ||
+ | |||
+ | Rendez-vous le mois prochain. |
issue65/python.1349540232.txt.gz · Dernière modification : 2012/10/06 18:17 de andre_domenech