We are writing our first application in python for s60

    imageIn the last topic , I tried to warm up your appetite, showing what python for s60 is capable of. Now I'll show you how easy it is to write applications. We will go all the way, from choosing and installing the necessary software, to packing the program in an sis package.

    I will make a reservation right away: in this article we will analyze an example that works on symbian 9.4 (smartphones with a touchscreen), although you can easily remake the example for earlier versions of symbian too. In addition, the story will be kept in view of the work on windows. Users of other operating systems can easily select the necessary software for their OS.

    By the way, the program that we will 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 python PC, version 2.5-2.6. He needs to ensymble utility work (which will be discussed below), to test the outline code, and so far the programmer in Python without it :)

    Next, download here "development package" for your OS. For windows, it will be PythonForS60_1.9.7_Setup.exe. Install, and see what we got.

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

    We open the folder and install from it into our smart python 1.9.7 (this is python runtime, necessary for the work of programs written in python, it must also be distributed with your future program), we also install the python script shell, which is the shell to run your scripts directly on smart (launch chrome, you can use the interactive python console both from smart and via bluetooth, via hyper terminal for example).

    Installed ?! Now it remains to decide the question of what to write and how to run what is written.

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

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

    You probably have Nokia PC Suite installed. This program has a “file manager” that is embedded in the explorer. But for me, the person who hates the Windows Explorer, it’s easier to use the wonderful TotalCommander plugin called NokiaFS (I like other FMs, I think they can find similar plugins on the Internet).

    Establish a connection between Nokia PC Suite and your phone. It is most convenient to use bluetooth, but it is also suitable through a cable. The algorithm is simple: we 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 the script shell installed), then in the script shell click the menu and “run script” ... I have hands accustomed 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 application that will make our phone moo when you click on the button in the center of the screen. And to our application favorably differs from similar, add the ability to count the number of times our phone said "moo" as well as the opportunity to share our achievements, adding posts to twitter our program twitter.com/pys60_cow :)

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

    Let's write the “skeleton” of the application. 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] this is the first letter
    7. # drive on which we are working now
    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.  
    12. class Main:
    13.  
    14.  
    15.     def __init __ (self):
    16.         # turn off the on-screen keyboard
    17.         app.directional_pad = False
    18.         # open the jeep pie with the background image
    19.         self.splash = Image.open (app_path + 'splash.jpg')
    20.         # initialize the canvas, on which we will continue to work
    21.         # in redraw_callback specify the function that will call when the screen is redrawn.
    22.         self.canvas = Canvas (redraw_callback = self.redraw)
    23.         # indicate what is visible on the screen we will have canvas
    24.         app.body = self.canvas
    25.  
    26.     def redraw (self, rect):
    27.         # put our picture on canvas
    28.         self.canvas.blit (self.splash)
    29.  
    30. # create a lock object that is needed so that your application does not close immediately after opening        
    31. lock = e32.Ao_lock ()
    32. # when you press the right soft key, release the lock, the application will close.
    33. app.exit_key_handler = lock.signal
    34. a = Main ()
    35. lock.wait ()


    Be sure to draw a picture for the program. I am not an artist, for the time being this amusement is mine.

    image

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

    Now we write the say_mu function. I want to draw attention to the fact that the function is reported as a tuple, which contains the coordinates where we poked with our finger, but we do not need them yet. And 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 drop the cow.mp3 file and import the new modules that we now need.
    import audio
    from key_codes import * # for EButton1Down constant

    Now add the “counter mu”. We create the mu.txt file, write the number 0 into it, and move it to our working directory.
    In __init__ we add the 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 on when building in sis I'll tell you why so done. In the say_mu function, add the lines:

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

    After all, we need to increase the moo counter and save it to a file.
    It remains to display the inscription on the canvas and redraw it forcibly after each mooing. And finally, we get the following 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.  
    9. app_path = app.full_name () [0] + u ': \\ data \\ python \\'
    10. data_path = app_path
    11.  
    12. class Main:
    13.  
    14.     def __init __ (self):
    15.         self.mu_count = int (open (data_path + 'mu.txt'). read ())
    16.         app.directional_pad = False
    17.         self.splash = Image.open (app_path + 'splash.jpg')
    18.         self.canvas = Canvas (redraw_callback = self.redraw)
    19.         app.body = self.canvas
    20.         self.canvas.bind (EButton1Down, self.say_mu, ((44,171), (300,400)))
    21.         self.mu = audio.Sound.open (app_path + 'cow.mp3')
    22.  
    23.     def redraw (self, rect = None):
    24.         self.canvas.blit (self.splash)
    25.         self.canvas.text ((30,465), text = u 'Missed% s times.'% (self.mu_count), fill = 0xffffff)
    26.  
    27.     def say_mu (self, event):
    28.         if self.mu.state () == audio.EPlaying:
    29.             self.mu.stop ()
    30.         self.mu.play ()
    31.         self.mu_count + = 1
    32.         open (data_path + 'mu.txt', 'w'). write (str (self.mu_count))
    33.         self.redraw ()
    34.  
    35.  
    36. lock = e32.Ao_lock ()
    37. app.exit_key_handler = lock.signal
    38. a = Main ()
    39. lock.wait ()


    Only a little remained, post at the request of the user on twitter :)
    For this we need a tweepy library (you can use another one, but I liked this one 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 in a zip archive and imported directly from there, which actually we will do now. I download the prepared archive (tweepy has a dependency on simplejson), put it in our working directory and add the lines to the script:
    sys.path.append (app_path + 'my_modules.zip')
    import tweepy

    Implementing a tweet will only take 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'User% s cow mumbled% s times! '% (name, self.mu_count))
        note (u'Tweet added!')


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

    app.menu = [(u'Post a tweet ', self.post_to_twitter)]

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

    Packing a python script in sis


    It's time to use the ensymble utility that comes with Python for s60. We launch this miracle utility.

    image

    Create a folder 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 the location of 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 disk on which we will install the application, and UID is the uid of the application. This folder is read only, therefore, application data should be placed in another folder, for example, in x: / data / PyS60Cow /, therefore we entered two variables app_path and data_path into our program. Now they will need to be changed to those that we will use in reality.

    First of all, we will 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 start packing by the Create button and we see a window with a lot of warnings:

    image

    And here it is our generated UID. We 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, click in ensymble more, and in the settings list, in the uid field, enter 0xeecc323a. Also, we immediately set the version of the application in the X.YY.ZZ format.
    Now we will deal with the data / PyS60Cow folder, which should be installed on the smartphone and contain the mu.txt file. In c: / temp / PyS60Cow we create any folder, for example other, in it a directory with the structure that should be on the smartphone. In our case, these are subfolders of data / PyS60Cow and copy the mu.txt file there. In ensymble, in the Additional Options setting, specify the option --extrasdir = other

    Now it's time for the icon. Application icon for sis application must be in svg format. You can take my ready-made icon for this application. Next, add the option --icon = path_to_icon for example --icon = c: \ temp \ icon.svg in Additional Options, click on the

    coveted Create button, install the resulting sis package in our smartphone and try to run it and see an error about the lack of the cgi module. It doesn’t matter, just ensymble “forgot” to put this module in the sis package. Add it manually by adding in additional options --extra-modules = cgi. Putting it again. Everything is now our application is ready, and pleases our inquisitive minds :)

    You can download the finished sis file from here , and daddy, ready for packaging, containing a script, media files, etc., take here

    Also popular now: