×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Cool A watch with analog and digital display.

    Hello to all the PyS60 users,
    I begin with PyS60 for few days but i programm with python since around 2 years and
    i want to present you here my first real programm for phones :
    Code:
    # -*- coding: utf-8 -*-
    # A watch construct with the appuifw.Canvas object 
    # with screen refreshment in a main loop. 
    # With 2 time representations:
    # -> Analog time representation.
    # -> Digital time representation.
    # Build with a canvas resolution of (360,487) in portrait mode.
    
    import appuifw, e32,key_codes,graphics
    import math
    import datetime
    
    
    def get_angle(pos,scale) :
      # pos = the time value we want the cursor angle in degrees
      # scale = the time unit: -> 60 for minutes and seconds
      #                        -> 12 for hours
      
      offset=-90 # Offset to the horizontal line ( if offset=0 return 3 o'clock for 12 o'clock)
      degrees=360/scale * pos + offset
      return degrees
    
    def cursor_pos(pos,scale,length) :
      # pos = the time value we want the cursor angle in degrees.
      # scale = the time unit: -> 60 for minutes and seconds.
      #                        -> 12 for hours.
      # length = the cursor length.
      #
      
      degrees=get_angle(pos,scale)
      radians_value=degrees/180.0*math.pi # Conversion from degrees in radians
      x_pos=round(math.cos(radians_value)*length+(360/2)) #360/2 is the analog watch center.
      y_pos=round(math.sin(radians_value)*length+(360/2)) #360/2 is the analog watch center.
      return (x_pos,y_pos)
    
    control=True # Boolean value for the main loop.
    def stop() :
      global control
      control=False # Set the condition of the loop to False.
      appuifw.app.set_exit() # Gracefull application exit.
      
      
    canvas=appuifw.Canvas()
    appuifw.app.screen = "normal"
    appuifw.app.directional_pad = False
    appuifw.app.body = canvas
    appuifw.app.orientation = "portrait"
    appuifw.app.title = u"Watch"
    appuifw.app.exit_key_handler=stop
    
    
    while control :
      # Compute the time, refresh every loop round.
      local_time=datetime.datetime.now()
      cursor_hour=cursor_pos(local_time.hour,12,40)
      cursor_minute=cursor_pos(local_time.minute,60,60)
      cursor_second=cursor_pos(local_time.second,60,80)
      
      
      canvas.begin_redraw()
      canvas.clear((0,0,0)) # Set background color
      
      # Start drawing the analog watch:
      canvas.ellipse(((30,30),(330,330)),fill=(127,127,127)) # Circle 1: Exterior Circle.
      canvas.ellipse(((60,60),(300,300)),fill=(0,0,0))       # Circle 2: Circle with time marks...
      canvas.ellipse(((90,90),(270,270)),fill=(255,255,255)) # Circle 3: The analog clock main circle.
      
      i=0
      while i < 60 : # Draw minutes marks on the circle 2
        to_draw_start=cursor_pos(i+1,60,120)
        to_draw_end=cursor_pos(i+1,60,90)
        canvas.line((to_draw_start , to_draw_end), outline=(255,0,0),width=1)
        i += 1
      
      i=0  
      while i < 12 : # Draw hours marks on the circle 2
        to_draw_start=cursor_pos(i+1,12,120)
        to_draw_end=cursor_pos(i+1,12,90)
        canvas.line((to_draw_start , to_draw_end), outline=(255,0,0),width=4)
        i += 1
      
      i=0
      while i < 12 : # Draw hours marks on the circle 3
        to_draw=cursor_pos(i+1,12,90)
        canvas.line(((canvas.size[0]/2,180),to_draw),outline=(255,0,0),width=1)
        i += 1
    	
      canvas.line(((canvas.size[0]/2,180),cursor_hour),outline=(0,0,0),width=6)   # Draw the hours cursor
      canvas.line(((canvas.size[0]/2,180),cursor_minute),outline=(0,0,0),width=4) # Draw the minutes cursor
      canvas.line(((canvas.size[0]/2,180),cursor_second),outline=(0,0,0),width=2) # Draw the seconds cursor
      # End of drawing the analog watch.
      
      # Start drawing the digital watch:
      canvas.rectangle(((30,347),(99+30,446)),width=6,outline=(63,63,63)) # Hours field
      canvas.text(((30+8,446-16)),unicode(str(local_time.hour).zfill(2)),fill=(255,0,0),font=(None,80,graphics.FONT_ANTIALIAS)) # Hours value printing 
      
      canvas.rectangle(((99+30,347),(99+99+30,446)),width=6,outline=(63,63,63)) # Minutes field
      canvas.text(((99+30+8,446-16)),unicode(str(local_time.minute).zfill(2)),fill=(255,0,0),font=(None,80,graphics.FONT_ANTIALIAS)) # Minutes values printing
      
      canvas.rectangle(((99+99+30,347),(99+99+99+30,446)),width=6,outline=(63,63,63)) # Seconds field
      canvas.text(((99+99+30+8,446-16)), unicode(str(local_time.second).zfill(2)),fill=(255,0,0),font=(None,80,graphics.FONT_ANTIALIAS)) # Seconds values printing
      # End of drawing the digital watch.
      
      canvas.end_redraw()
      
      e32.ao_yield() #We need this call to catch events in the loop.
    The major problem for me was to get access to events in the main loop to stop it and i find a code on the net who solve the problem with the e32.ao_yield() calll.

    Thank's to leave a comment, you can custom the programm and adapt it for your phone.
    Last edited by PySymbianer; 2012-10-17 at 21:04.

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,737

    Re: A watch with analog and digital display.

    Just a short notice: you may find the book, Python on Symbian useful. It is available in the Wiki: http://www.developer.nokia.com/Commu...hon_on_Symbian

Similar Threads

  1. Using GraphicsView for games - how to use mainloop?
    By Arvis17 in forum [Archived] Beginning Qt
    Replies: 5
    Last Post: 2011-03-22, 15:42
  2. Shutdown via Python with S60 3rd
    By heuri in forum Symbian
    Replies: 40
    Last Post: 2008-07-25, 19:24
  3. How to shutdown S60 or S80 phone?
    By timatima in forum Symbian
    Replies: 0
    Last Post: 2006-03-07, 13:01
  4. Shutdown problem
    By Talalo in forum General Development Questions
    Replies: 1
    Last Post: 2005-11-05, 03:33

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×