Posted By

whitetiger on 11/09/06


Tagged

resize image python download timer mask series60 pys60 blit scale sprite feature sleep sms inbox wget tutorial thread miso light lighton


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

anayhk


PyS60 - Thread


 / Published in: Python
 

  1. import appuifw
  2. import e32
  3. import graphics
  4. import thread
  5. import time
  6.  
  7. class Main:
  8.  
  9. def __init__(self):
  10.  
  11. appuifw.app.title = u'Fibonacci'
  12. appuifw.app.body = self.canvas = appuifw.Canvas()
  13. appuifw.exit_key_handler = self.OnExit
  14. appuifw.app.menu = [(u'Calcola', self.OnCalcola)]
  15.  
  16. self.nlock = thread.allocate_lock() # Allocazione di un lock per il thread
  17. self.vlock = thread.allocate_lock()
  18.  
  19. self.ris1 = 0
  20. self.ris2 = ''
  21. self.loop = 1
  22. self.indice = 0
  23. self.img = graphics.Image.new(self.canvas.size)
  24.  
  25. self.OnLoop()
  26.  
  27. def OnCalcola(self):
  28.  
  29. numero = appuifw.query(u'Inserire un Numero', 'number')
  30.  
  31. thread.start_new_thread(self.fibonacci, (numero, ))
  32.  
  33. def fibonacci(self, n):
  34.  
  35. self.ris1 = time.strftime('%H:%M:%S')
  36. self.ris2 = ''
  37.  
  38. a, b = 0, 1
  39.  
  40. for i in range(n):
  41.  
  42. a, b = b, a + b
  43. self.nlock.acquire()
  44. self.indice = ((i+1)*100)/n
  45. self.nlock.release()
  46.  
  47. self.nlock.acquire()
  48. self.ris2 = time.strftime('%H:%M:%S')
  49. self.nlock.release()
  50.  
  51. def OnExit(self):
  52.  
  53. self.loop = 0
  54.  
  55. def OnLoop(self):
  56.  
  57. while self.loop:
  58.  
  59. e32.ao_sleep(0.1)
  60.  
  61. self.img.clear(0)
  62.  
  63. self.img.line([37, 50, 137, 50], 0xffffff, width=20)
  64. self.img.line([37, 50, 37 + self.indice, 50], 0xfffc0d, width=20)
  65. self.img.text((80, 55), u'' + str(self.indice) + u'%', 0x000000)
  66.  
  67. self.vlock.acquire()
  68. if self.ris2 <> '':
  69.  
  70. self.img.text((45, 80), u'Start: ' + str(self.ris1), 0xffffff)
  71. self.img.text((45, 92), u'End: ' + str(self.ris2), 0xffffff)
  72.  
  73.  
  74. self.canvas.blit(self.img)
  75. self.vlock.release()
  76.  
  77. if __name__ == '__main__':
  78.  
  79. main = Main()

Report this snippet  

You need to login to post a comment.