Custom Search
Showing posts with label PyS60. Show all posts
Showing posts with label PyS60. Show all posts

Sunday, January 3, 2010

PyS60 Tutorial - Setting the application's orientation

PyS60 Tutorial - Setting the application's orientation

------------------------------------

#Setting the application's orientation

#From S60 3rd edition onwards you can set the orientation of the screen

/************ Complete Source Code Start *************/


import appuifw, e32

#Define the exit function
def quit():
app_lock.signal()


appuifw.app.exit_key_handler=quit

appuifw.app.orientation='landscape'
#or, after waiting 5 seconds
e32.ao_sleep(5)
appuifw.app.orientation='portrait'

app_lock=e32.Ao_lock()
app_lock.wait()

/************ Complete Source Code End *************/

PyS60 Tutorial - Setting the application's screen size

PyS60 Tutorial - Setting the application's screen size

------------------------------------

#Setting the application's screen size

#The available screen sizes are Normal, Large (meaning that only the softkeys pane is visible) and Full

/************ Complete Source Code Start *************/


import appuifw, e32

#Define the exit function
def quit():
app_lock.signal()


appuifw.app.exit_key_handler=quit

#For normal:
appuifw.app.screen="normal"
e32.ao_sleep(5)

#For large:
appuifw.app.screen="large"
e32.ao_sleep(5)

#For full:
appuifw.app.screen="full"

app_lock=e32.Ao_lock()
app_lock.wait()

/************ Complete Source Code End *************/

PyS60 Tutorial - Content Queries

PyS60 Tutorial - Content Queries

------------------------------------

# using this you can store the information entered by the user in a variable.


/*********** Description Start *************/


import appuifw


#asks for a number, in this case integer; for a fractional number use "float"
inf=appuifw.query(u"Enter a number", "number")

#asks for a text
inf=appuifw.query(u"Enter a word", "text")

#asks for a password, and shows the characters as "*" for protection
inf=appuifw.query(u"Enter a password", "code")

#asks for a time in hh:mm format
inf=appuifw.query(u"Enter a time", "time")

#displays a question, the returned values being 1 if the user has selected "Yes" and 0 if the user has selected "No"
inf=appuifw.query(u"Would you like to play again?", "query")

#asks for two fields of information
inf=appuifw.multi_query(u"Part1", u"Part2")

/*********** Description End *************/

/*********** Example 1 Start *************/

import appuifw,e32

def quit():

app_lock.signal()

appuifw.app.exit_key_handler=quit

# Declaring an empty tuple ‘data’

data={}

data['name'] = appuifw.query(u"Enter Name", "text")
data['age'] = appuifw.query(u"Enter age", "number")

data['username'] = appuifw.query(u"Enter UserName", "text")

data['password'] = appuifw.query(u"Enter password", "code")

data['time'] = appuifw.query(u"Enter time", "time")

data['agree'] = appuifw.query(u"Are you agree our terms and conditions?", "query")

if(data['agree']==1):

for key in data:

print key + " : " , data[key]

print "Registration Success"

else:

print "Registration failed"

print "Please agree to continue"

app_lock=e32.Ao_lock()

app_lock.wait()

OUTPUT

----------

name : sanju

age : 22

username : sanju123

password : 123456

time : 12:30

agree : 1

Registration Success

/*********** Example 1 End *************/

PyS60 Tutorial - Content handler

PyS60 Tutorial - Content handler

------------------------------------


# For Open any supported file type with its designated application


/************ Complete Source Code End *************/


import appuifw

a=appuifw.Content_handler()


a.open("C:\\movie.mpg")


#This opens the video file with RealPlayer, or whatever the default player is

/************ Complete Source Code End *************/

PyS60 Tutorial - Canvas

PyS60 Tutorial - Canvas

------------------------------------

#Canvas

#The application's body can be set to Canvas, which is used mainly for displaying images

/************ Complete Source Code Start *************/

import appuifw, graphics, e32

def quit():

app_lock.signal()

appuifw.app.exit_key_handler=quit

def handle_redraw(rect):

c.blit(myimage)

myimage=graphics.Image.open("C:\\myimage.jpg")

c = appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw)

appuifw.app.body=c

app_lock=e32.Ao_lock()

app_lock.wait()

/************ Complete Source Code End *************/

/*********** Description Start *************/

import appuifw, graphics, e32

#Define the exit function

def quit():

app_lock.signal()

appuifw.app.exit_key_handler=quit

#We define a method that will place the image 'myimage' on the canvas:

def handle_redraw(rect):

c.blit(myimage)

# We can make a blank one, specifying its size in pixels

#myimage=graphics.Image.new((240,320))

#We open myimage

myimage=graphics.Image.open("C:\\myimage.jpg")

#This sets ‘c’ as a canvas which can be redrawn and can be modified if certain events (in this case key presses) occur if you make a method for that

c = appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw)

# Setting default body of the application to ‘c’.

appuifw.app.body=c

#c.text((20,30), u"something", font="title")

#Here the text is displayed at the specified coordinates on the canvas, with the title font, if the 1 button is pressed

#Other font types are normal, default, dense, symbol, annotation, and legend. You can also use other formats, as is explained below

#As you've seen above, you can write text on a canvas.

#An additional way of specifying the parameters is:

#c.text((2,24), u'Hello', 0x008000, font=(u'Nokia Hindi S60',48,appuifw.STYLE_BOLD))

#Here 0x008000 is the color of the text, 48 is its size and appuifw.STYLE_BOLD is its style

#The canvas can also be cleared (and optionally filled with a color)

#c.clear()

#c.clear(0x008000) fills it with green

app_lock=e32.Ao_lock()

app_lock.wait()

/*********** Description End *************/

/*********** Note Start *************/

---------------------------------------------------------

Canvas is a UI control that provides a drawable area on the screen and support for handling raw key events .

Canvas([redraw_callback=None, event_callback=None])

Constructs a Canvas object. The optional parameters are callbacks or functions or callable objects that are called when specific events occur. redraw_callback is called whenever a part of the Canvas has been obscured by something, is then revealed, and needs to be redrawn. This can typically happen, for example, when the user switches away from the Python application and back again, or after displaying a pop-up menu. The callback takes as its argument a four-element tuple that contains the top-left and the bottom-right corner of the area that needs to be redrawn. In many cases redrawing the whole Canvas is a reasonable option.

The event_callback is called whenever a raw key event is received. There are three kinds of key events: EEventKeyDown, EEventKey, and EEventKeyUp. When a user presses a key down, events EEventKeyDown and EEventKey are generated. When the key is released, an EEventKeyUp event is generated.

The argument to the event_callback is a dictionary that contains the following data for key events:

# 'type': one of EEventKeyDown, EEventKey, or EEventKeyUp

# 'keycode': the keycode of the key

# 'scancode': the scancode of the key

# 'modifiers': the modifiers that apply to this key event

------------------------------------------------------------

Image Image.open(filename)

Returns a new Image object (mode RGB16) that contains the contents of the named file. The supported file formats are JPEG and PNG. The file format is automatically detected based on file contents. filename should be a full path name.

------------------------------------------------------------

blit(image[,target=(0,0),source=((0,0),image.size),mask=None,scale=0])

Copies the source area from the given image to the target area in this drawable. The source area is copied in its entirety if mask is not given or is set to None. If the mask is given, the source area is copied where the mask is white. mask can be either None or a 1-bit (black and white) image and must be of the same size as the source image.

target and source specify the target area in this image and the source area in the given source. They are coordinate sequences of one or two coordinates. If they specify one coordinate, it is interpreted as the upper-left corner for the area; if they specify two coordinates, they are interpreted as the top-left and bottom-right corners of the area.

If scale is other than zero, scaling is performed on the fly while copying the source area to the target area. If scale is zero, no scaling is performed, and the size of the copied area is clipped to the smaller of source and target areas.

Note that a blit operation with scaling is slower than one without scaling. If you need to blit the same Image many times in a scaled form, consider making a temporary Image of the scaling result and blitting it without scaling. Note also that the scaling performed by the blit operation is much faster but of worse quality than the one done by the resize method, since the blit method does not perform any antialiasing.

------------------------------------------------------------

/*********** Note End *************/

PyS60 Tutorial - Forms

PyS60 Tutorial - Forms

/************ Complete Source Code Start *************/

import appuifw, e32

#Define the exit function

def quit():

app_lock.signal()

appuifw.app.exit_key_handler=quit

#Create a list to be used in 'combo' selection mode

model=[u'6630', u'E90', u'7610', u'N95', u'N73']

#Define the field list (consists of tuples: (label, type ,value)); label is a unicode string

#Type is one of the following strings: 'text', 'number', 'date', 'time',or 'combo'

data=[(u'Mobile','text', u'Nokia'),(u'Model','combo', (model,0)),(u'Amount','number', 5),(u'Date','date'),(u'Time','time')]

#Set the view/edit mode of the form

flags=appuifw.FFormEditModeOnly

#Create an instance of the form

f=appuifw.Form(data, flags)

#Make the form visible on the UI

f.execute()

app_lock=e32.Ao_lock()

app_lock.wait()

/************ Complete Source Code End *************/

/*********** Description Start *************/

import appuifw, e32

#Define the exit function

def quit():

app_lock.signal()

appuifw.app.exit_key_handler=quit

# Create a list to be used in 'combo' selection mode

# Create a list 'model' to used in 'combo' as values

model=[u'6630', u'E90', u'7610', u'N95', u'N73']

# Define the field list 'data' (consists of tuples: (label, type ,value)); label is a unicode string

# Type is one of the following strings: 'text', 'number', 'date', 'time',or 'combo', value is the value

# to be displayed in the corresponding field,

# 'text' -------> text field

# 'number' -------> number field

# 'date' -------> date field

# 'time' -------> time field

# 'combo' -------> combo box

data=[(u'Mobile','text', u'Nokia'),(u'Model','combo', (model,0)),(u'Amount','number', 5),(u'Date','date'),(u'Time','time')]

# Set the view/edit mode of the form

# Here setting the attribute 'flags' to Ediit mode only for Form type object.

# We can use any name for 'flags'. 'flags' is a default argument to Form, see syntax of form.

# This flag is defined in the module appuifw, So we can directly access it using module name.

flags=appuifw.FFormEditModeOnly

# Create an instance or object of the Form Type

# Passing field list 'data' and flag 'flags' as argument

f = appuifw.Form(data, flags)

# Make the form (form object) visible on the UI

f.execute()

app_lock=e32.Ao_lock()

app_lock.wait()

/*********** Description End *************/

/*********** Note Start *************/

----------------------------------------------------------

Form implements a dynamically configurable, editable multi-field dialog. Form caters for advanced dialog use cases with

requirements such as free selectability of the combination of fields, possibility of validating the user input, and automatically

producing the contents of some dialog fields before allowing the closing of the dialog.

Form([fields=field_list, flags=flag])

For creates a Form instance (object).

# field_list consists of tuples: (label, type [,value]), where

# label is a Unicode string

# type is one of the following strings: 'text', 'number', 'date', 'time',or 'combo'

# value, depending on type: Unicode string, numeric, float (seconds since Unix epoch rounded down to the nearest local midnight),

float (seconds since local midnight), or ([unicode_string choices], index)

Form can also be configured and populated after construction. The configuration flags are visible as an attribute. Form implements

the list protocol that can be used for setting the form fields, as well as obtaining their values after the dialog has been executed.

----------------------------------------------------------

Some of the attributes of the Form instance or object

This 'flags' attribute of Form Type object holds the values of the various configuration flags. Currently supported flags are:

FFormEditModeOnly

When this flag is set, the form remains in edit mode while execute runs.

This flag is defined in the module appuifw, So we can directly access it using module name.

----------------------------------------------------------

/*********** Note End *************/

PyS60 Tutorial - Menus, Application's menu, Popup menu, Simple selection, list Multiselection list

PyS60 Tutorial - Menus, Application's menu, Popup menu, Simple selection, list Multiselection list

Menus

----------

Application's menu

Popup menu

Simple selection list

Multiselection list

# There are several kinds of menus in PyS60:

# The application's menu, triggered by pressing the left softkey

# A popup menu somewhat similar in aspect to the application's menu

# Selection lists, which can be simple or multiple and have the ability to search for items; useful if you have many items to put in the list

/************ Complete Source Code Start *************/

import appuifw, e32

def quit():

app_lock.signal()

appuifw.app.exit_key_handler=quit

def callback_A():print "A"

def callback_B():print "B"

def callback_C():print "C"

def callback_D():print "D"

#The application's menu

appuifw.app.menu=[(u"Item_A", callback_A), (u"Item_B", ((u"Item_C", callback_C), (u"Item_D", callback_D)))]

#Popup menu

i = appuifw.popup_menu([u"Item1", u"Item2"])

#Simple selection list

i =appuifw.selection_list([u"Item1", u"Item2"])

#Multiselection list

i = appuifw.multi_selection_list([u"Item1", u"Item2"], style='checkbox', search_field=1)

app_lock=e32.Ao_lock()

app_lock.wait()

/************ Complete Source Code End *************/

/*********** Description Start *************/

import appuifw, e32

# Define the exit function

def quit():

app_lock.signal()

appuifw.app.exit_key_handler=quit

# Defining fumctions callback_A, callback_B, callback_C and callback_D.

def callback_A():print "A"

def callback_B():print "B"

def callback_C():print "C"

def callback_D():print "D"

# The application's menu.

# Here we can select Item_A, or select Item_B in which case a list with Item_C and Item_D is shown.

# Here menu "Item_B" has submenus Item_C and Item_D.

# When we selecting the menu "Item_A" it call the callable object or function "callback_A()"

# When we selecting the menu "Item_B" it shows the menu Item_C and Item_D.

# Setting attribute "menu" of the "Application" type object "app".

appuifw.app.menu=[(u"Item_A", callback_A), (u"Item_B", ((u"Item_C", callback_C), (u"Item_D", callback_D)))]

# Popup menu

# Stores the index of the selected option in the variable i ; 0 for Item1, 1 for Item2 etc.

# calling the free function "popup_menu" of "appuifw" module.

i = appuifw.popup_menu([u"Item1", u"Item2"])

# Simple selection list

# Stores the index of the selected option in the variable i ; 0 for Item1, 1 for Item2 etc.

# calling the free function "selection_list" of "appuifw" module.

i=appuifw.selection_list([u"Item1", u"Item2"])

# Multiselection list

# Shows a list from which you can select multiple items, selected ones being marked; here you can also enable a search field.

# Returns a tuple of indexes (a pair of Unicode strings) of the chosenitems, or None if the selection is cancelled by the users.

# calling the free function "multi_selection_list" of "appuifw" module.

i=appuifw.multi_selection_list([u"Item1", u"Item2"], style='checkbox', search_field=1)

app_lock=e32.Ao_lock()

app_lock.wait()

/*********** Description End *************/

/*********** Note Start *************/

---------------------------------------------------

Menu can be:

# a list of (title, callback) pairs, where each pair describes an item in the application’s menu bar, or

# a list of (title, ((title, callback),...)) pairs, where the tuple of Unicode strings ((title, callback),...) creates a submenu.

title (Unicode) is the name of the item and callback the associated callable object(similar to function). The maximum

allowed number of items in a menu, or items in a submenu, or submenus in a menu is 30.

Example:

appuifw.app.menu = [(u"item 1", item1),

(u"Submenu 1", ((u"sub item 1", subitem1),

(u"sub item 2", subitem2) ))

]

----------------------------------------------------------

index popup_menu(list [, label])

A pop-up menu style dialog. list representing the menu contents can be a list of Unicode strings or a list of Unicode string

pairs (tuples). The resulting dialog list is then a single-style or a double-style list. A single-style list is shown in full; whereas

a double-style list shows the items one at a time. Returns None if the users cancel the operation. Return index of chosen menu.

----------------------------------------------------------

index selection_list(choices=list [, search_field=0])

Executes a dialog that allows the users to select a list item and returns the index of the chosen item, or None

if the selection is cancelled by the users. (choices=list [, search_field=0]) consists of keywords and values, where

# choices is a list of Unicode strings.

# search_field is 0 (disabled) by default and is optional. Setting it to 1 enables a search field (find pane) that facilitates

searching for items in long lists. If enabled, the search field appears after you press a letter key.

----------------------------------------------------------

(indexes) multi_selection_list(choices=list [, style='checkbox', search_field=0])

Executes a dialog that allows the users to select multiple list items. Returns a tuple of indexes (a pair of Unicode strings) of the chosen

items, or None if the selection is cancelled by the users. (choices=list [, style='checkmark', search_field=0])

consists of keywords and values, where

# choices is a list of Unicode strings.

# style is an optional string; the default value being 'checkbox'. If 'checkbox' is given, the list will be a checkbox list, where

empty checkboxes indicate what items can be marked. The other possible value that can be set for style is 'checkmark'.

If 'checkmark' is given, the list will be a markable list, which lists items but does not indicate specifically that items can be selected.

To select items on a markable list, use the Navigation key to browse the list and the Edit key to select an item. For example views on

checkbox and markable lists.

# search_field is 0 (disabled) by default and is optional. Setting it to 1 enables a search field (find pane) that facilitates searching

for items in long lists. If enabled, the search field is always visible with checkbox lists; with markable lists it appears by pressing a letter key.

Example:

tuple = appuifw.multi_selection_list(L, style='checkmark', search_field=1)

----------------------------------------------------------

Here popup_menu, selection_list, multi_selection_list is a free functions functions that do not belong

to any class − are defined in the appuifw module.So we can directly call these functions using module name "appuifw".

----------------------------------------------------------

/*********** Note End *************/