1+ from tkinter import *
2+ from tkinter .ttk import *
3+ import datetime
4+ import platform
5+ try :
6+ import winsound
7+ type = 'windows'
8+ except :
9+ import os
10+ type = 'other'
11+ window = Tk ()
12+ window .title ("Clock" )
13+ window .geometry ('500x250' )
14+ stopwatch_counter_num = 66600
15+ stopwatch_running = False
16+ timer_counter_num = 66600
17+ timer_running = False
18+ def clock ():
19+ date_time = datetime .datetime .now ().strftime ("%d-%m-%Y %H:%M:%S/%p" )
20+ date ,time1 = date_time .split ()
21+ time2 ,time3 = time1 .split ('/' )
22+ hour ,minutes ,seconds = time2 .split (':' )
23+ if int (hour ) > 11 and int (hour ) < 24 :
24+ time = str (int (hour ) - 12 ) + ':' + minutes + ':' + seconds + ' ' + time3
25+ else :
26+ time = time2 + ' ' + time3
27+ time_label .config (text = time )
28+ date_label .config (text = date )
29+ time_label .after (1000 , clock )
30+ def alarm ():
31+ main_time = datetime .datetime .now ().strftime ("%H:%M %p" )
32+ alarm_time = get_alarm_time_entry .get ()
33+ alarm_time1 ,alarm_time2 = alarm_time .split (' ' )
34+ alarm_hour , alarm_minutes = alarm_time1 .split (':' )
35+ main_time1 ,main_time2 = main_time .split (' ' )
36+ main_hour1 , main_minutes = main_time1 .split (':' )
37+ if main_time2 == 'PM' :
38+ main_hour = str (int (main_hour1 ) - 12 )
39+ else :
40+ main_hour = main_hour1
41+ if int (alarm_hour ) == int (main_hour ) and int (alarm_minutes ) == int (main_minutes ) and main_time2 == alarm_time2 :
42+ for i in range (3 ):
43+ alarm_status_label .config (text = 'Time Is Up' )
44+ if platform .system () == 'Windows' :
45+ winsound .Beep (5000 ,1000 )
46+ elif platform .system () == 'Darwin' :
47+ os .system ('say Time is Up' )
48+ elif platform .system () == 'Linux' :
49+ os .system ('beep -f 5000' )
50+ get_alarm_time_entry .config (state = 'enabled' )
51+ set_alarm_button .config (state = 'enabled' )
52+ get_alarm_time_entry .delete (0 ,END )
53+ alarm_status_label .config (text = '' )
54+ else :
55+ alarm_status_label .config (text = 'Alarm Has Started' )
56+ get_alarm_time_entry .config (state = 'disabled' )
57+ set_alarm_button .config (state = 'disabled' )
58+ alarm_status_label .after (1000 , alarm )
59+ def stopwatch_counter (label ):
60+ def count ():
61+ if stopwatch_running :
62+ global stopwatch_counter_num
63+ if stopwatch_counter_num == 66600 :
64+ display = "Starting..."
65+ else :
66+ tt = datetime .datetime .fromtimestamp (stopwatch_counter_num )
67+ string = tt .strftime ("%H:%M:%S" )
68+ display = string
69+ label .config (text = display )
70+ label .after (1000 , count )
71+ stopwatch_counter_num += 1
72+ count ()
73+ def stopwatch (work ):
74+ if work == 'start' :
75+ global stopwatch_running
76+ stopwatch_running = True
77+ stopwatch_start .config (state = 'disabled' )
78+ stopwatch_stop .config (state = 'enabled' )
79+ stopwatch_reset .config (state = 'enabled' )
80+ stopwatch_counter (stopwatch_label )
81+ elif work == 'stop' :
82+ stopwatch_running = False
83+ stopwatch_start .config (state = 'enabled' )
84+ stopwatch_stop .config (state = 'disabled' )
85+ stopwatch_reset .config (state = 'enabled' )
86+ elif work == 'reset' :
87+ global stopwatch_counter_num
88+ stopwatch_running = False
89+ stopwatch_counter_num = 66600
90+ stopwatch_label .config (text = 'Stopwatch' )
91+ stopwatch_start .config (state = 'enabled' )
92+ stopwatch_stop .config (state = 'disabled' )
93+ stopwatch_reset .config (state = 'disabled' )
94+ def timer_counter (label ):
95+ def count ():
96+ global timer_running
97+ if timer_running :
98+ global timer_counter_num
99+ if timer_counter_num == 66600 :
100+ for i in range (3 ):
101+ display = "Time Is Up"
102+ if platform .system () == 'Windows' :
103+ winsound .Beep (5000 ,1000 )
104+ elif platform .system () == 'Darwin' :
105+ os .system ('say Time is Up' )
106+ elif platform .system () == 'Linux' :
107+ os .system ('beep -f 5000' )
108+ timer_running = False
109+ timer ('reset' )
110+ else :
111+ tt = datetime .datetime .fromtimestamp (timer_counter_num )
112+ string = tt .strftime ("%H:%M:%S" )
113+ display = string
114+ timer_counter_num -= 1
115+ label .config (text = display )
116+ label .after (1000 , count )
117+ count ()
118+ def timer (work ):
119+ if work == 'start' :
120+ global timer_running , timer_counter_num
121+ timer_running = True
122+ if timer_counter_num == 66600 :
123+ timer_time_str = timer_get_entry .get ()
124+ hours ,minutes ,seconds = timer_time_str .split (':' )
125+ minutes = int (minutes ) + (int (hours ) * 60 )
126+ seconds = int (seconds ) + (minutes * 60 )
127+ timer_counter_num = timer_counter_num + seconds
128+ timer_counter (timer_label )
129+ timer_start .config (state = 'disabled' )
130+ timer_stop .config (state = 'enabled' )
131+ timer_reset .config (state = 'enabled' )
132+ timer_get_entry .delete (0 ,END )
133+ elif work == 'stop' :
134+ timer_running = False
135+ timer_start .config (state = 'enabled' )
136+ timer_stop .config (state = 'disabled' )
137+ timer_reset .config (state = 'enabled' )
138+ elif work == 'reset' :
139+ timer_running = False
140+ timer_counter_num = 66600
141+ timer_start .config (state = 'enabled' )
142+ timer_stop .config (state = 'disabled' )
143+ timer_reset .config (state = 'disabled' )
144+ timer_get_entry .config (state = 'enabled' )
145+ timer_label .config (text = 'Timer' )
146+ tabs_control = Notebook (window )
147+ clock_tab = Frame (tabs_control )
148+ alarm_tab = Frame (tabs_control )
149+ stopwatch_tab = Frame (tabs_control )
150+ timer_tab = Frame (tabs_control )
151+ tabs_control .add (clock_tab , text = "Clock" )
152+ tabs_control .add (alarm_tab , text = "Alarm" )
153+ tabs_control .add (stopwatch_tab , text = 'Stopwatch' )
154+ tabs_control .add (timer_tab , text = 'Timer' )
155+ tabs_control .pack (expand = 1 , fill = "both" )
156+ time_label = Label (clock_tab , font = 'calibri 40 bold' , foreground = 'black' )
157+ time_label .pack (anchor = 'center' )
158+ date_label = Label (clock_tab , font = 'calibri 40 bold' , foreground = 'black' )
159+ date_label .pack (anchor = 's' )
160+ get_alarm_time_entry = Entry (alarm_tab , font = 'calibri 15 bold' )
161+ get_alarm_time_entry .pack (anchor = 'center' )
162+ alarm_instructions_label = Label (alarm_tab , font = 'calibri 10 bold' , text = "Enter Alarm Time. Eg -> 01:30 PM, 01 -> Hour, 30 -> Minutes" )
163+ alarm_instructions_label .pack (anchor = 's' )
164+ set_alarm_button = Button (alarm_tab , text = "Set Alarm" , command = alarm )
165+ set_alarm_button .pack (anchor = 's' )
166+ alarm_status_label = Label (alarm_tab , font = 'calibri 15 bold' )
167+ alarm_status_label .pack (anchor = 's' )
168+ stopwatch_label = Label (stopwatch_tab , font = 'calibri 40 bold' , text = 'Stopwatch' )
169+ stopwatch_label .pack (anchor = 'center' )
170+ stopwatch_start = Button (stopwatch_tab , text = 'Start' , command = lambda :stopwatch ('start' ))
171+ stopwatch_start .pack (anchor = 'center' )
172+ stopwatch_stop = Button (stopwatch_tab , text = 'Stop' , state = 'disabled' ,command = lambda :stopwatch ('stop' ))
173+ stopwatch_stop .pack (anchor = 'center' )
174+ stopwatch_reset = Button (stopwatch_tab , text = 'Reset' , state = 'disabled' , command = lambda :stopwatch ('reset' ))
175+ stopwatch_reset .pack (anchor = 'center' )
176+ timer_get_entry = Entry (timer_tab , font = 'calibiri 15 bold' )
177+ timer_get_entry .pack (anchor = 'center' )
178+ timer_instructions_label = Label (timer_tab , font = 'calibri 10 bold' , text = "Enter Timer Time. Eg -> 01:30:30, 01 -> Hour, 30 -> Minutes, 30 -> Seconds" )
179+ timer_instructions_label .pack (anchor = 's' )
180+ timer_label = Label (timer_tab , font = 'calibri 40 bold' , text = 'Timer' )
181+ timer_label .pack (anchor = 'center' )
182+ timer_start = Button (timer_tab , text = 'Start' , command = lambda :timer ('start' ))
183+ timer_start .pack (anchor = 'center' )
184+ timer_stop = Button (timer_tab , text = 'Stop' , state = 'disabled' ,command = lambda :timer ('stop' ))
185+ timer_stop .pack (anchor = 'center' )
186+ timer_reset = Button (timer_tab , text = 'Reset' , state = 'disabled' , command = lambda :timer ('reset' ))
187+ timer_reset .pack (anchor = 'center' )
188+ clock ()
189+ window .mainloop ()
0 commit comments