Skip to content

Instantly share code, notes, and snippets.

@AlfredoTigolo
Last active May 20, 2021 00:59
Show Gist options
  • Save AlfredoTigolo/4155fd12963bea5b822ea49d946e6f94 to your computer and use it in GitHub Desktop.
Save AlfredoTigolo/4155fd12963bea5b822ea49d946e6f94 to your computer and use it in GitHub Desktop.
Python Clock Timer GUI example
'''
[email protected]
I first installed the python-dateutil package. With this package, we can import the modules dateutil and tz to help
assist us in getting the different time zones in different regions of the world.
Using the datetime and time class, we can obtain our current time zone and us strftime class to help with formatting
month, day, year, time, hours, minutes, and seconds
Once we have some examples of different time zone regions, we can start using python graphic widgets like Tk(), label,
and radiobutton to help change the different time zone regions
pip install python-dateutil
https://pypi.org/project/python-dateutil/
https://dateutil.readthedocs.io/en/stable/tz.html
https://docs.python.org/3/library/datetime.html#datetime.tzinfo
https://en.wikipedia.org/wiki/List_of_time_zones_by_country
'''
from tkinter import *
from datetime import datetime
from dateutil import tz
import dateutil
import datetime
import time
from time import strftime
#======= Configuring window =========
window = Tk()
window.title("Python Time Zone Clock")
window.geometry("1000x200")
window.configure(bg="navy")
clock_label = Label(window, bg="teal", fg="cyan", font = ("Arial", 30), relief='flat')
clock_label.place(x = 20, y = 20)
v = IntVar()
v.set(1)
def changeTimeZone():
current_time=""
if ( 5 == v.get() ):
z = radio5['text']
current_time = z + "\n" + datetime.datetime.now(dateutil.tz.gettz('Cuba')).strftime("%A %B %d %Y %I: %M: %S.%f %p")
elif ( 4 == v.get() ):
z = radio4['text']
current_time = z + "\n" + datetime.datetime.now(dateutil.tz.gettz('Central Standard Time')).strftime("%A %B %d %Y %I: %M: %S.%f %p")
elif ( 3 == v.get() ):
z = radio3['text']
current_time = z + "\n" + datetime.datetime.now(dateutil.tz.gettz('Australia/Tasmania')).strftime("%A %B %d %Y %I: %M: %S.%f %p")
elif ( 2==v.get() ):
z = radio2['text']
current_time = z + "\n" + datetime.datetime.now(dateutil.tz.gettz('Europe/Vienna')).strftime("%A %B %d %Y %I: %M: %S.%f %p")
elif ( 1==v.get() ):
z = radio1['text']
current_time = z + "\n" + datetime.datetime.now().strftime("%A %B %d %Y %I: %M: %S.%f %p")
else:
tz = entry1.get()
current_time = tz + "\n" + datetime.datetime.now(dateutil.tz.gettz(tz)).strftime("%A %B %d %Y %I: %M: %S.%f %p")
clock_label.configure(text = current_time)
clock_label.after(80, changeTimeZone)
def changeTimeManual():
current_time=""
try:
tz = entry1.get()
#print ( tz )
current_time = tz + "\n" + datetime.datetime.now(dateutil.tz.gettz(tz)).strftime("%A %B %d %Y %I: %M: %S.%f %p")
clock_label.configure(text = current_time)
clock_label.after(80, changeTimeManual )
except Exception as e:
print ( e + " Changing Time" )
radio1 = Radiobutton(window, text="Pacific Standard Time", variable=v, value=1,
command = changeTimeZone)
radio1.place(x=825, y=50)
radio2 = Radiobutton(window, text="Europe/Vienna", variable=v, value=2,
command = changeTimeZone)
radio2.place(x=825, y=75)
radio3 = Radiobutton(window, text="Australia/Tasmania", variable=v, value=3,
command = changeTimeZone)
radio3.place(x=825, y=100)
radio4 = Radiobutton(window, text="Central Standard Time", variable=v, value=4,
command = changeTimeZone)
radio4.place(x=825, y=125)
radio5 = Radiobutton(window, text="Cuba", variable=v, value=5,
command = changeTimeZone)
radio5.place(x=825, y=150)
entry1 = Entry(window, text=v, width=100)
#entry1.bind("<Return>", changeTimeManual )
entry1.place(x=825, y=175)
changeBtn = Button(window, text="Change", command = changeTimeManual)
changeBtn.place(x=750, y=175)
changeTimeZone()
window.mainloop()
'''
[email protected]
Using datetime, dateutil, tz, and time modules, we can have the user to select
different time zones using a try keyboard interrupt expection to interrupt the
while true loop, the user can select the time zones in a dictionary
'''
# https://en.wikipedia.org/wiki/List_of_time_zones_by_country
import datetime
import time
from dateutil import tz
import dateutil
from time import strftime
current_time = ""
region = 1
tz = {
1:'Cuba',
2:'Central Standard Time',
3:'Australia/Tasmania',
4:'Europe/Vienna',
5:'Pacific Standard Time',
6:'Eastern Standard Time',
7:'Chamorro Time Zone'
}
def selectRegion(int):
global current_time
global tz
#print ( tz )
print ( " Current time zone : " + tz[int] +"\r")
current_time = datetime.datetime.now(dateutil.tz.gettz(tz[int])).strftime("%A %B %d %Y %I: %M: %S.%f %p")
#current_time = datetime.datetime.now().strftime("%A %B %d %Y %I: %M: %S.%f %p")
while True:
try:
time.sleep(2.0)
selectRegion(region)
print ("Press Ctrl-C to change time zone", end="\r")
print("\n" + current_time, end="\r")
except KeyboardInterrupt as k:
print ( "select region\r")
region = int(input( tz ))
selectRegion(region)
from time import strftime
from tkinter import Label, Tk
#======= Configuring window =========
window = Tk()
window.title("")
window.geometry("200x150")
window.configure(bg="blue")
clock_label = Label(window, bg="green", fg="cyan", font = ("Arial", 30), relief='flat')
clock_label.place(x = 20, y = 20)
def update_label():
current_time = strftime('%H: %M: %S')
clock_label.configure(text = current_time)
clock_label.after(80, update_label)
def countdown(count): # one minute count down
# change text in label
clock_label['text'] = count
if count > 0:
# call countdown again after 1000ms (1s)
window.after(1000, countdown, count-1)
def countdown2(t):
# change text in labal
#mins, secs = divmod(t, 60)
#timer = '{:02d}:{:02d}'.format(mins, secs)
#clock_label['text'] = timer
#while t > 0:
if t > 0:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
clock_label['text'] = timer
#print(timer, end="\r")
#time.sleep(1)
window.after(1000, countdown2, t-1)
t -= 1
#update_label() #clock
#countdown2(int(60)) # one minute
countdown2(int(300)) # 5 minute
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment