issue63:tuto_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue63:tuto_python [2012/08/11 18:25] – créée fredphil91 | issue63:tuto_python [2012/09/15 12:32] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | This time, we are going to take a short detour from our exploration of Android programming, | + | **This time, we are going to take a short detour from our exploration of Android programming, |
- | First off, Kivy is an open source library that makes use of multi-touch displays. If that isn’t cool enough, it’s also cross-platform, | + | First off, Kivy is an open source library that makes use of multi-touch displays. If that isn’t cool enough, it’s also cross-platform, |
- | Before we go too far, let me make a couple of statements. Kivy is VERY powerful. Kivy gives you a new set of tools to make your GUI programming. All that having been said, Kivy is also fairly complicated to deal with. You are limited to the widgets that they have provided. In addition, there is no GUI designer for Kivy, so you have to do a GREAT deal of pre-planning before you try to do anything complicated. Also remember, Kivy is continually under development so things can change quickly. So far, I haven’t found any of my test code that has broken by a new version of Kivy, but that’s always a possibility. | + | Cette fois-ci, nous allons dévier un peu de notre exploration de la programmation Android et regarder un nouvel environnement pour la programmation d' |
+ | |||
+ | Tout d' | ||
+ | |||
+ | **Before we go too far, let me make a couple of statements. Kivy is VERY powerful. Kivy gives you a new set of tools to make your GUI programming. All that having been said, Kivy is also fairly complicated to deal with. You are limited to the widgets that they have provided. In addition, there is no GUI designer for Kivy, so you have to do a GREAT deal of pre-planning before you try to do anything complicated. Also remember, Kivy is continually under development so things can change quickly. So far, I haven’t found any of my test code that has broken by a new version of Kivy, but that’s always a possibility. | ||
Rather than jump in and create our own code this month, we’ll look at some of the examples that come with Kivy, and, next month, we’ll “roll our own”. | Rather than jump in and create our own code this month, we’ll look at some of the examples that come with Kivy, and, next month, we’ll “roll our own”. | ||
Ligne 9: | Ligne 13: | ||
Once you’ve unpacked Kivy into its own folder, use a terminal and change to that folder. Mine is in / | Once you’ve unpacked Kivy into its own folder, use a terminal and change to that folder. Mine is in / | ||
- | It’s very simple, but shows a really neat widget. Below is their code. | + | It’s very simple, but shows a really neat widget. Below is their code.** |
- | As you can see, the first three lines are import statements. Any widget you use must be imported, and you must always import App from kivy.app. | + | Avant d' |
+ | |||
+ | Plutôt que de nous précipiter et de créer notre propre code ce mois-ci, nous allons examiner quelques-uns des exemples livrés avec Kivy et, le mois prochain, nous préparerons le nôtre. | ||
+ | |||
+ | Une fois que vous avez décompressé Kivy dans son propre dossier, utilisez un terminal et allez dans ce dossier. Le mien est dans / | ||
+ | |||
+ | Il est très simple, mais montre un widget très chouette. Ci-dessous, voici leur code. | ||
+ | |||
+ | **As you can see, the first three lines are import statements. Any widget you use must be imported, and you must always import App from kivy.app. | ||
The next eight lines are the main application class. The class is defined, then a routine called build is created. You will almost always have a build routine somewhere in your Kivy programs. Next we set a root object from the Accordion widget. Next we create five AccordionItems and set their title. We then add ten labels with the text “Very big content”. We then add each label to the root widget (the Accordion) and then finally we return the root object. This, in essence, displays the root object in the window that Kivy creates for us. Finally we have the “if __name__” statement and then run the application. | The next eight lines are the main application class. The class is defined, then a routine called build is created. You will almost always have a build routine somewhere in your Kivy programs. Next we set a root object from the Accordion widget. Next we create five AccordionItems and set their title. We then add ten labels with the text “Very big content”. We then add each label to the root widget (the Accordion) and then finally we return the root object. This, in essence, displays the root object in the window that Kivy creates for us. Finally we have the “if __name__” statement and then run the application. | ||
- | Go ahead and run it to see what it does. | + | Go ahead and run it to see what it does.** |
+ | |||
+ | Comme vous pouvez le voir, les trois premières lignes sont des déclarations d' | ||
+ | |||
+ | Les huit lignes suivantes sont la classe principale de l' | ||
+ | |||
+ | Allez-y, exécutez cet exemple pour voir ce qu'il fait. | ||
- | You will see that in a moment or two, a window opens up with five vertical bars in it. Clicking on a bar causes it to open up revealing the ten labels. Of course, each bar has the same text in the ten labels, but you can figure out how to fix that. | + | **You will see that in a moment or two, a window opens up with five vertical bars in it. Clicking on a bar causes it to open up revealing the ten labels. Of course, each bar has the same text in the ten labels, but you can figure out how to fix that. |
The Accordion widget can be used for any number of things, but the thing that has always jumped to my mind is for a configuration screen... each bar being a different configuration set. | The Accordion widget can be used for any number of things, but the thing that has always jumped to my mind is for a configuration screen... each bar being a different configuration set. | ||
Ligne 23: | Ligne 41: | ||
Next we’ll look at the textalign.py example. It’s not as “sexy” as the last one, but it’s a good example that gives you some important information for later on. | Next we’ll look at the textalign.py example. It’s not as “sexy” as the last one, but it’s a good example that gives you some important information for later on. | ||
- | Before we look at the code, run the program. | + | Before we look at the code, run the program.** |
- | What you should see is a label at the top of the window, a set of nine red boxes with text in a 3x3 grid, and four buttons along the bottom of the window. As you click (tap) each of the buttons, the alignment of the text within the red boxes will change. The main reason you would want to pay attention to this example is how to use and control some of the important widgets as well as how to change the alignment in your widgets, which is not completely intuitive. | + | Dans quelques instants, vous verrez une fenêtre s' |
+ | |||
+ | Le widget Accordéon peut être utilisé pour toutes sortes de choses, mais la chose qui me vient toujours à l' | ||
+ | |||
+ | Ensuite, nous allons prendre l' | ||
+ | |||
+ | Avant de regarder le code, exécutez le programme. | ||
+ | |||
+ | **What you should see is a label at the top of the window, a set of nine red boxes with text in a 3x3 grid, and four buttons along the bottom of the window. As you click (tap) each of the buttons, the alignment of the text within the red boxes will change. The main reason you would want to pay attention to this example is how to use and control some of the important widgets as well as how to change the alignment in your widgets, which is not completely intuitive. | ||
Above right is their code for this one. I’ll break it into pieces. First the import code (above right). | Above right is their code for this one. I’ll break it into pieces. First the import code (above right). | ||
Below is something special. They created a class with no code in it. I’ll discuss that in a few minutes: | Below is something special. They created a class with no code in it. I’ll discuss that in a few minutes: | ||
+ | |||
+ | class BoundedLabel(Label): | ||
+ | |||
+ | pass** | ||
+ | |||
+ | Vous devriez voir une étiquette en haut de la fenêtre, un ensemble de neuf cases rouges avec du texte dans une grille 3x3 et quatre boutons le long du bas de la fenêtre. Lorsque vous cliquez (appuyez) sur chacun des boutons, l' | ||
+ | |||
+ | Le code de cet exemple se trouve ci-dessus à droite. Je vais le décortiquer. D' | ||
+ | |||
+ | Ci-dessous se trouve quelque chose de spécial. Ils ont créé une classe qui ne contient aucun code. Je vais en parler dans quelques minutes : | ||
class BoundedLabel(Label): | class BoundedLabel(Label): | ||
Ligne 35: | Ligne 71: | ||
pass | pass | ||
- | Next a class called “Selector” (below) is created: | + | **Next a class called “Selector” (below) is created: |
class Selector(FloatLayout): | class Selector(FloatLayout): | ||
Ligne 44: | Ligne 80: | ||
Here the routine select is created. A GridLayout widget is created (called grid) which has 3 rows and 3 columns. This grid is going to hold the nine red boxes. | Here the routine select is created. A GridLayout widget is created (called grid) which has 3 rows and 3 columns. This grid is going to hold the nine red boxes. | ||
+ | |||
+ | for valign in (' | ||
+ | |||
+ | for halign in (' | ||
+ | |||
+ | Ensuite, une classe appelée « Selector » (ci-dessous) est créée : | ||
+ | |||
+ | class Selector(FloatLayout): | ||
+ | |||
+ | app = ObjectProperty(None) | ||
+ | |||
+ | Maintenant, la classe Application est créée. | ||
+ | |||
+ | C'est ici que la routine de sélection est créée. Un widget GridLayout est créé (appelée grid ou grille), qui dispose de 3 lignes et 3 colonnes. Cette grille va contenir les neuf cases rouges. | ||
for valign in (' | for valign in (' | ||
Ligne 49: | Ligne 99: | ||
for halign in (' | for halign in (' | ||
- | Here we have two loops, one inner and one outer. | + | **Here we have two loops, one inner and one outer. |
label = BoundedLabel(text=' | label = BoundedLabel(text=' | ||
Ligne 59: | Ligne 109: | ||
In the code above, an instance of the BoundedLabel widget is created, once for each of the nine red boxes. You might want to stop here and say “But wait! There isn’t a BoundedLabel widget. It just has a pass statement in it.” Well, yes, and no. We are creating an instance of a custom widget. As I said a little bit above, we’ll talk more about that in a minute. | In the code above, an instance of the BoundedLabel widget is created, once for each of the nine red boxes. You might want to stop here and say “But wait! There isn’t a BoundedLabel widget. It just has a pass statement in it.” Well, yes, and no. We are creating an instance of a custom widget. As I said a little bit above, we’ll talk more about that in a minute. | ||
- | In the code block (top right, next page), we examine the variable ‘case’ which is passed into the select routine. | + | In the code block (top right, next page), we examine the variable ‘case’ which is passed into the select routine.** |
- | Here, the grid is removed, to clear the screen. | + | Ici nous avons deux boucles, l'une extérieure et l' |
+ | |||
+ | label = BoundedLabel(text=' | ||
+ | |||
+ | size_hint=(None, | ||
+ | |||
+ | halign=halign, | ||
+ | |||
+ | Dans le code ci-dessus, une instance du widget BoundedLabel est créée, une fois pour chacune des neuf cases rouges. Vous voudrez peut-être m' | ||
+ | |||
+ | Dans le bloc de code (en haut à droite de la page suivante), nous examinons la variable « case », qui est passée à la routine de sélection. | ||
+ | |||
+ | **Here, the grid is removed, to clear the screen. | ||
if self.grid: | if self.grid: | ||
Ligne 75: | Ligne 137: | ||
self.root.add_widget(grid) | self.root.add_widget(grid) | ||
- | Remember in the last example I said that you will almost always use a build routine. Here is the one for this example. The root object is created with a FloatLayout widget. Next (middle right) we call the Selector class to create a Selector object, then it’s added to the root object, and we initialize the display by calling self.select(0). | + | Remember in the last example I said that you will almost always use a build routine. Here is the one for this example. The root object is created with a FloatLayout widget. Next (middle right) we call the Selector class to create a Selector object, then it’s added to the root object, and we initialize the display by calling self.select(0).** |
- | Finally the application is allowed to run. | + | Ici, on retire la grille pour effacer l' |
+ | |||
+ | if self.grid: | ||
+ | |||
+ | self.root.remove_widget(self.grid) | ||
+ | |||
+ | La méthode bind ici définit la taille et la grille est ajoutée à l' | ||
+ | |||
+ | grid.bind(minimum_size=grid.setter(' | ||
+ | |||
+ | self.grid = grid | ||
+ | |||
+ | self.root.add_widget(grid) | ||
+ | |||
+ | Rappelez-vous, | ||
+ | |||
+ | **Finally the application is allowed to run. | ||
TextAlignApp().run() | TextAlignApp().run() | ||
Ligne 87: | Ligne 165: | ||
#:kivy 1.0 | #:kivy 1.0 | ||
- | Here the BoundedLabel widget is created. Each of the red boxes in the application is a BoundedLabel. | + | Here the BoundedLabel widget is created. Each of the red boxes in the application is a BoundedLabel.** |
- | Color sets the background color of the box to red (rgb: 1,0,0). The Rectangle widget creates a (you guessed it) rectangle. When we call the BoundedLabel widget in the actual application code, we are passing a label as the parent. The size and position (here in the .kv file) are set to whatever the size and position of the label are. | + | Enfin, l' |
+ | |||
+ | TextAlignApp().run() | ||
+ | |||
+ | Maintenant, avant d' | ||
+ | |||
+ | Cette première ligne indique la version minimum de Kivy qui doit être utilisée pour exécuter cette application. | ||
+ | |||
+ | #:kivy 1.0 | ||
+ | |||
+ | Ensuite le widget BoundedLabel est créé. Chacune des cases rouges dans l' | ||
+ | |||
+ | **Color sets the background color of the box to red (rgb: 1,0,0). The Rectangle widget creates a (you guessed it) rectangle. When we call the BoundedLabel widget in the actual application code, we are passing a label as the parent. The size and position (here in the .kv file) are set to whatever the size and position of the label are. | ||
Here (right, next page) the Selector widget is created. This is the four buttons that appear at the bottom of the window as well as the label across the top of the window. | Here (right, next page) the Selector widget is created. This is the four buttons that appear at the bottom of the window as well as the label across the top of the window. | ||
Ligne 95: | Ligne 185: | ||
Notice that the label that makes up the title at the top of the window has a position (pos_hint) as top, has a height of 50 pixels and a font size of 16. Each of the buttons has an alignment for the text of center. The on_release statement is a bind-like statement so that, when the button is released, it calls (in this case) root.app.select with a case value. | Notice that the label that makes up the title at the top of the window has a position (pos_hint) as top, has a height of 50 pixels and a font size of 16. Each of the buttons has an alignment for the text of center. The on_release statement is a bind-like statement so that, when the button is released, it calls (in this case) root.app.select with a case value. | ||
- | Hopefully, this is beginning to make sense now. You can see why Kivy is so powerful. | + | Hopefully, this is beginning to make sense now. You can see why Kivy is so powerful.** |
- | Let’s talk for a moment about two widgets that I have passed over in the discussion of the application code, The GridLayout and the FloatLayout. | + | Color définit la couleur d' |
+ | |||
+ | Ici (à droite sur la page suivante) le widget de sélection est créé. Ce sont les quatre boutons qui apparaissent au bas de la fenêtre ainsi que l' | ||
+ | |||
+ | Remarquez que l' | ||
+ | |||
+ | J' | ||
+ | |||
+ | **Let’s talk for a moment about two widgets that I have passed over in the discussion of the application code, The GridLayout and the FloatLayout. | ||
The GridLayout is a parent widget that uses a row and column description to allow widgets to be placed in each cell. In this case, it is a 3x3 grid (like a Tic-Tac-Toe (or Naughts and Crosses) board). | The GridLayout is a parent widget that uses a row and column description to allow widgets to be placed in each cell. In this case, it is a 3x3 grid (like a Tic-Tac-Toe (or Naughts and Crosses) board). | ||
Ligne 105: | Ligne 203: | ||
| | | | | | ||
- | When you want to place a widget into a GridLayout, you use the add_widget method. Here lies a problem. You can’t specify which control goes into which grid cell other than the order in which you add them. In addition, each widget is added from left to right, top to bottom. You can’t have an empty cell. Of course, you can cheat. I’ll leave that up to you to figure out. | + | When you want to place a widget into a GridLayout, you use the add_widget method. Here lies a problem. You can’t specify which control goes into which grid cell other than the order in which you add them. In addition, each widget is added from left to right, top to bottom. You can’t have an empty cell. Of course, you can cheat. I’ll leave that up to you to figure out.** |
- | The FloatLayout widget seems to be just a parent container for other child widgets. | + | Examinons un instant deux widgets dont je n'ai pas parlé ci-dessus pendant la discussion du code de l' |
+ | |||
+ | GridLayout est un widget parent qui utilise une description en ligne et colonne qui permet de placer les widgets dans chaque cellule. Dans ce cas, il s'agit d'une grille 3x3 (comme un plateau de Tic-Tac-Toe). | ||
+ | |||
+ | __|__|__ | ||
+ | __|__|__ | ||
+ | | | | ||
+ | |||
+ | Lorsque vous voulez placer un widget dans un GridLayout, vous utilisez la méthode add_widget. Il y a cependant un problème. Vous ne pouvez pas spécifier la cellule de la grille dans laquelle chaque commande sera placée autrement que par l' | ||
+ | |||
+ | **The FloatLayout widget seems to be just a parent container for other child widgets. | ||
I’ve glossed over a few points for now. My intent this time was simply to get you somewhat excited about the possibilities that Kivy has to offer. In the next couple of articles, we’ll continue to explore what Kivy has for us, how to use various widgets, and how to create an APK to publish our applications to Android. | I’ve glossed over a few points for now. My intent this time was simply to get you somewhat excited about the possibilities that Kivy has to offer. In the next couple of articles, we’ll continue to explore what Kivy has for us, how to use various widgets, and how to create an APK to publish our applications to Android. | ||
- | Until then, explore more of the examples in Kivy, and be sure to go to the documentation page for Kivy at http:// | + | Until then, explore more of the examples in Kivy, and be sure to go to the documentation page for Kivy at http:// |
+ | |||
+ | Le widget FloatLayout semble être juste un conteneur parent pour d' | ||
+ | |||
+ | J'ai passé sous silence quelques points pour l' | ||
+ | |||
+ | En atendant, explorez davantage les exemples de Kivy et, surtout, allez sur la page de documentation de Kivy : http:// | ||
issue63/tuto_python.1344702337.txt.gz · Dernière modification : 2012/08/11 18:25 de fredphil91