You Are Here:

Community: Wiki

This page was last modified on 4 September 2009, at 18:08.

How to use Canvas

From Forum Nokia Wiki

Reviewer Approved   


ID Creation date January 3, 2009
Platform S60 1st Edition, S60 2nd Edition, S60 3rd Edition Tested on devices Nokia N95, Nokia E90
Category Python Subcategory UI


Keywords (APIs, classes, methods, functions): appuifw, graphics


Overview

This article explains how to use the Canvas class from the appuifw module in Python.

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

Preconditions

Note: The method open of the Image class is not available in S60 1st Edition.

Source code

Simple canvas example with an image being displayed

import appuifw, e32, graphics
 
 
app_lock = e32.Ao_lock()
#Define the exit function
def quit():
app_lock.signal()
appuifw.app.exit_key_handler = quit
 
#Open an image
img = graphics.Image.open("C:\\logo_forumnokia.gif")
 
#Define a function that will be called when the canvas needs to be redrawn
def handle_redraw(rect):
#Place the image on the canvas
c.blit(img)
 
#Create an instance of Canvas and set it as the application's body
c = appuifw.Canvas(redraw_callback=handle_redraw)
appuifw.app.body = c
 
#Wait for the user to request the exit
app_lock.wait()

Complex canvas example with key binding, shape drawing and text writing

import appuifw, e32, graphics, sysinfo
from key_codes import *
 
 
app_lock = e32.Ao_lock()
#Define the exit function
def quit():
app_lock.signal()
appuifw.app.exit_key_handler = quit
 
#Set the screen to full
appuifw.app.screen = 'full'
 
#Get the screen's width and height
screen_w, screen_h = sysinfo.display_pixels()
 
#Define a set of coordinates at which a dot will be drawn on the image
coord = (screen_w / 2, screen_h / 2)
 
#Define a function that will be called when the canvas needs to be redrawn
def handle_redraw(rect):
#Clear the canvas
c.clear()
#Write text
c.text((screen_w / 10, screen_h - screen_h / 10), u"Move the dot", 0x008000, font=(u'Nokia Hindi S60', 35, appuifw.STYLE_BOLD))
#Draw the dot
c.point(coord, 0xff0000, width=10)
 
#Define a function that will be called when one of the bound keys is pressed
def move(direction):
global coord
#Modify the dot's coordinates by 10 pixels according to the direction
if (direction == 'left') and (coord[0] - 10 >= 0):
coord = (coord[0] - 10, coord[1])
if (direction == 'right') and (coord[0] + 10 <= screen_w):
coord = (coord[0] + 10, coord[1])
if (direction == 'up') and (coord[1] - 10 >= 0):
coord = (coord[0], coord[1] - 10)
if (direction == 'down') and (coord[1] + 10 <= screen_h):
coord = (coord[0], coord[1] + 10)
#Redraw the canvas
handle_redraw(())
 
#Create an instance of Canvas and set it as the application's body
c = appuifw.Canvas(redraw_callback=handle_redraw)
appuifw.app.body = c
 
#Bind the navigation keys
c.bind(EKeyLeftArrow, lambda:move('left'))
c.bind(EKeyRightArrow, lambda:move('right'))
c.bind(EKeyUpArrow, lambda:move('up'))
c.bind(EKeyDownArrow, lambda:move('down'))
 
#Wait for the user to request the exit
app_lock.wait()

Post conditions

The Canvas object is created and displayed.

Simple canvas example with an image being displayed
Complex canvas example with key binding, shape drawing and text writing

Additional information

The constructor for the Canvas class is Canvas([redraw_callback=None, event_callback=None, resize_callback=None]), where

  • redraw_callback is a function that is called whenever a part of the canvas has been obscured by something, is then revealed and needs to be redrawn.
  • event_callback is a function that is called whenever a raw key event is received.
  • resize_callback is a function that is called when screen size is changed when the canvas rect size has been changed. It takes a two-element tuple that contains the new clientRect width and height as its argument.


Instances of Canvas have the attribute size, which is tuple containing the current width and height of the canvas.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHowE5ftoE5fcalculatesE5ftheE5ftransparencyE5fforE5ficonsX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxHowE20toE20calculatesE20theE20transparencyE20forE20iconsE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZpythonQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZseriesE5f60QRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxpythonX qfnZuserE5ftagQSxs60X qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ