Skip to content

Commit e01b704

Browse files
Merge pull request #1643 from Volcano-Dragon/master
[GSSOC'23] Time Zone converter in Tkinter GUI
2 parents 4de8a7c + 6b17a8c commit e01b704

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

Timezone Converter GUI/Readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Package/Script Name
2+
3+
To convert the given time and date into a selected timezone, the user needs to provide the current timezone, converted timezone, and current time
4+
5+
## Setup instructions
6+
7+
Install the following modules <br>
8+
import datetime <br>
9+
import tkinter <br>
10+
import tkinter.messagebox <br>
11+
import pytz
12+
13+
## Detailed explanation of script, if needed
14+
15+
A script file's comments explain what is happening
16+
17+
## Author(s)
18+
19+
Volcano-Dragon
20+
21+
## Output
22+
![image](https://github.com/Volcano-Dragon/Amazing-Python-Scripts/assets/93902835/dba692d9-fffc-428f-b4d6-15cb39fffbba)
23+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)