Zegar

Stronę tą wyświetlono już: 331 razy

Kolejny prosty programik napisany w Pythonie 3+ z wykorzystaniem modułu tkinter, którego celem jest nic innego jak tylko wyświetlanie bieżącego czasu. Oto kod tego programu:

Listing 1
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # program created by owner of page obliczeniowo.jcom.pl
  4. # Licence GPL-3.0 www.gnu.org/licenses/gpl-3.0.en.html
  5. import tkinter as tk
  6. import time as tm
  7. import datetime as dt
  8. import math as mt
  9. def main():
  10. window = tk.Tk()
  11. class Clock:
  12. def __init__(self, window):
  13. self.window = window
  14. self.width = 500
  15. self.height = 500
  16. self.c_draw = tk.Canvas(window, width = self.width, height = self.height)
  17. self.c_draw.pack()
  18. self.settime()
  19. def draw(self):
  20. self.c_draw.delete("all")
  21. l = []
  22. self.c_draw.create_oval([10,10,self.width - 10, self.height - 10], fill="#aaaaaa")
  23. self.c_draw.create_oval([30,30,self.width - 30, self.height - 30], fill="white")
  24. self.c_draw.create_oval([60,60,self.width - 60, self.height - 60], fill="white")
  25. p = [self.width / 2, self.height / 2]
  26. ray = self.width / 2 - 60
  27. raymax = 7
  28. for i in range(12):
  29. p2 = [p[0] + ray * mt.sin(mt.radians(i * 30)), p[1] + ray * mt.cos(mt.radians(i * 30))]
  30. self.c_draw.create_oval([p2[0] - raymax, p2[1] - raymax, p2[0] + raymax, p2[1] + raymax], fill="black")
  31. raymin = 3
  32. for i in range(60):
  33. p2 = [p[0] + ray * mt.sin(mt.radians(i * 6)), p[1] + ray * mt.cos(mt.radians(i * 6))]
  34. self.c_draw.create_oval([p2[0] - raymin, p2[1] - raymin, p2[0] + raymin, p2[1] + raymin], fill="white")
  35. w_min_ray = self.width / 2 - 160
  36. self.c_draw.create_line([p[0], p[1], p[0] + w_min_ray * mt.sin(mt.radians(360 * (self.hour * 60 + self.minutes) / 720)), p[1] - w_min_ray * mt.cos(mt.radians(360 * (self.hour * 60 + self.minutes) / 720))], width = 5., fill='#aaaaaa')
  37. w_max_ray = self.width / 2 - 60
  38. self.c_draw.create_line([p[0], p[1], p[0] + w_max_ray * mt.sin(mt.radians(360 * (self.minutes * 60 + self.seconds) / 3600)), p[1] - w_max_ray * mt.cos(mt.radians(360 * (self.minutes * 60 + self.seconds) / 3600))], width = 4., fill='#aaaaaa')
  39. self.c_draw.create_line([p[0], p[1], p[0] + w_max_ray * mt.sin(mt.radians(360 * self.seconds / 60)), p[1] - w_max_ray * mt.cos(mt.radians(360 * self.seconds / 60))], width = 2., fill='black')
  40. def settime(self):
  41. t = tm.localtime()
  42. self.hour = t.tm_hour % 12
  43. self.minutes = t.tm_min
  44. self.seconds = t.tm_sec
  45. self.window.title("Clock" + str(dt.datetime.now().time()).split(".")[0])
  46. self.draw()
  47. self.window.after(1000, self.settime)
  48. cl = Clock(window)
  49. cl.draw()
  50. window.mainloop()
  51. return 0
  52. if __name__ == '__main__':
  53. main()

Poniżej zamieszczam screen programu.

Screen programu Zegar napisanego w Pythonie 3+
Rys. 1
Screen programu Zegar napisanego w Pythonie 3+.

Komentarze