Guest User

Zegar analogowy

a guest
Feb 7th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import time
  2. import tkinter as tk
  3. import math
  4.  
  5.  
  6. def gettime():
  7. txt = time.ctime()
  8. return int(txt[11:13]),int(txt[14:16]),int(txt[17:19])
  9.  
  10. class tarcza:
  11. def __init__(self, kanwa):
  12. self.kanwa = kanwa
  13. self.konfig()
  14. self.rysuj()
  15.  
  16. def konfig(self):
  17. self.wys = int(self.kanwa.config('height')[-1])
  18. self.szer = int(self.kanwa.config('width')[-1])
  19. self.x0,self.y0=self.szer/2, self.wys/2
  20. self.Rmax = min(self.x0,self.y0)
  21.  
  22.  
  23. def rysuj(self):
  24. godzina = 12
  25. for alfa in range(90,421,30):
  26. self.linia(self.Rmax-50, alfa, self.Rmax-70, alfa)
  27. self.podpis( self.Rmax-20, alfa, str(godzina))
  28. godzina -= 1
  29.  
  30. def podpis (self, r, alfa, txt):
  31. x=self.x0 + r*math.cos(math.radians(alfa))
  32. y=self.y0 - r*math.sin(math.radians(alfa))
  33. kanwa.create_text(x,y,text=txt, fill='blue', font=('Arial',18))
  34.  
  35. def linia(self, r1, alfa1, r2, alfa2):
  36. x1=self.x0 + r1*math.cos(math.radians(alfa1))
  37. y1=self.y0 - r1*math.sin(math.radians(alfa1))
  38. x2=self.x0 + r2*math.cos(math.radians(alfa1))
  39. y2=self.y0 - r2*math.sin(math.radians(alfa1))
  40. kanwa.create_line(x1,y1,x2,y2,width=5)
  41.  
  42. class wskaz:
  43. def __init__(self, kanwa):
  44. self.kanwa = kanwa
  45. x0 = 600/2; lx = 9*600/20
  46. y0 = 600/2; ly = 9*600/20
  47. r1 = 0.9 * min(lx,ly)
  48. r2 = 0.6 * min(lx,ly)
  49. r3 = 0.8 * min(lx,ly)
  50.  
  51. def czas(self):
  52. t = localtime()
  53. t_s = t[5]
  54. t_m = t[4] + t_s/60
  55. t_h = t[3] % 12 + t_m/60
  56.  
  57. def godzina(self, x0, y0, r1):
  58. phi = pi/6 * t_h
  59. x = x0 + r1 * sin(phi)
  60. y = y0 - r1 * cos(phi)
  61. kanwa.create_line(x0, y0, x, y, arrow=LAST, fill="red", width=3)
  62.  
  63. def minuta(self, x0, y0, r2):
  64. phi = pi/30 * t_m
  65. x = x0 + r2 * sin(phi)
  66. y = y0 - r2 * cos(phi)
  67. kanwa.create_line(x0, y0, x, y, arrow=LAST, fill="blue", width=2)
  68.  
  69. def sekunda(self, x0, y0, r3):
  70. phi = pi/30 * t_s
  71. x = x0 + r2 * sin(phi)
  72. y = y0 - r2 * cos(phi)
  73. kanwa.create_line(x0, y0 , x, y, arrow=LAST)
  74.  
  75.  
  76. class zegarek(wskaz):
  77. def zegar(kanwa):
  78. kanwa.delete("all")
  79. wskaz(kanwa)
  80. kanwa.after(10, zegar, kanwa)
  81.  
  82.  
  83. if __name__ == '__main__':
  84. app = tk.Tk()
  85. app.title('Zegarek')
  86. kanwa = tk.Canvas(app, width = 600, height = 600)
  87. kanwa.pack()
  88.  
  89. T=tarcza(kanwa)
  90. W=zegarek(kanwa)
  91.  
  92.  
  93. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment