issue62:tutopython
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue62:tutopython [2012/07/03 06:55] – créée fredphil91 | issue62:tutopython [2012/08/30 15:28] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | This time, we’ll finish up using SL4A. We’ll make a larger program and then send it to the virtual machine via ADB. | + | **This time, we’ll finish up using SL4A. We’ll make a larger program and then send it to the virtual machine via ADB. |
Let’s deal with our code first. In this, we’ll simply be trying out some of the “widgets” that are available to us when using SL4A. Start on your desktop using your favorite editor. | Let’s deal with our code first. In this, we’ll simply be trying out some of the “widgets” that are available to us when using SL4A. Start on your desktop using your favorite editor. | ||
Ligne 5: | Ligne 5: | ||
Enter the code shown top right and save it (but don’t try to run it) as “atest.py”. | Enter the code shown top right and save it (but don’t try to run it) as “atest.py”. | ||
- | The first line imports the android library. We create an instance of it in the second line. Line 3 creates and displays a dialog box with the title “Hello”, | + | The first line imports the android library. We create an instance of it in the second line. Line 3 creates and displays a dialog box with the title “Hello”, |
- | Save your code as atest1.py. We’ll be sending this to our virtual machine after we discuss what it does. | + | Cette fois-ci, nous allons en terminer avec l' |
+ | |||
+ | Occupons-nous d' | ||
+ | |||
+ | Entrez le code que vous voyez en haut à droite et sauvegardez-le (mais n' | ||
+ | |||
+ | La première ligne importe la bibliothèque android. Nous en créons une instance dans la deuxième ligne. La ligne 3 crée et affiche une boîte de dialogue avec le titre « Bonjour », | ||
+ | |||
+ | **Save your code as atest1.py. We’ll be sending this to our virtual machine after we discuss what it does. | ||
Take a look at the first four lines we just entered. We create an alert type dialog asking “Would you like to play a game?”. In the case of an alert type dialog, there’s no text box to enter anything. The next two lines say to create two buttons, one with the text “Yes”, which is a “positive” button, and one with the text “No”, a “negative” button. The positive and negative buttons refer to the response returned - either “positive” or “negative”. The next line then shows the dialog. The next seven lines wait for a response from the user. | Take a look at the first four lines we just entered. We create an alert type dialog asking “Would you like to play a game?”. In the case of an alert type dialog, there’s no text box to enter anything. The next two lines say to create two buttons, one with the text “Yes”, which is a “positive” button, and one with the text “No”, a “negative” button. The positive and negative buttons refer to the response returned - either “positive” or “negative”. The next line then shows the dialog. The next seven lines wait for a response from the user. | ||
We create a simple loop (while True:) then wait for a response for up to 10 seconds by using the droid.eventWait(value) call. The response (either “positive” or “negative”) will be returned in - you guessed it - the response variable. If response has the name of “dialog”, | We create a simple loop (while True:) then wait for a response for up to 10 seconds by using the droid.eventWait(value) call. The response (either “positive” or “negative”) will be returned in - you guessed it - the response variable. If response has the name of “dialog”, | ||
+ | |||
+ | {u’data’: | ||
+ | |||
+ | Enregistrez votre code sous « atest1.py ». Nous allons envoyer cela à notre machine virtuelle après avoir discuté de ce qu'il fait. | ||
+ | |||
+ | Jetez un coup d'œil aux quatre premières lignes que nous venons de saisir. Nous créons une boîte de dialogue d' | ||
+ | |||
+ | Nous créons une simple boucle (while True:), puis attendons une réponse pendant 10 secondes en utilisant la fonction droid.eventWait(valeur). La réponse (soit positive soit négative) sera retournée dans - vous l' | ||
{u’data’: | {u’data’: | ||
- | You can see that the value is passed in the ‘data’ dictionary, the dialog key is in the ‘name’ dictionary, and there is a ‘time’ value that we don’t care about here. | + | **You can see that the value is passed in the ‘data’ dictionary, the dialog key is in the ‘name’ dictionary, and there is a ‘time’ value that we don’t care about here. |
Finally we dismiss the dialog box. | Finally we dismiss the dialog box. | ||
Ligne 22: | Ligne 38: | ||
Now, let’s push it to our virtual machine. Open a terminal window and change to the folder you saved the code in. Assuming you have set your path to include the SDK, type | Now, let’s push it to our virtual machine. Open a terminal window and change to the folder you saved the code in. Assuming you have set your path to include the SDK, type | ||
+ | |||
+ | adb devices** | ||
+ | |||
+ | Vous pouvez voir que la valeur est passée dans le tableau « data », le texte « dialog » dans le tableau « name » et il y a une valeur « time » dont nous ne nous soucions pas ici. | ||
+ | |||
+ | Enfin, nous fermons la boîte de dialogue. | ||
+ | |||
+ | Avant que nous puissions envoyer notre code à la machine virtuelle, il faut la faire démarrer. Exécutez votre émulateur Android. Une fois qu'il a démarré, vous remarquerez que la barre de titre contient quatre chiffres au début du titre. Il s'agit du port sur lequel la machine est à l' | ||
+ | |||
+ | Maintenant, nous allons envoyer le code vers notre machine virtuelle. Ouvrez une fenêtre de terminal et allez dans le dossier où vous avez enregistré le code. En supposant que vous avez configuré votre chemin par défaut pour y inclure le SDK, saisissez : | ||
adb devices | adb devices | ||
- | This asks adb to show any devices that are connected. This can include not only the Android emulator but also any smartphones, | + | **This asks adb to show any devices that are connected. This can include not only the Android emulator but also any smartphones, |
List of devices attached | List of devices attached | ||
Ligne 35: | Ligne 61: | ||
So, in my case it would be... | So, in my case it would be... | ||
+ | |||
+ | adb push atest1.py / | ||
+ | |||
+ | Ceci demande à adb de montrer tous les périphériques qui sont connectés. Cela peut inclure non seulement l' | ||
+ | |||
+ | Liste des périphériques connectés | ||
+ | emulator-5554 | ||
+ | |||
+ | Maintenant que nous sommes sûrs que notre appareil est connecté, nous voulons envoyer l' | ||
+ | |||
+ | adb push fichier_source chemin_et_fichier_destination | ||
+ | |||
+ | Donc, dans mon cas, ce serait : | ||
adb push atest1.py / | adb push atest1.py / | ||
- | If everything works correctly, you’ll get a rather disappointing message similar to this... | + | **If everything works correctly, you’ll get a rather disappointing message similar to this... |
11 KB/s (570 bytes in 0.046s) | 11 KB/s (570 bytes in 0.046s) | ||
Ligne 44: | Ligne 83: | ||
Now, on the Android emulator, start SL4A. You should see all of the python scripts, and, in there you should see atest1.py. Tap (click) on ‘atest1.py’, | Now, on the Android emulator, start SL4A. You should see all of the python scripts, and, in there you should see atest1.py. Tap (click) on ‘atest1.py’, | ||
- | You’ll see the first dialog asking for your name. Enter something in the box and tap (click) the ‘Ok’ button. You’ll see the hello message. Next, you’ll see the alert dialog. Tap (click) on either button to dismiss the dialog. We aren’t looking at the responses yet so it doesn’t matter which one you choose. Now we’ll add some more code (top right). | + | You’ll see the first dialog asking for your name. Enter something in the box and tap (click) the ‘Ok’ button. You’ll see the hello message. Next, you’ll see the alert dialog. Tap (click) on either button to dismiss the dialog. We aren’t looking at the responses yet so it doesn’t matter which one you choose. Now we’ll add some more code (top right).** |
- | I’m sure you can figure out that this set of code simply checks the response, and, if it’s ‘None’ due to a timeout, we simply print “Timed out.” And, if it’s actually something we want, then we assign the data to the variable rdialog. Now add the next bit of code (below)... | + | Si tout fonctionne correctement, |
+ | |||
+ | 11 Ko/s (570 octets en 0.046s) | ||
+ | |||
+ | Maintenant, sur l' | ||
+ | |||
+ | Vous verrez la première boîte de dialogue vous demandant votre nom. Entrez quelque chose dans la boîte et tapez (cliquez) sur le bouton « Ok ». Vous verrez le message bonjour. Ensuite, vous verrez la boîte de dialogue d' | ||
+ | |||
+ | **I’m sure you can figure out that this set of code simply checks the response, and, if it’s ‘None’ due to a timeout, we simply print “Timed out.” And, if it’s actually something we want, then we assign the data to the variable rdialog. Now add the next bit of code (below)... | ||
This part of the code will look at the data passed back by the button-press event. We check to see if the response has a “which” key, and, if so, it’s a legitimate button press for us. We then check to see if the result is a “positive” (‘Ok’ button) response. If so, we’ll create another alert dialog, but this time, we will add a list of items for the user to choose from. In this case, we offer the user to select from a list including Checkers, Chess, Hangman, and Thermal Nuclear War, and we assign the values 0 to 3 to each item. (Is this starting to seem familiar? Yes, it’s from a movie.) We then display the dialog and wait for a response. The part of the response we are interested in is in the form of a dictionary. Assuming the user tapped (clicked) on Chess, the resulting response comes back like this... | This part of the code will look at the data passed back by the button-press event. We check to see if the response has a “which” key, and, if so, it’s a legitimate button press for us. We then check to see if the result is a “positive” (‘Ok’ button) response. If so, we’ll create another alert dialog, but this time, we will add a list of items for the user to choose from. In this case, we offer the user to select from a list including Checkers, Chess, Hangman, and Thermal Nuclear War, and we assign the values 0 to 3 to each item. (Is this starting to seem familiar? Yes, it’s from a movie.) We then display the dialog and wait for a response. The part of the response we are interested in is in the form of a dictionary. Assuming the user tapped (clicked) on Chess, the resulting response comes back like this... | ||
+ | |||
+ | Result(id=12, | ||
+ | |||
+ | Je suis sûr que vous comprendrez que ce morceau de code vérifie simplement la réponse et, si c'est « Aucune » à cause du temps d' | ||
+ | |||
+ | Ce morceau de code regardera les données transmises par l' | ||
Result(id=12, | Result(id=12, | ||
- | In this case, we are really interested in the result portion of the returned data. The selection is #1 and is held in the ‘item’ key. Here’s the next part of the code (above right)... | + | **In this case, we are really interested in the result portion of the returned data. The selection is #1 and is held in the ‘item’ key. Here’s the next part of the code (above right)... |
Here we check to see if the response has the key “item”, and, if so, assign it to the variable “sel”. Now we use an if/ | Here we check to see if the response has the key “item”, and, if so, assign it to the variable “sel”. Now we use an if/ | ||
Ligne 58: | Ligne 111: | ||
As you can see, we simply respond to the other types of button-presses here. | As you can see, we simply respond to the other types of button-presses here. | ||
- | Save, push, and run the program. | + | Save, push, and run the program.** |
- | As you can see, SL4A gives you the ability to make “GUIfied” applications, | + | Dans ce cas, nous sommes vraiment intéressés par la partie « result » des données renvoyées. La sélection est le n° 1 et est contenue dans la clé « item ». Voici la partie suivante du code (en haut à droite) : |
+ | |||
+ | Ici, nous vérifions pour voir si la réponse contient la clé « item » et, si c'est le cas, nous l' | ||
+ | |||
+ | Comme vous pouvez le voir, nous répondons simplement aux autres appuis de boutons ici. | ||
+ | |||
+ | Sauvegardez, | ||
+ | |||
+ | **As you can see, SL4A gives you the ability to make “GUIfied” applications, | ||
By the way, you can push directly to your smartphone or tablet in the same way. | By the way, you can push directly to your smartphone or tablet in the same way. | ||
Ligne 66: | Ligne 127: | ||
As usual, the code has been put up on pastebin at http:// | As usual, the code has been put up on pastebin at http:// | ||
- | See you next time. | + | See you next time.** |
+ | |||
+ | Comme vous pouvez le voir, SL4A vous donne la possibilité de faire des applications « graphiques », mais pas complètement. Cela ne devrait toutefois pas vous empêcher d' | ||
+ | |||
+ | Au fait, vous pouvez envoyer votre code directement sur votre smartphone ou votre tablette de la même manière. | ||
+ | |||
+ | Comme d' | ||
+ | |||
+ | Rendez-vous la prochaine fois. | ||
issue62/tutopython.1341291359.txt.gz · Dernière modification : 2012/07/03 06:55 de fredphil91