7
7
pygame .mixer .init ()
8
8
pomo_count = 0
9
9
break_count = 0
10
+ enable = 0
10
11
11
12
# path of host file in windows
12
13
host_path = r"C:\Windows\System32\drivers\etc\hosts"
13
14
14
15
# URL of websites to block
15
- block_list = [
16
- 'www.facebook.com' , 'facebook.com' ,
17
- 'www.youtube.com' , 'youtube.com' ,
18
- 'www.gmail.com' , 'gmail.com' ,
19
- 'www.instagram.com' , 'instagram.com' ,
20
- 'www.twitter.com' , 'twitter.com'
21
- ]
16
+ block_list = []
22
17
23
18
# redirecting above URLs to this localhost to ensure blocking
24
19
redirect = "127.0.0.1"
@@ -29,7 +24,12 @@ def block_websites():
29
24
the file if it is not already present and redirect it to the localhost
30
25
for blocking
31
26
"""
32
-
27
+ global web_var
28
+ global enable
29
+ global block_list
30
+ global host_path
31
+ url = web_var .get ()
32
+ block_list .append (url )
33
33
try :
34
34
# Opening the host file in reading and writing mode
35
35
with open (host_path , 'r+' ) as h_file :
@@ -45,48 +45,103 @@ def block_websites():
45
45
else :
46
46
h_file .write (redirect + "\t " + website + "\n " )
47
47
48
+ tk .messagebox .showinfo ("Blocked" , f"{ url } successfully blocked!" )
49
+ enable = 1
50
+ web_var .set ("" )
51
+
48
52
except PermissionError :
49
- tk .messagebox .showinfo ("Error" , "Run cmd in the admin mode and then try again!\n Deselect the option to prevent this popup to show again." )
53
+ tk .messagebox .showinfo ("Error" , "Run cmd in the admin mode and then try again!" )
54
+ web_var .set ("" )
50
55
51
56
except (FileNotFoundError , NameError ):
52
- tk .messagebox .showinfo ("Error" , "Functionality not supported in your OS!\n Deselect the option to prevent this popup to show again." )
57
+ tk .messagebox .showinfo ("Error" , "Functionality not supported in your OS!" )
58
+ web_var .set ("" )
53
59
54
60
55
61
def remove_websites ():
56
62
"""
57
63
The function will unblock the block_list websites by opening the file
58
64
and removing the changes we made before
59
65
"""
66
+ global block_list
67
+ global host_path
60
68
try :
61
- with open (host_path , "r+" ) as file :
62
- content = file .readlines ()
63
- file .seek (0 )
64
- for lines in content :
65
- if not any (website in lines for website in block_list ):
66
- file .write (lines )
67
- file .truncate ()
69
+ if enable :
70
+ # Opening the host file in reading and writing mode
71
+ with open (host_path , "r+" ) as file :
72
+
73
+ # making each line of file into a list
74
+ content = file .readlines ()
75
+
76
+ # sets the file pointer at the beginning of the file
77
+ file .seek (0 )
78
+
79
+ # Traversing through each line of the host file and
80
+ # checking for the websites to be blocked
81
+ for lines in content :
82
+ if not any (website in lines for website in block_list ):
83
+ file .write (lines )
84
+
85
+ # Truncating the file to its original size
86
+ file .truncate ()
87
+
88
+ block_list .clear ()
89
+ enable = 0
68
90
except :
69
91
pass
70
92
finally :
71
93
pass
72
94
73
95
74
- def high_focus ():
96
+ def blocker ():
97
+ """
98
+ The function asks input from user to block websites for high focus mode.
99
+ """
75
100
global enable
76
- if enable .get () == 1 :
77
- block_websites ()
78
- else :
79
- remove_websites ()
101
+ global popup_4
102
+ popup_4 = tk .Toplevel (root )
103
+ popup_4 .title ("Website Blocker!" )
104
+ popup_4 .geometry ("360x220" )
105
+ popup_4 .config ( bg = 'DodgerBlue4' )
106
+
107
+ global block_list
108
+ global web_var
109
+ web_var = tk .StringVar ()
110
+
111
+ pass_label = tk .Label (popup_4 , text = 'Enter URL to block:' , font = ('Arial' ,12 , 'bold' ), bg = 'DodgerBlue4' , fg = 'white' )
112
+ pass_entry = tk .Entry (popup_4 , textvariable = web_var , font = ('Arial' ,12 , 'bold' ))
113
+
114
+ sub_btn = tk .Button (popup_4 , text = 'Block' , font = ('Arial' ,12 , 'bold' ), command = block_websites , bg = 'gold' , activebackground = 'yellow' )
115
+
116
+ text_to_put = '*Supported for windows ONLY\n *You can add multiple urls\n *Don\' t forget to unblock after'
117
+
118
+ instructions = tk .Label (popup_4 , text = text_to_put , font = ('Arial' ,12 , 'bold' ), justify = 'left' , bg = 'sky blue' )
119
+
120
+ unblock_btn = tk .Button (popup_4 , text = 'Unblock all' , font = ('Arial' ,12 , 'bold' ), command = remove_websites , state = 'disabled' , width = 23 , height = 2 , bg = 'gold' , activebackground = 'yellow' )
121
+
122
+ if enable :
123
+ unblock_btn .config (state = 'normal' )
124
+
125
+ pass_label .place (x = 25 , y = 10 )
126
+ pass_entry .place (x = 25 , y = 34 )
127
+ sub_btn .place (x = 255 , y = 30 )
128
+ instructions .place (x = 25 , y = 80 )
129
+ unblock_btn .place (x = 50 , y = 150 )
80
130
81
131
82
132
def break_timer ():
133
+ """
134
+ 5 min timer popup window acting as a callback function to the break timer button
135
+ """
136
+ global enable
83
137
global popup_2
84
138
popup_2 = tk .Toplevel (root )
85
139
popup_2 .title ("Break Timer!" )
86
140
popup_2 .geometry ("370x120" )
87
141
round = 0
88
142
89
143
try :
144
+ # Creating a continous loop of text of time on the screen for 25 mins
90
145
t = 5 * 60
91
146
while t > - 1 :
92
147
minute_count = t // 60
@@ -99,18 +154,22 @@ def break_timer():
99
154
t -= 1
100
155
except :
101
156
pass
102
-
103
- pygame .mixer .music .load ("beep.wav" )
104
- pygame .mixer .music .play (loops = 0 )
105
-
157
+
158
+ # Setting up an alarm sound and popup window to let user know when the time is up
106
159
if t == - 1 :
107
160
tk .messagebox .showinfo ("Time's up!" , "Break is over!\n Time to get to work!" )
108
161
popup_2 .destroy ()
109
162
global break_count
163
+ pygame .mixer .music .load ("./Pomodoro_GUI/beep.wav" )
164
+ pygame .mixer .music .play (loops = 1 )
110
165
break_count += 1
111
166
112
167
113
168
def show_report ():
169
+ """
170
+ The function acts as a callback for show report button and shows the report the hours
171
+ of work they have put in.
172
+ """
114
173
global popup_3
115
174
popup_3 = tk .Toplevel (root )
116
175
popup_3 .title ("Report" )
@@ -126,13 +185,17 @@ def show_report():
126
185
127
186
128
187
def pomodoro_timer ():
188
+ """
189
+ 25 min timer popup window acting as a callback function to the work timer button
190
+ """
129
191
global popup_1
130
192
popup_1 = tk .Toplevel (root )
131
193
popup_1 .title ("Work Timer!" )
132
194
popup_1 .geometry ("370x120" )
133
195
round = 0
134
196
135
197
try :
198
+ # Creating a continous loop of text of time on the screen for 25 mins
136
199
t = 25 * 60
137
200
while t > - 1 :
138
201
minute_count = t // 60
@@ -145,38 +208,39 @@ def pomodoro_timer():
145
208
t -= 1
146
209
except :
147
210
pass
148
-
149
- pygame .mixer .music .load ("beep.wav" )
150
- pygame .mixer .music .play (loops = 1 )
151
-
211
+
212
+ # Setting up an alarm sound and popup window to let user know when the time is up
152
213
if t == - 1 :
153
214
tk .messagebox .showinfo ("Time's up!" , "Pomodoro completed successfully!\n You deserve a break!" )
154
215
popup_1 .destroy ()
155
216
global pomo_count
156
217
pomo_count += 1
218
+ pygame .mixer .music .load ("./Pomodoro_GUI/beep.wav" )
219
+ pygame .mixer .music .play (loops = 0 )
157
220
158
221
159
222
def main ():
223
+ """
224
+ This function produces the main screen of the Pomodoro timer with options to
225
+ select the 25mins work timer, 5mins break timer, block websites for extra focus and
226
+ another option to see the statistics of the time you've put in the work
227
+ """
228
+ # Creating the root window (main screen)
160
229
global root
161
230
root = tk .Tk ()
162
231
root .title ('Timer' )
163
232
root .geometry ('470x608' )
164
233
165
- bg = tk .PhotoImage (file = "bg.png" )
166
-
167
- # Show image using label
234
+ # Setting the screen background
235
+ bg = tk .PhotoImage (file = "./Pomodoro_GUI/bg.png" )
168
236
label1 = tk .Label ( root , image = bg )
169
237
label1 .place (x = 0 , y = 0 )
170
238
171
- global count
172
-
173
239
intro1 = tk .Label (root , text = 'POMODORO TIMER' , bg = 'snow' , fg = 'maroon' , font = ('Arial' , 25 , 'bold' ))
174
- intro1 .place (x = 100 , y = 120 )
240
+ intro1 .place (x = 100 , y = 100 )
175
241
176
- global enable
177
- enable = tk .IntVar ()
178
- check = tk .Checkbutton (root , text = 'Enable website blocker' , variable = enable , font = ('Arial' , 12 , 'bold' ), bg = 'gold' , activebackground = 'yellow' , height = 1 , width = 25 , onvalue = 1 , offvalue = 0 , command = high_focus )
179
- check .place (x = 100 , y = 190 )
242
+ blocker_btn = tk .Button (root , text = 'WEBSITE BLOCKER' , command = blocker , font = ('Arial' , 12 , 'bold' ), bg = 'gold' , activebackground = 'yellow' , height = 3 , width = 25 )
243
+ blocker_btn .place (x = 100 , y = 150 )
180
244
181
245
start_btn = tk .Button (root , text = 'START WORK TIMER' , command = pomodoro_timer , font = ('Arial' , 12 , 'bold' ), bg = 'gold' , activebackground = 'yellow' , height = 3 , width = 25 )
182
246
start_btn .place (x = 100 , y = 250 )
0 commit comments