Zegar

Autor podstrony: Krzysztof Zajączkowski

Stronę tą wyświetlono już: 4725 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:

#!/usr/bin/env python # -*- coding: utf-8 -*- # program created by owner of page obliczeniowo.jcom.pl # Licence GPL-3.0 www.gnu.org/licenses/gpl-3.0.en.html import tkinter as tk import time as tm import datetime as dt import math as mt def main(): window = tk.Tk() class Clock: def __init__(self, window): self.window = window self.width = 500 self.height = 500 self.c_draw = tk.Canvas(window, width = self.width, height = self.height) self.c_draw.pack() self.settime() def draw(self): self.c_draw.delete("all") l = [] self.c_draw.create_oval([10,10,self.width - 10, self.height - 10], fill="#aaaaaa") self.c_draw.create_oval([30,30,self.width - 30, self.height - 30], fill="white") self.c_draw.create_oval([60,60,self.width - 60, self.height - 60], fill="white") p = [self.width / 2, self.height / 2] ray = self.width / 2 - 60 raymax = 7 for i in range(12): p2 = [p[0] + ray * mt.sin(mt.radians(i * 30)), p[1] + ray * mt.cos(mt.radians(i * 30))] self.c_draw.create_oval([p2[0] - raymax, p2[1] - raymax, p2[0] + raymax, p2[1] + raymax], fill="black") raymin = 3 for i in range(60): p2 = [p[0] + ray * mt.sin(mt.radians(i * 6)), p[1] + ray * mt.cos(mt.radians(i * 6))] self.c_draw.create_oval([p2[0] - raymin, p2[1] - raymin, p2[0] + raymin, p2[1] + raymin], fill="white") w_min_ray = self.width / 2 - 160 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') w_max_ray = self.width / 2 - 60 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') 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') def settime(self): t = tm.localtime() self.hour = t.tm_hour % 12 self.minutes = t.tm_min self.seconds = t.tm_sec self.window.title("Clock" + str(dt.datetime.now().time()).split(".")[0]) self.draw() self.window.after(1000, self.settime) cl = Clock(window) cl.draw() window.mainloop() return 0 if __name__ == '__main__': main()

Poniżej zamieszczam screen programu.

Screen programu Zegar napisanego w Pythonie 3+
Rys. 1
Screen programu Zegar napisanego w Pythonie 3+.
Propozycje książek