|
| 1 | +from datetime import datetime |
| 2 | +import tkinter as tk |
| 3 | +import tkinter.messagebox |
| 4 | +import pytz |
| 5 | + |
| 6 | +#Creating main Window |
| 7 | +window = tk.Tk() |
| 8 | +window.title("Time-Zone Converter") |
| 9 | +window.geometry('480x300') |
| 10 | + |
| 11 | +# giving the format of datetime |
| 12 | +format = "%Y-%m-%d %H:%M" |
| 13 | + |
| 14 | +#entry box for all the values |
| 15 | +entry_cur_tz = tk.Entry(window, width=20, font=('arial',10)) |
| 16 | +entry_cur_tz.place(x=20,y=100) |
| 17 | + |
| 18 | +entry_cnv_tz = tk.Entry(window, width=20, font=('arial',10)) |
| 19 | +entry_cnv_tz.place(x=250,y=100) |
| 20 | + |
| 21 | +entry_cur_tm = tk.Entry(window, width=20, font=('arial',10)) |
| 22 | +entry_cur_tm.place(x=20,y=170) |
| 23 | + |
| 24 | +entry_cnv_tm = tk.Entry(window, width=20, font=('arial',10)) |
| 25 | +entry_cnv_tm.place(x=250,y=170) |
| 26 | + |
| 27 | +#All Entry Box configuration |
| 28 | +def on_focus_in_ent_cur_tz(event): |
| 29 | + if entry_cur_tz.get() == 'Eg. Asia/Kolkata': |
| 30 | + entry_cur_tz.delete(0, "end") |
| 31 | + entry_cur_tz.insert(0, '') |
| 32 | + entry_cur_tz.config(fg = 'black') |
| 33 | +def on_focus_out_ent_cur_tz(event): |
| 34 | + if entry_cur_tz.get() == '': |
| 35 | + entry_cur_tz.insert(0, 'Eg. Asia/Kolkata') |
| 36 | + entry_cur_tz.config(fg = 'grey') |
| 37 | + |
| 38 | +entry_cur_tz.insert(0, 'Eg. Asia/Kolkata') |
| 39 | +entry_cur_tz.bind('<FocusIn>', on_focus_in_ent_cur_tz) |
| 40 | +entry_cur_tz.bind('<FocusOut>', on_focus_out_ent_cur_tz) |
| 41 | +entry_cur_tz.config(fg = 'grey') |
| 42 | + |
| 43 | + |
| 44 | +def on_focus_in_ent_cnv_tz(event): |
| 45 | + if entry_cnv_tz.get() == 'Eg. Australia/Sydney': |
| 46 | + entry_cnv_tz.delete(0, "end") |
| 47 | + entry_cnv_tz.insert(0, '') |
| 48 | + entry_cnv_tz.config(fg = 'black') |
| 49 | +def on_focus_out_ent_cnv_tz(event): |
| 50 | + if entry_cnv_tz.get() == '': |
| 51 | + entry_cnv_tz.insert(0, 'Eg. Australia/Sydney') |
| 52 | + entry_cnv_tz.config(fg = 'grey') |
| 53 | + |
| 54 | +entry_cnv_tz.insert(0, 'Eg. Australia/Sydney') |
| 55 | +entry_cnv_tz.bind('<FocusIn>', on_focus_in_ent_cnv_tz) |
| 56 | +entry_cnv_tz.bind('<FocusOut>', on_focus_out_ent_cnv_tz) |
| 57 | +entry_cnv_tz.config(fg = 'grey') |
| 58 | + |
| 59 | + |
| 60 | +def on_focus_in_ent_cur_tm(event): |
| 61 | + if entry_cur_tm.get() == "Eg. 2023-06-02 20:01": |
| 62 | + entry_cur_tm.delete(0, "end") |
| 63 | + entry_cur_tm.insert(0, '') |
| 64 | + entry_cur_tm.config(fg = 'black') |
| 65 | +def on_focus_out_ent_cur_tm(event): |
| 66 | + if entry_cur_tm.get() == '': |
| 67 | + entry_cur_tm.insert(0, "Eg. 2023-06-02 20:01") |
| 68 | + entry_cur_tm.config(fg = 'grey') |
| 69 | + |
| 70 | +entry_cur_tm.insert(0, "Eg. 2023-06-02 20:01") |
| 71 | +entry_cur_tm.bind('<FocusIn>', on_focus_in_ent_cur_tm) |
| 72 | +entry_cur_tm.bind('<FocusOut>', on_focus_out_ent_cur_tm) |
| 73 | +entry_cur_tm.config(fg = 'grey') |
| 74 | + |
| 75 | + |
| 76 | +def on_focus_in_ent_cnv_tm(event): |
| 77 | + if entry_cnv_tm.get() == 'Click convert button': |
| 78 | + entry_cnv_tm.delete(0, "end") |
| 79 | + entry_cnv_tm.insert(0, '') |
| 80 | + entry_cnv_tm.config(fg = 'black') |
| 81 | +def on_focus_out_ent_cnv_tm(event): |
| 82 | + if entry_cnv_tm.get() == '': |
| 83 | + entry_cnv_tm.insert(0, 'Click convert button') |
| 84 | + entry_cnv_tm.config(fg = 'grey') |
| 85 | + |
| 86 | +entry_cnv_tm.insert(0, 'Click convert button') |
| 87 | +entry_cnv_tm.bind('<FocusIn>', on_focus_in_ent_cnv_tm) |
| 88 | +entry_cnv_tm.bind('<FocusOut>', on_focus_out_ent_cnv_tm) |
| 89 | +entry_cnv_tm.bind("<Key>", lambda a: "break") |
| 90 | +entry_cnv_tm.config(fg = 'grey') |
| 91 | + |
| 92 | + |
| 93 | +#All label with text |
| 94 | +title_lbl=tk.Label(master=window,text="Time-Zone Converter",font=("broadway", 25),bg="black",fg='white') |
| 95 | +title_lbl.place(x=48,y=10) |
| 96 | + |
| 97 | +cur_tz_lbl=tk.Label(master=window,text="Current timezone name",font=("Times Roman", 10),fg='black') |
| 98 | +cur_tz_lbl.place(x=16,y=80) |
| 99 | +cnv_tz_lbl=tk.Label(master=window,text="Convert timezone name",font=("Times Roman", 10),fg='black') |
| 100 | +cnv_tz_lbl.place(x=246,y=80) |
| 101 | + |
| 102 | +cur_tm_lbl=tk.Label(master=window,text="Current time (YYYY-MM-DD H:M)",font=("Times Roman", 10),fg='black') |
| 103 | +cur_tm_lbl.place(x=16,y=150) |
| 104 | +cnv_tm_lbl=tk.Label(master=window,text="Converted time (YYYY-MM-DD H:M)",font=("Times Roman", 10),fg='black') |
| 105 | +cnv_tm_lbl.place(x=246,y=150) |
| 106 | + |
| 107 | +#Convert button function |
| 108 | +def cnv_time(): |
| 109 | + date_input = True |
| 110 | + |
| 111 | + #Check all the data is entered or not |
| 112 | + if entry_cur_tz.get() == 'Eg. Asia/Kolkata' or entry_cnv_tz.get() == 'Eg. Australia/Sydney' or entry_cur_tm.get() == 'Eg. 2023-06-02 20:01' or entry_cnv_tm == 'Click convert button': |
| 113 | + tkinter.messagebox.showerror(title="Error", message="Invalid inputs") |
| 114 | + #Check the date input format |
| 115 | + elif entry_cur_tm.get() != 'Eg. 2023-06-02 20:01': |
| 116 | + try: |
| 117 | + my_timestamp = datetime.strptime(entry_cur_tm.get(), '%Y-%m-%d %H:%M') |
| 118 | + except ValueError: |
| 119 | + date_input = False |
| 120 | + tkinter.messagebox.showerror(title="Error", message="Invalid Date input") |
| 121 | + #Check if timezone entered is right or wrong and convert the date |
| 122 | + if entry_cur_tz != 'Eg. Asia/Kolkata' and entry_cnv_tz != 'Eg. Australia/Sydney': |
| 123 | + try: |
| 124 | + if date_input == True: |
| 125 | + date_time = datetime.strptime(entry_cur_tm.get(), '%Y-%m-%d %H:%M') |
| 126 | + org_timezone = pytz.timezone(entry_cur_tz.get()) |
| 127 | + new_timezone = pytz.timezone(entry_cnv_tz.get()) |
| 128 | + org_timestamp = org_timezone.localize(date_time) |
| 129 | + new_timestamp = org_timestamp.astimezone(new_timezone) |
| 130 | + entry_cnv_tm.delete(0, "end") |
| 131 | + entry_cnv_tm.config(fg = 'black') |
| 132 | + entry_cnv_tm.insert(0, new_timestamp.strftime(format)) |
| 133 | + except pytz.exceptions.UnknownTimeZoneError: |
| 134 | + tkinter.messagebox.showerror(title="Error", message="Invalid timezone input") |
| 135 | + |
| 136 | +# Convert Button |
| 137 | +button_cnv = tk.Button(window, text="Convert", width=10, command=cnv_time) |
| 138 | +button_cnv.place(x=20,y=210) |
| 139 | + |
| 140 | +window.resizable(False, False) |
| 141 | +window.mainloop() |
0 commit comments