issue64:tuto_python
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 | ||
issue64:tuto_python [2012/09/30 14:14] – fredphil91 | issue64:tuto_python [2012/10/07 09:27] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
Now to the actual meat of this article... more on Kivy.** | Now to the actual meat of this article... more on Kivy.** | ||
- | Avant de commencer, je tiens à souligner que cet article marque les trois ans de la série d' | + | Avant de commencer, je tiens à souligner que cet article marque les trois ans de la série d' |
- | Je tiens aussi à mettre | + | Je tiens aussi à saisir l' |
Maintenant place au contenu de cet article... la suite sur Kivy. | Maintenant place au contenu de cet article... la suite sur Kivy. | ||
Ligne 15: | Ligne 15: | ||
Our app will be fairly simple. A title label, a button with our basic scale as the text, a scrollview (a wonderful parent widget that holds other controls and allows you to “fling” the inside of the control to scroll) holding a number of buttons that have repositioned scales as the text, and an exit button. It will look SOMETHING like the text below.** | Our app will be fairly simple. A title label, a button with our basic scale as the text, a scrollview (a wonderful parent widget that holds other controls and allows you to “fling” the inside of the control to scroll) holding a number of buttons that have repositioned scales as the text, and an exit button. It will look SOMETHING like the text below.** | ||
- | Imaginez que vous jouez de la guitare. Pas de la « air guitare » (en faisant semblant), mais avec une guitare réelle. Cependant, vous n' | + | Imaginez que vous jouez de la guitare. Pas de la « air guitare » (en faisant semblant), mais avec une guitare réelle. Cependant, vous n' |
- | Notre application va être assez simple. Une étiquette de titre, un bouton avec notre échelle basique | + | Notre application va être assez simple. Une étiquette de titre, un bouton avec la gamme de base comme texte, une vue défilante « scrollview » (un widget parent merveilleux qui contient d' |
- | Start with a new python file named main.py. This will be important if/when you decide to create an Android app from Kivy. Now we’ll add our import statements which are shown on the next page, top right. | + | **Start with a new python file named main.py. This will be important if/when you decide to create an Android app from Kivy. Now we’ll add our import statements which are shown on the next page, top right. |
Notice the second line, “kivy.require(‘1.0.8’)”. This allows you to make sure that you can use the latest and greatest goodies that Kivy provides. Also notice that we are including a system exit (line 3). We’ll eventually include an exit button. | Notice the second line, “kivy.require(‘1.0.8’)”. This allows you to make sure that you can use the latest and greatest goodies that Kivy provides. Also notice that we are including a system exit (line 3). We’ll eventually include an exit button. | ||
Ligne 29: | Ligne 29: | ||
def exit(instance): | def exit(instance): | ||
+ | sys.exit()** | ||
+ | |||
+ | Commencez avec un nouveau fichier Python nommé main.py. Ce nom sera important si/quand vous décidez de créer une application Android avec Kivy. Maintenant, nous allons ajouter nos instructions d' | ||
+ | |||
+ | Remarquez la deuxième ligne, « kivy.require(' | ||
+ | |||
+ | Voici le début de notre classe appelée « Transpose ». | ||
+ | |||
+ | class Transpose(App): | ||
+ | def exit(instance): | ||
sys.exit() | sys.exit() | ||
- | Now we work on our build routine (middle right). This is needed for every Kivy app. | + | **Now we work on our build routine (middle right). This is needed for every Kivy app. |
This looks rather confusing. Unfortunately, | This looks rather confusing. Unfortunately, | ||
- | The text2 string should be the same thing but repeated. We will use an offset into the text2 string to fill in the button text within the scrollview widget. | + | The text2 string should be the same thing but repeated. We will use an offset into the text2 string to fill in the button text within the scrollview widget.** |
+ | |||
+ | Maintenant, travaillons sur notre routine « build » (au milieu à droite). Elle est nécessaire pour toutes les applications Kivy. | ||
+ | |||
+ | Cela semble confus. Malheureusement, | ||
+ | |||
+ | La chaîne texte2 devrait être la même chose, mais répétée. Nous allons utiliser un décalage dans la chaîne texte2 pour remplir le texte du bouton à l' | ||
- | Now we create the root object (which is our main window) containing a GridLayout widget. If you remember WAY back when we were doing other GUI development for Glade, there was a grid view widget. Well, the GridLayout here is pretty much the same. In this case, we have a grid that has one column and three rows. In each of the cells created in the grid, we can put other widgets. Remember, we can’t define which widget goes where other than in the order in which we place the widgets. | + | **Now we create the root object (which is our main window) containing a GridLayout widget. If you remember WAY back when we were doing other GUI development for Glade, there was a grid view widget. Well, the GridLayout here is pretty much the same. In this case, we have a grid that has one column and three rows. In each of the cells created in the grid, we can put other widgets. Remember, we can’t define which widget goes where other than in the order in which we place the widgets. |
root = GridLayout(orientation=' | root = GridLayout(orientation=' | ||
Ligne 49: | Ligne 65: | ||
----------------------------- | ----------------------------- | ||
(2) scrollview | (2) scrollview | ||
- | ----------------------------- | + | -----------------------------** |
+ | |||
+ | Nous créons maintenant l' | ||
+ | |||
+ | racine = GridLayout(orientation=' | ||
+ | |||
+ | Dans ce cas, la représentation est la suivante... | ||
+ | |||
+ | ----------------------------- | ||
+ | (0) | ||
+ | ----------------------------- | ||
+ | (1) | ||
+ | ----------------------------- | ||
+ | (2) | ||
+ | | ||
- | The scrollview contains multiple items – in our case buttons. Next, we create the label which will be at the top of our application. | + | **The scrollview contains multiple items – in our case buttons. Next, we create the label which will be at the top of our application. |
lbl = Label(text=' | lbl = Label(text=' | ||
Ligne 57: | Ligne 87: | ||
size_hint=(None, | size_hint=(None, | ||
size=(480, | size=(480, | ||
- | padding=(10, | + | padding=(10, |
- | The properties that are set should be fairly self-explanatory. The only ones that might give you some trouble would be the padding and size_hint properties. The padding is the number of pixels around the item in an x,y reference. Taken directly from the Kivy documentation size_hint (for X which is same as Y) is defined as: | + | La vue scrollview contient plusieurs éléments ; dans notre cas ce sont des boutons. Ensuite, on crée l' |
+ | |||
+ | etiquette = Label(text=' | ||
+ | font_size=20, | ||
+ | size_hint=(None, | ||
+ | size=(480, | ||
+ | padding=(10, | ||
+ | |||
+ | **The properties that are set should be fairly self-explanatory. The only ones that might give you some trouble would be the padding and size_hint properties. The padding is the number of pixels around the item in an x,y reference. Taken directly from the Kivy documentation size_hint (for X which is same as Y) is defined as: | ||
X size hint. Represents how much space the widget should use in the direction of the X axis, relative to its parent’s width. Only Layout and Window make use of the hint. The value is in percent as a float from 0. to 1., where 1. means the full size of his parent. 0.5 represents 50%. | X size hint. Represents how much space the widget should use in the direction of the X axis, relative to its parent’s width. Only Layout and Window make use of the hint. The value is in percent as a float from 0. to 1., where 1. means the full size of his parent. 0.5 represents 50%. | ||
- | In this case, size_hint is set to none, which defaults to 100% or 1. This will be more important (and convoluted) later on. | + | In this case, size_hint is set to none, which defaults to 100% or 1. This will be more important (and convoluted) later on.** |
+ | |||
+ | Les propriétés qui sont définies devraient être assez explicites. Les seules qui pourraient vous poser problème sont celles de « padding » (remplissage) et de « size_hint ». Le remplissage est le nombre de pixels autour de l' | ||
+ | |||
+ | X size hint. Représente la quantité d' | ||
+ | |||
+ | Dans notre cas, size_hint est défini à « none » (aucun), qui vaut par défaut 100 % ou 1. Ce sera plus important (et compliqué) plus tard. | ||
- | Now we define our “main” button (next page, top right). This is a static reference for the scale. | + | **Now we define our “main” button (next page, top right). This is a static reference for the scale. |
Again, most of this should be fairly clear. | Again, most of this should be fairly clear. | ||
Ligne 74: | Ligne 118: | ||
root.add_widget(lbl) | root.add_widget(lbl) | ||
root.add_widget(btn1) | root.add_widget(btn1) | ||
+ | # | ||
+ | |||
+ | Maintenant, nous définissons notre bouton « principal » (en haut à droite de la page suivante). Il s'agit d'une référence statique pour la gamme. | ||
+ | |||
+ | Encore une fois, tout devrait être assez clair. | ||
+ | |||
+ | Maintenant, nous ajoutons les widgets à l' | ||
+ | |||
+ | # | ||
+ | racine.add_widget(etiquette) | ||
+ | racine.add_widget(btn1) | ||
# | # | ||
- | Now comes some harder-to-understand code. We create another GridLayout object and call it “s”. We then bind it to the height of the next widget which, in this case, will be the ScrollView, NOT the buttons. | + | **Now comes some harder-to-understand code. We create another GridLayout object and call it “s”. We then bind it to the height of the next widget which, in this case, will be the ScrollView, NOT the buttons. |
s = GridLayout(cols=1, | s = GridLayout(cols=1, | ||
s.bind(minimum_height=s.setter(' | s.bind(minimum_height=s.setter(' | ||
- | Now (middle right) we create 20 buttons, fill in the text property, and then add it to the GridLayout. | + | Now (middle right) we create 20 buttons, fill in the text property, and then add it to the GridLayout.** |
- | Now we create the ScrollView, set its size, and add it to the root GridLayout. | + | Arrive maintenant du code plus difficile à comprendre. Nous créons un autre objet GridLayout et l' |
+ | |||
+ | s = GridLayout(cols=1, | ||
+ | s.bind(minimum_height=s.setter(' | ||
+ | |||
+ | Maintenant (au milieu à droite), nous créons 20 boutons, remplissons la propriété « texte », | ||
+ | |||
+ | **Now we create the ScrollView, set its size, and add it to the root GridLayout. | ||
sv = ScrollView(size_hint=(None, | sv = ScrollView(size_hint=(None, | ||
Ligne 95: | Ligne 157: | ||
sv.add_widget(s) | sv.add_widget(s) | ||
- | return root | + | return root** |
+ | |||
+ | Maintenant nous créons le ScrollView, définissons sa taille, et l' | ||
+ | |||
+ | sv = ScrollView(size_hint=(None, | ||
+ | sv.center = Window.center | ||
+ | racine.add_widget(sv) | ||
+ | |||
+ | Enfin, nous ajoutons le GridLayout, qui contient tous nos boutons dans le ScrollView, et retournons l' | ||
+ | |||
+ | sv.add_widget(s) | ||
+ | return racine | ||
- | Finally, we have our “if __name__” routine. Notice that we are setting ourselves up for the possibility of using the application as an android app. | + | **Finally, we have our “if __name__” routine. Notice that we are setting ourselves up for the possibility of using the application as an android app. |
if __name__ in (' | if __name__ in (' | ||
Ligne 107: | Ligne 180: | ||
The source code can be found on PasteBin at http:// | The source code can be found on PasteBin at http:// | ||
- | Until next time, enjoy and thank you for putting up with me for three years! | + | Until next time, enjoy and thank you for putting up with me for three years!** |
+ | |||
+ | Enfin, nous avons notre routine « if __name__ ». Remarquez que nous nous réservons la possibilité d' | ||
+ | |||
+ | if __name__ in (' | ||
+ | Transpose().run() | ||
+ | |||
+ | Maintenant, vous vous demandez peut-être pourquoi j'ai utilisé des boutons au lieu d' | ||
+ | |||
+ | Le code source peut être trouvé sur Pastebin : http:// | ||
+ | |||
+ | Jusqu' |
issue64/tuto_python.1349007281.txt.gz · Dernière modification : 2012/09/30 14:14 de fredphil91