forked from mrayanasim09/python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.py
77 lines (64 loc) · 2.31 KB
/
clock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# This code is made by MRayan Asim
try:
import tkinter as Tkinter
except ImportError:
import Tkinter
import math
import time
import os
class Main(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.x = 150
self.y = 150
self.length = 50
self.create_all_functions()
def create_all_functions(self):
self.create_canvas_for_shapes()
self.create_background()
self.create_sticks()
def create_background(self):
file_name = "download.png" # you can replace it with your image name or you can download the image you can check out it at https://github.com/mrayanasim09/python-projects/blob/main/GUI/download.png
script_directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_directory, file_name)
self.image = Tkinter.PhotoImage(file=file_path)
self.canvas.create_image(150, 150, image=self.image)
def create_canvas_for_shapes(self):
self.canvas = Tkinter.Canvas(self, bg="black")
self.canvas.pack(expand="yes", fill="both")
def create_sticks(self):
self.sticks = []
for _ in range(3):
store = self.canvas.create_line(
self.x,
self.y,
self.x + self.length,
self.y + self.length,
width=2,
fill="red",
)
self.sticks.append(store)
def update_class(self):
now = time.localtime()
t = time.strptime(str(now.tm_hour), "%H")
hour = int(time.strftime("%I", t)) * 5
now = (hour, now.tm_min, now.tm_sec)
for n, i in enumerate(now):
x, y = self.canvas.coords(self.sticks[n])[0:2]
cr = [x, y]
cr.append(
self.length * math.cos(math.radians(i * 6) - math.radians(90)) + self.x
)
cr.append(
self.length * math.sin(math.radians(i * 6) - math.radians(90)) + self.y
)
self.canvas.coords(self.sticks[n], tuple(cr))
if __name__ == "__main__":
root = Main()
def main_loop():
root.update()
root.update_idletasks()
root.update_class()
root.after(1000, main_loop) # Call the main loop function after 1 second
main_loop()
root.mainloop()