issue64:tuto_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue64:tuto_python [2012/09/13 22:59] – créée fredphil91 | issue64:tuto_python [2012/10/07 09:27] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Before I begin, I want to note that this article marks three years of the Beginning Programming using Python series. I want to thank Ronnie and the entire staff of Full Circle Magazine for all their support and especially, you, the readers. I NEVER thought that this would continue this long. | + | **Before I begin, I want to note that this article marks three years of the Beginning Programming using Python series. I want to thank Ronnie and the entire staff of Full Circle Magazine for all their support and especially, you, the readers. I NEVER thought that this would continue this long. |
I also want to take a second to note that there has been some comments floating around the ether that, after three years, the word “Beginning” might be misplaced in the title of this series. After all, after three years, would you still be a beginner? Well, on some levels, I agree. However, I still get comments from readers saying that they just found the series and Full Circle Magazine, and that they are now running back to the beginning of the series. So, those people ARE beginners. So, as of part 37, we’ll drop “Beginning” from the series title. | I also want to take a second to note that there has been some comments floating around the ether that, after three years, the word “Beginning” might be misplaced in the title of this series. After all, after three years, would you still be a beginner? Well, on some levels, I agree. However, I still get comments from readers saying that they just found the series and Full Circle Magazine, and that they are now running back to the beginning of the series. So, those people ARE beginners. So, as of part 37, we’ll drop “Beginning” from the series title. | ||
- | Now to the actual meat of this article... more on Kivy. | + | Now to the actual meat of this article... more on Kivy.** |
- | Imagine you play guitar. Not air guitar, but an actual guitar. However, you aren’t the best guitar player, and some chords are problematical for you. For example, you know the standard C, E, G, F type chords, but some chords – like F# minor or C# minor – while doable, are hard to get your fingers set in a fast song. What do you do, especially if the gig is only a couple of weeks away and you HAVE to be up to speed TODAY? The workaround for this is to use the Capo (that funny clippy thing that you see sometimes on the neck of the guitar). This raises the key of the guitar and you use different chords to match the rest of the band. This is called transposing. Sometimes, you can transpose on the fly in your head. Sometimes, it’s easier to sit down on paper and work out that if, for example, the chord is F# minor and you put the capo on fret 2, you can simply play an E minor. But that takes time. Let’s make an app that allows you to simply scroll through the fret positions to find the easiest chords to play. | + | Avant de commencer, je tiens à souligner que cet article marque les trois ans de la série d' |
- | 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. | + | Je tiens aussi à saisir l' |
- | 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. | + | Maintenant place au contenu de cet article... la suite sur Kivy. |
+ | |||
+ | **Imagine you play guitar. Not air guitar, but an actual guitar. However, you aren’t the best guitar player, and some chords are problematical for you. For example, you know the standard C, E, G, F type chords, but some chords – like F# minor or C# minor – while doable, are hard to get your fingers set in a fast song. What do you do, especially if the gig is only a couple of weeks away and you HAVE to be up to speed TODAY? The workaround for this is to use the Capo (that funny clippy thing that you see sometimes on the neck of the guitar). This raises the key of the guitar and you use different chords to match the rest of the band. This is called transposing. Sometimes, you can transpose on the fly in your head. Sometimes, it’s easier to sit down on paper and work out that if, for example, the chord is F# minor and you put the capo on fret 2, you can simply play an E minor. But that takes time. Let’s make an app that allows you to simply scroll through the fret positions to find the easiest chords to play. | ||
+ | |||
+ | 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' | ||
+ | |||
+ | 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. | ||
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 19: | 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.** |
- | 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. | + | 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. | ||
root = GridLayout(orientation=' | root = GridLayout(orientation=' | ||
Ligne 39: | Ligne 65: | ||
----------------------------- | ----------------------------- | ||
(2) scrollview | (2) scrollview | ||
- | ----------------------------- | + | -----------------------------** |
- | The scrollview contains multiple items – in our case buttons. Next, we create the label which will be at the top of our application. | + | 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. | ||
lbl = Label(text=' | lbl = Label(text=' | ||
Ligne 47: | Ligne 87: | ||
size_hint=(None, | size_hint=(None, | ||
size=(480, | size=(480, | ||
- | padding=(10, | + | |
+ | |||
+ | 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, | ||
+ | | ||
- | 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: | + | **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.** |
- | Now we define our “main” button (next page, top right). This is a static reference for the scale. | + | 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. | ||
Again, most of this should be fairly clear. | Again, most of this should be fairly clear. | ||
Ligne 64: | 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. | ||
+ | |||
+ | s = GridLayout(cols=1, | ||
+ | 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 comes some harder-to-understand | + | Arrive maintenant du code plus difficile à comprendre. Nous créons un autre objet GridLayout |
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 | + | Maintenant |
- | Now we create the ScrollView, set its size, and add it to the root GridLayout. | + | **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 85: | 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 97: | 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.1347569952.txt.gz · Dernière modification : 2012/09/13 22:59 de fredphil91