How To Get And Display Time Zones and Local Times In Python Tkinter
In this Python tutorial we will create a Table to display Various Time Zones With Their Corresponding Local Times using the Tkinter library for the graphical user interface.
The application consists of two main components: a ClockPanel that shows the current time and a ClockTable that lists time zones and their local times.
What We Are Gonna Use In This Project:
- Python Programming Language.- Tkinter for GUI.
- VS Code Editor.
- VS Code Editor.
Project Source Code:
import tkinter as tk
from tkinter import ttk
from datetime import datetime
import pytz #pip install pytz
class TimeZoneApp:
def __init__(self, root):
# Initialize the main application window
self.root = root
self.root.title("Time Zones")
self.root.geometry("500x500")
# Create instances of ClockPanel and ClockTable
self.ClockPanel = ClockPanel(root)
self.ClockTable = ClockTable(root)
# Pack ClockPanel and ClockTable widgets into the main window
self.ClockPanel.pack(side=tk.TOP, fill=tk.X)
self.ClockTable.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
class ClockPanel(tk.Canvas):
def __init__(self, master):
# Initialize ClockPanel as a Canvas with black background and height 100
super().__init__(master, bg="black", height=100)
self.time_format = "%I:%M:%S %p"
# Display the time in ClockPanel
self.display_time()
def display_time(self):
# Get the current time and format it
current_time = datetime.now().strftime(self.time_format)
# Clear the canvas and display the formatted time in orange
self.delete("all")
self.create_text(250,50, text=current_time, font=("Arial", 36, "bold"),
fill="orange")
# Schedule the display_time method to be called after 1000 milliseconds
# (1 second)
self.after(1000, self.display_time)
class ClockTable(ttk.Treeview):
def __init__(self, master):
# Initialize ClockTable as a Treeview with columns "Time Zone" and "Time"
super().__init__(master, columns=("Time Zone","Time"))
# Set the headings for the columns
self.heading("#0", text="Time Zone")
self.heading("Time Zone", text="Time Zone")
self.heading("Time", text="Time")
# Treeview Style
style = ttk.Style()
style.configure("Treeview", rowheight=25)
style.configure("Treeview", font=("Arial", 14))
style.configure("Treeview.Heading", font=("Arial", 17))
# Get all time zones and insert them into the Treeview
# along with the formatted time
time_zones = pytz.all_timezones
for time_zone in time_zones:
self.insert("", tk.END, values=(time_zone,
self.get_formatted_time(time_zone)))
# Show only the headings
self["show"] = "headings"
# Set the width for each column
self.column("#0", width=200)
self.column("Time Zone", width=200)
self.column("Time", width=200)
# Add a vertical scrollbar to the Treeview
vsb = tk.Scrollbar(self, orient="vertical", command=self.yview)
vsb.pack(side="right", fill="y")
self.configure(yscrollcommand=vsb.set)
def get_formatted_time(self, time_zone):
# Get the local time in the specified time zone and format it
loacl_time = datetime.now(pytz.timezone(time_zone))
return loacl_time.strftime("%I:%M:%S %p")
if __name__ == "__main__":
root = tk.Tk()
app = TimeZoneApp(root)
root.mainloop()