|
| 1 | +import time |
| 2 | +import tkinter as tk |
| 3 | +from tkinter import messagebox |
| 4 | +import pygame |
| 5 | +from datetime import timedelta |
| 6 | + |
| 7 | +pygame.mixer.init() |
| 8 | +pomo_count = 0 |
| 9 | +break_count = 0 |
| 10 | + |
| 11 | +# path of host file in windows |
| 12 | +host_path = r"C:\Windows\System32\drivers\etc\hosts" |
| 13 | + |
| 14 | +# 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 | +] |
| 22 | + |
| 23 | +# redirecting above URLs to this localhost to ensure blocking |
| 24 | +redirect = "127.0.0.1" |
| 25 | + |
| 26 | +def block_websites(): |
| 27 | + """ |
| 28 | + The function will open the host file and add the block-list websites to |
| 29 | + the file if it is not already present and redirect it to the localhost |
| 30 | + for blocking |
| 31 | + """ |
| 32 | + |
| 33 | + try: |
| 34 | + # Opening the host file in reading and writing mode |
| 35 | + with open(host_path, 'r+') as h_file: |
| 36 | + content = h_file.read() |
| 37 | + |
| 38 | + for website in block_list: |
| 39 | + |
| 40 | + # Website is already blocked |
| 41 | + if website in content: |
| 42 | + pass |
| 43 | + |
| 44 | + # To redirect the website to be blocked |
| 45 | + else: |
| 46 | + h_file.write(redirect + "\t" + website + "\n") |
| 47 | + |
| 48 | + except PermissionError: |
| 49 | + tk.messagebox.showinfo("Error", "Run cmd in the admin mode and then try again!\nDeselect the option to prevent this popup to show again.") |
| 50 | + |
| 51 | + except (FileNotFoundError, NameError): |
| 52 | + tk.messagebox.showinfo("Error", "Functionality not supported in your OS!\nDeselect the option to prevent this popup to show again.") |
| 53 | + |
| 54 | + |
| 55 | +def remove_websites(): |
| 56 | + """ |
| 57 | + The function will unblock the block_list websites by opening the file |
| 58 | + and removing the changes we made before |
| 59 | + """ |
| 60 | + 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() |
| 68 | + except: |
| 69 | + pass |
| 70 | + finally: |
| 71 | + pass |
| 72 | + |
| 73 | + |
| 74 | +def high_focus(): |
| 75 | + global enable |
| 76 | + if enable.get() == 1: |
| 77 | + block_websites() |
| 78 | + else: |
| 79 | + remove_websites() |
| 80 | + |
| 81 | + |
| 82 | +def break_timer(): |
| 83 | + global popup_2 |
| 84 | + popup_2 = tk.Toplevel(root) |
| 85 | + popup_2.title("Break Timer!") |
| 86 | + popup_2.geometry("370x120") |
| 87 | + round = 0 |
| 88 | + |
| 89 | + try: |
| 90 | + t = 5*60 |
| 91 | + while t>-1: |
| 92 | + minute_count = t // 60 |
| 93 | + second_count = t % 60 |
| 94 | + timer = '{:02d}:{:02d}'.format(minute_count, second_count) |
| 95 | + time_display = tk.Label(popup_2, text = timer, bg = 'DodgerBlue4', fg = 'white', font = ('STIX', 90, 'bold')) |
| 96 | + time_display.place(x=0,y=0) |
| 97 | + popup_2.update() |
| 98 | + time.sleep(1) |
| 99 | + t -= 1 |
| 100 | + except: |
| 101 | + pass |
| 102 | + |
| 103 | + pygame.mixer.music.load("beep.wav") |
| 104 | + pygame.mixer.music.play(loops=0) |
| 105 | + |
| 106 | + if t == -1: |
| 107 | + tk.messagebox.showinfo("Time's up!", "Break is over!\nTime to get to work!") |
| 108 | + popup_2.destroy() |
| 109 | + global break_count |
| 110 | + break_count += 1 |
| 111 | + |
| 112 | + |
| 113 | +def show_report(): |
| 114 | + global popup_3 |
| 115 | + popup_3 = tk.Toplevel(root) |
| 116 | + popup_3.title("Report") |
| 117 | + popup_3.geometry("370x170") |
| 118 | + popup_3.config( bg = 'DodgerBlue4') |
| 119 | + |
| 120 | + pomo_time = str(timedelta(minutes=pomo_count*25))[:-3] |
| 121 | + break_time = str(timedelta(minutes=pomo_count*5))[:-3] |
| 122 | + tk.Label(popup_3, text=f"Number of Pomodoros completed: {pomo_count}", justify=tk.LEFT, bg = 'DodgerBlue4', fg = 'white', font=('Arial',12,'bold')).place(x = 10, y = 10) |
| 123 | + tk.Label(popup_3, text=f"Number of breaks completed: {break_count}", justify=tk.LEFT, bg = 'DodgerBlue4', fg = 'white', font=('Arial',12,'bold')).place(x = 10, y = 50) |
| 124 | + tk.Label(popup_3, text=f"Hours of work done: {pomo_time} hrs", justify=tk.LEFT, bg = 'DodgerBlue4', fg = 'white', font=('Arial',12,'bold')).place(x = 10, y = 90) |
| 125 | + tk.Label(popup_3, text=f"Hours of break taken: {break_time} hrs", justify=tk.LEFT, bg = 'DodgerBlue4', fg = 'white', font=('Arial',12,'bold')).place(x = 10, y = 130) |
| 126 | + |
| 127 | + |
| 128 | +def pomodoro_timer(): |
| 129 | + global popup_1 |
| 130 | + popup_1 = tk.Toplevel(root) |
| 131 | + popup_1.title("Work Timer!") |
| 132 | + popup_1.geometry("370x120") |
| 133 | + round = 0 |
| 134 | + |
| 135 | + try: |
| 136 | + t = 25*60 |
| 137 | + while t>-1: |
| 138 | + minute_count = t // 60 |
| 139 | + second_count = t % 60 |
| 140 | + timer = '{:02d}:{:02d}'.format(minute_count, second_count) |
| 141 | + time_display = tk.Label(popup_1, text = timer, bg = 'DodgerBlue4', fg = 'white', font = ('STIX', 90, 'bold')) |
| 142 | + time_display.place(x=0,y=0) |
| 143 | + popup_1.update() |
| 144 | + time.sleep(1) |
| 145 | + t -= 1 |
| 146 | + except: |
| 147 | + pass |
| 148 | + |
| 149 | + pygame.mixer.music.load("beep.wav") |
| 150 | + pygame.mixer.music.play(loops=1) |
| 151 | + |
| 152 | + if t == -1: |
| 153 | + tk.messagebox.showinfo("Time's up!", "Pomodoro completed successfully!\nYou deserve a break!") |
| 154 | + popup_1.destroy() |
| 155 | + global pomo_count |
| 156 | + pomo_count += 1 |
| 157 | + |
| 158 | + |
| 159 | +def main(): |
| 160 | + global root |
| 161 | + root = tk.Tk() |
| 162 | + root.title('Timer') |
| 163 | + root.geometry('470x608') |
| 164 | + |
| 165 | + bg = tk.PhotoImage(file = "bg.png") |
| 166 | + |
| 167 | + # Show image using label |
| 168 | + label1 = tk.Label( root, image = bg) |
| 169 | + label1.place(x = 0, y = 0) |
| 170 | + |
| 171 | + global count |
| 172 | + |
| 173 | + intro1 = tk.Label(root, text = 'POMODORO TIMER', bg = 'snow', fg = 'maroon', font = ('Arial', 25, 'bold')) |
| 174 | + intro1.place(x=100, y=120) |
| 175 | + |
| 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) |
| 180 | + |
| 181 | + 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 | + start_btn.place(x=100, y=250) |
| 183 | + |
| 184 | + break_btn = tk.Button(root, text = 'START BREAK TIMER', command = break_timer, font = ('Arial', 12, 'bold'), bg='gold', activebackground='yellow', height = 3, width = 25) |
| 185 | + break_btn.place(x=100, y=350) |
| 186 | + |
| 187 | + report_btn = tk.Button(root, text = 'SHOW REPORT', command = show_report, font = ('Arial', 12, 'bold'), bg='gold', activebackground='yellow', height = 3, width = 25) |
| 188 | + report_btn.place(x=100, y=450) |
| 189 | + |
| 190 | + root.mainloop() |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == '__main__': |
| 194 | + main() |
| 195 | + |
0 commit comments