×
Namespaces

Variants
Actions
Revision as of 02:47, 6 August 2009 by lpvalente (Talk | contribs)

Archived:Drawing a Calendar on PySymbian Canvas

From Nokia Developer Wiki
Jump to: navigation, search


Article Metadata
Tested with
Devices(s): N73,N95
Compatibility
Platform(s): S60 3rd Edition, S60 5th Edition
Article
Keywords: Calendar
Created: (20 Jul 2009)
Last edited: lpvalente (06 Aug 2009)

Contents

Introduction

This article demonstrates how to draw a calendar on a canvas. The code snippet is written with the simplest mobile application development tool, PyS60.

The application uses the following modules:

  • appuifw module
  • graphics module
  • e32 module


The following functions are used to create the canvas based calendar:

  • draw_screen():draw the black screen on canvas
  • draw_text():write some text on square
  • print_number: print number on square


The following parameters are used when developing this application. They are customizable.

  • number_of_square: Initialize number of column
  • number_of_lines: Initialize number of row
  • Horizontal_Separator: Separate the space between column
  • Vertical_Separator: Separate the space between row


All the functions and parameters are defined in source code in detail.

Code snippet

The code for the calendar is below.

# import module
import appuifw,graphics,e32
 
# define color code
BLACK= (0,0,0)
RED=(255,0,0)
BLUE= (0,0,255)
GREEN=(0,255,0)
ORANGE= (255,127,0)
WHITE=(255,255,255)
global width,height
width=320
height=320
 
#Global variables
sqrt_x=width/16
sqrt_y=height/4
sqrt_width=30
sqrt_height=25
number_of_square=7
number_of_lines=6
Horizontal_Separator=3
Vertical_Separator=3
 
TextColor=RED
PrintNumber=WHITE
canvas=None
img=None
 
def handle_redraw(rect):
canvas.blit(img)
 
# exit function
def quit():
app_lock.signal()
 
#---------------------------
#draw draw screen
#---------------------------
 
def draw_screen():
 
#Global variable for application UI
global canvas
 
#Create an instance of Canvas and set it as the application's body
canvas = appuifw.Canvas(redraw_callback = handle_redraw,event_callback = None)
appuifw.app.body=canvas
 
#Set the screen to full
appuifw.app.screen = 'full'
appuifw.app.exit_key_handler=quit
 
#Global variable for application UI
global img
img = graphics.Image.new((canvas.size))
 
#Clear the image
img.clear(BLACK)
global x1,y1,x2,y2, sqrt_x, sqrt_y, sqrt_width, sqrt_height,number_of_square, Horizontal_Separator,Vertical_Separator,number_of_lines
j=0
while j<number_of_lines:
i=0
k=0
 
#define rectangle coordinate
x1=sqrt_x
y1=sqrt_y+(j*(sqrt_height+Vertical_Separator))
x2=x1+sqrt_width
y2=y1+sqrt_height
while (i<number_of_square):
#draw rectangle
img.rectangle((x1+ (i*sqrt_width),y1,x2+ (i*sqrt_width)-Horizontal_Separator,y2),outline=BLUE)
i=i+1
handle_redraw(None)
j=j+1
Total_x, Total_y = canvas.size
img.text((35,50), u"CALENDER", fill = BLUE,font=(u'Nokia Hindi S60',40,appuifw.STYLE_BOLD))
handle_redraw(None)
 
#define draw_text function
def draw_text():
global sqrt_x,sqrt_y,t1,t2,sqrt_height,y3,x3, number_of_square
t1=5
t2=20
y3=sqrt_y+t2
i=1
a=[u'S',u'M',u'T',u'W',u'T',u'F',u'S']
while i< number_of_square +1:
# draw text on canvas
img.text(((sqrt_x+(i*t1)+((i-1)*sqrt_height)),y3), unicode(a[i-1]), fill=TextColor,font=(u'Nokia Hindi S60',20,appuifw.STYLE_BOLD))
i=i+1
handle_redraw(None)
 
 
def draw_number(i, no_of_days_in_month):
global number_of_lines, number_of_square
j=0
a=1
while j<(number_of_lines-1):
if i==number_of_square:
i=0
t1=5
x1=sqrt_x
y1=sqrt_y+((j+1)*sqrt_width)+14
while (i<number_of_square):
img.text((x1+ (i*sqrt_height)+((i+1)*t1),y1),unicode(a),fill=PrintNumber)
a=a+1
i=i+1
if a==no_of_days_in_month+1:
break
handle_redraw(None)
#a=a+1
j=j+1
#calling the function
draw_screen()
draw_text()
draw_number(3,31)
app_lock=e32.Ao_lock()
 
#Wait for the user to request the exit
app_lock.wait()

Screenshots

Draw square.JPG Draw text.JPG


Print number.JPG Print number2.JPG

Related links

115 page views in the last 30 days.
×