📜 ⬆️ ⬇️

We write our first application in python for s60

image In the past topic , I tried to warm up your appetite, showing what python for s60 is capable of. Now I will show you how easy it is to write applications. We will go all the way, from the selection and installation of the necessary software, to the packaging of the program in the sis package.

I’ll make a reservation right away: in this article we’ll look at the example working on symbian 9.4 (smartphones with touchscreen), although you can easily remake the example for earlier versions of symbian. In addition, the story will be based on the work on windows. Users of other operating systems, without problems, will be able to pick up the necessary software for their OS.

By the way, the program that we develop in this topic has a twitter account :)

Let's start ...
')

Tools


The choice of tools is based on my personal experience. Maybe you will go the other way :)

So, we will need:
Installed on your PC python, version 2.5-2.6. It is required for the work of the ensymble utility (which will be discussed below), for testing code sketches, and where can a python programmer without him :)

Next, download the “developer package” for your OS from here. For windows, this will be PythonForS60_1.9.7_Setup.exe. Install, and see what we got.

We click on the start menu, and we find Python for s60. There we will have the ensymble utility, a link to on-line documentation (off line docks can be downloaded from me from here ), a shortcut to the PyS60 Dependencies folder, which we will now need.

Open the folder, and install it into our smart python 1.9.7 (this is the python runtime required for the programs written in python, and you need to distribute it along with your future program), also install the python script shell, which is the shell to launch your scripts directly on a smart (Kromer launch, you can use the interactive python console either from smart or via bluetooth, through a hyper terminal for example).

Established? It now remains to decide the question of what to write and how to start writing.

Any text editor with utf-8 support is suitable for writing, preferably with code highlighting support. Which of the hundreds of editors to choose, you decide. Nothing special is required of him, we will not run through it anyway.

You can run scripts through an emulator, but I use a different method, running on the "live gland".

You probably installed Nokia PC Suite. This program has a "file manager", which is built into the explorer. But for me, a person who hates Windows Explorer, it’s easier to use the wonderful TotalCommander plugin, which is called NokiaFS (I love other FMs, I think they will be able to find similar plugins on the Internet).

Establish a connection between Nokia PC Suite and your phone. The most convenient way is to use bluetooth, but it will also work through a cable. The algorithm is simple: type the code, click save, switch TC, copy the script to the phone in the data / python folder (on the disk where you have python and script shell installed), then in the script shell click the menu and “run script” ... I hands used to shortcuts and function keys do it in 1-2 seconds :)

For convenience, you can delete examples from data / python, let's make this directory our working folder.

Let's write something


We will write a mega app that will make our phone moo when you press a button in the center of the screen. And so that our application compares favorably with similar ones, we will add the ability to calculate how many times our phone said “mu” as well as the opportunity to share our achievements by adding messages to our twitter twitter/pys60_cow program :)

I mean that you have at least some knowledge of the python programming language. If not, run to learn :) I cited the link to the documentation for symbian-specific modules above.

Let's write the application "skillet". This is how my applications usually start, in the UI of which we use Canvas:
  1. # - * - coding: utf-8 - * -
  2. import sys
  3. import e32
  4. from appuifw import *
  5. from graphics import *
  6. # app_path - the path where we will have pictures and sounds ... app.full_name () [0] is the first letter
  7. # disk on which we are working
  8. app_path = app. full_name ( ) [ 0 ] + u ': \\ data \\ python \\ '
  9. # the path where the file with the number of clicks will lie, later we will change this path
  10. data_path = app_path
  11. class Main:
  12. def __init__ ( self ) :
  13. # turn off the on-screen keyboard
  14. app. directional_pad = False
  15. # open the jpeg with background image
  16. self . splash = Image. open ( app_path + 'splash.jpg' )
  17. # initialize the canvas on which we will continue to work
  18. # in redraw_callback specify the function that will be called when the screen is redrawn.
  19. self . canvas = Canvas ( redraw_callback = self . redraw )
  20. # indicate what is visible on the screen we will have canvas
  21. app. body = self . canvas
  22. def redraw ( self , rect ) :
  23. # put on canvas our picture
  24. self . canvas . blit ( self . splash )
  25. # create a lock object, which is needed so that your application does not close immediately after opening
  26. lock = e32. Ao_lock ( )
  27. # when you press the right soft key, release the lock, the application closes.
  28. app. exit_key_handler = lock. signal
  29. a = Main ( )
  30. lock. wait ( )


Be sure to draw a picture to the program. I'm not an artist, for the test so far this is my fancy work.

image

Now it remains to revive our our program, adding the ability to our cow moo. Open the picture in the editor and measure the line where we have the picture.
Add the following lines to __init__:
# zabind on the coordinates where we have the cow icon say_mu function
self . canvas . bind ( EButton1Down, self . say_mu , ( ( 44 , 171 ) , ( 300 , 400 ) ) )
# open the sound of cow mooing
self . mu = audio. Sound . open ( app_path + 'cow.mp3' )

Now we write the function say_mu. I want to note that the functions are reported, a tuple, which contains the coordinates where we poked a finger, but we don’t need them yet. Finally, we write a function that will “moo”
def say_mu ( self , event ) :
# if the sound is already playing, stop it
if self . mu . state ( ) == audio. EPlaying :
self . mu . stop ( )
self . mu . play ( )

Do not forget to upload the cow.mp3 file and import the new modules that we need now.
import audio
from key_codes import * # for EButton1Down constant

Now add “mu counter”. Create the file mu.txt, write the number 0 into it, and move it to our working directory.
Add __init__ line:

self.mu_count = int (open (data_path + 'mu.txt'). read ())

The path to the mu.txt file is not accidentally located in the variable data_path a not in app_path, later when building in sis I will tell you why it was done this way. In the function say_mu, append the lines:

self.mu_count + = 1
open (data_path + 'mu.txt', 'w'). write (str (self.mu_count))

After all, we need to increase the lowing counter and save it to a file.
It remains to display on the canvas and redraw it forcibly after each lowing. As a result, we get this script:
  1. # - * - coding: utf-8 - * -
  2. import sys
  3. import e32
  4. from appuifw import *
  5. from graphics import *
  6. import audio
  7. from key_codes import *
  8. app_path = app. full_name ( ) [ 0 ] + u ': \\ data \\ python \\ '
  9. data_path = app_path
  10. class Main:
  11. def __init__ ( self ) :
  12. self . mu_count = int ( open ( data_path + 'mu.txt' ) . read ( ) )
  13. app. directional_pad = False
  14. self . splash = Image. open ( app_path + 'splash.jpg' )
  15. self . canvas = Canvas ( redraw_callback = self . redraw )
  16. app. body = self . canvas
  17. self . canvas . bind ( EButton1Down, self . say_mu , ( ( 44 , 171 ) , ( 300 , 400 ) ) )
  18. self . mu = audio. Sound . open ( app_path + 'cow.mp3' )
  19. def redraw ( self , rect = None ) :
  20. self . canvas . blit ( self . splash )
  21. self . canvas . text ( ( 30 , 465 ) , text = u 'Dribbled% s times.' % ( self . mu_count ) , fill = 0xffffff )
  22. def say_mu ( self , event ) :
  23. if self . mu . state ( ) == audio. EPlaying :
  24. self . mu . stop ( )
  25. self . mu . play ( )
  26. self . mu_count + = 1
  27. open ( data_path + 'mu.txt' , 'w' ) . write ( str ( self . mu_count ) )
  28. self . redraw ( )
  29. lock = e32. Ao_lock ( )
  30. app. exit_key_handler = lock. signal
  31. a = Main ( )
  32. lock. wait ( )


Just a little bit left, post at the user's request in twitter :)
To do this, we need the tweepy library (you can use another one, but this one I liked the most).

Add the ability to work with a third-party module is quite easy. You can unpack the package in some folder and add this path to sys.path. Also, these third-party modules can be packaged into a zip archive and imported directly from there, which we will actually do now. I download the prepared archive (tweepy has a dependency on simplejson), put it in our working directory and add lines to the script:
sys . path . append ( app_path + 'my_modules.zip' )
import tweepy

The implementation of adding a tweet takes only a couple of lines:
def post_to_twitter ( self ) :
name = query ( u 'Your name:' , 'text' )
if not name: name = u 'Anonymous'
api = tweepy. API . new ( 'basic' , 'pys60_cow' , 'pys60cow' )
api. update_status ( u 'At users% s, the cow has moaned% s times!' % ( name, self . mu_count ) )
note ( u 'Tweet added!' )


Do not forget to create a menu with one single item:

app.menu = [(u'Back Tweet ', self.post_to_twitter)]

Now our script is ready. And we proceed with the next part of our article.

Packing the python script in sis


It's time to use the ensymble utility, which comes bundled with Python for s60. We start this miracle utility.

image

Create a daddy somewhere for our project, for example c: / temp / Pys60Cow, and put the files cow.py, cow.mp3, splash.jpg, my_modules.zip there.
Rename cow.py to default.py

Now let's talk about placing files in the installed application.
The script itself, with the files it needs, such as graphics, etc., will fall into the x: / private / UID / folder, where x is the drive on which we will install the application, and UID is the uid of the application. This folder is read only, so these applications should be placed in another folder, for example, in x: / data / PyS60Cow /, so we drove two variables app_path and data_path into our program. Now they will need to be changed to those that will be used in reality.

First we generate a valid UID for the program. To do this, run ensymble, select the Script directory, click browse and select the folder c: / temp / Pys60Cow. We launch the packaging by the Create button and see a window with a lot of warnings:

image

And here it is our generated UID. Open default.py and edit app_path and data_path:

app_path = app.full_name () [0] + u ': \\ private \\ eecc323a \\'
data_path = app.full_name () [0] + u ': \\ data \\ Pys60Cow \\'

Accordingly, in ensymble click more, and in the list of settings, enter 0xeecc323a in the uid field. We also immediately specify the application version in the format X.YY.ZZ
Now let's deal with the data / PyS60Cow folder, which should be installed on the smartphone and contain the file mu.txt. In c: / temp / PyS60Cow we create any folder, for example other, in it directories with the structure that should be on the smartphone. In our case, these are subfolders of data / PyS60Cow and copy the file mu.txt there. In ensymble, in the Additional Options setting we set the option --extrasdir = other

Now it's time for the icons. Application icon for sis applications must be in svg format. You can take my finished icon to this application. Next we add in the Additional Options option --icon = path_to_icon for example --icon = c: \ temp \ icon.svg

Press the cherished Create button, install the resulting sis package into our smartphone and try to start and see an error about the lack of the cgi module. It does not matter, just the ensymble "forgot" to put this module in the sis package. Add it manually by adding in additional options - extra-modules = cgi. We collect again. Everything is now our application is ready, and pleases our inquiring minds :)

You can download the finished sis file from here , and the daddy, ready for packaging, which contains the script, media files, etc., is available here.

Source: https://habr.com/ru/post/73951/


All Articles