Custom Search

Sunday, January 3, 2010

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 *************/

1 comment: