Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Live World Time Application using Python



Live world time project is a GUI application which has been coded using basic Python and library called Tkinter. The option provides an ability to input a continent and country to show the current local time and open Google Maps with the country's location on the map.

Required Libraries

First, ensure that you have the following libraries installed −

  • Tkinter − It comes with Python for defining and designing Graphical user interface applications.
  • pytz − This library is used for time zones management.
  • webbrowser − This library is used to make a web-browser open URLs.

Installation

The installation can be done either using the PIP or through direct command prompt. Below are the code statements to install required libraries.

To install tkinter, use the following command −

pip install tkinter

To install pytz, use the following command −

pip install pytz

You can get the webbrowser module from the official documentation from the command prompt as follows −

python -m webbrowser -t "http://www.python.org"

Steps to Create Live World Time Application

Step 1: Import Necessary Libraries

Use the following code statements to import the required libraries −

  • Import datetime for handling date and time.
  • Import pytz for time zone conversions.
  • Import tkinter and ttk for GUI elements.
  • Import webbrowser to open URLs.

Step 2: Initialize Tkinter Window

To initialize tkinter window −

  • Create a window using Tk().
  • Set the window's title and size.

Step 3: Define the Functions

Below are the functions that are using to get the different task done in the project −

  • open_map() − This function opens Google Maps based on the selected country.
  • update_map_and_time() − This function updates the displayed time and opens Google Maps.
  • update_countries(event) − This function updates the list of countries when a continent is selected.

Step 4: Create Dropdown Menus

You have to create dropdown menus for −

  • Continent selection.
  • Country selection, populated based on the continent.

Step 5: Display Current Time

To display the current time, use a label to show the local time of the selected country.

Step 6: Bind Functions to Events

Bind the following functions for −

  • Continent dropdown to update the country list.
  • Country dropdown to update the time and map.

Step 7: Run the Application

Use root.mainloop() to start the Tkinter event loop.

Code to Create Live World Time Application

from datetime import datetime
import pytz
from tkinter import *
from tkinter import ttk
import webbrowser

root = Tk()
root.title("OPEN World")
root.geometry("700x400")

# Function to open Google Maps with the selected location
def open_map():
   map_url = f"https://www.google.com/maps/search/?api=1&query={country_var.get().replace(' ', '+')}"
   webbrowser.open(map_url)

# Function to update the map and time display
def update_map_and_time():
   continent = continent_var.get()
   country = country_var.get()

   if continent == 'Asia':
      if country == 'India':
         timezone = 'Asia/Kolkata'
      elif country == 'China':
         timezone = 'Asia/Shanghai'
      elif country == 'Japan':
         timezone = 'Asia/Tokyo'
      elif country == 'Pakistan':
         timezone = 'Asia/Karachi'
      elif country == 'Bangladesh':
         timezone = 'Asia/Dhaka'
      else:
         return
   elif continent == 'Australia':
      timezone = 'Australia/Victoria'
   elif continent == 'Africa':
      if country == 'Nigeria':
         timezone = 'Africa/Lagos'
      elif country == 'Algeria':
         timezone = 'Africa/Algiers'
      else:
         return
   elif continent == 'America':
      if country == 'USA (West)':
         timezone = 'America/Los_Angeles'
      elif country == 'Argentina':
         timezone = 'America/Argentina/Buenos_Aires'
      elif country == 'Canada':
         timezone = 'America/Toronto'
      elif country == 'Brazil':
         timezone = 'America/Sao_Paulo'
      else:
         return
   elif continent == 'Europe':
      if country == 'UK':
         timezone = 'Europe/London'
      elif country == 'Portugal':
         timezone = 'Europe/Lisbon'
      elif country == 'Italy':
         timezone = 'Europe/Rome'
      elif country == 'Spain':
         timezone = 'Europe/Madrid'
      else:
         return
    
   # Update the time
   home = pytz.timezone(timezone)
   local_time = datetime.now(home)
   current_time = local_time.strftime("%H:%M:%S")
   clock_label.config(text=current_time)

   # Open Google Maps in the web browser
   open_map()

# Function to update the country options based on selected continent
def update_countries(event):
   continent = continent_var.get()

   if continent == 'Asia':
      countries = ['India', 'China', 'Japan', 'Pakistan', 'Bangladesh']
   elif continent == 'Australia':
      countries = ['Australia']
   elif continent == 'Africa':
      countries = ['Nigeria', 'Algeria']
   elif continent == 'America':
      countries = ['USA (West)', 'Argentina', 'Canada', 'Brazil']
   elif continent == 'Europe':
      countries = ['UK', 'Portugal', 'Italy', 'Spain']
   else:
      countries = []

   country_menu['values'] = countries
   country_var.set('')  # Reset country selection

# Continent Selection
continent_var = StringVar()
continent_var.set('Asia')  # Default value
continent_label = Label(root, text="Select Continent:", font=("Arial", 14))
continent_label.pack(pady=10)
continent_menu = ttk.Combobox(root, textvariable=continent_var, font=("Arial", 12), state="readonly", width=20)
continent_menu['values'] = ['Asia', 'Australia', 'Africa', 'America', 'Europe']
continent_menu.pack(pady=10)
continent_menu.bind('<<ComboboxSelected>>', update_countries)

# Country Selection
country_var = StringVar()
country_label = Label(root, text="Select Country:", font=("Arial", 14))
country_label.pack(pady=10)
country_menu = ttk.Combobox(root, textvariable=country_var, font=("Arial", 12), state="readonly", width=20)
country_menu.pack(pady=10)
country_menu.bind('<<ComboboxSelected>>', lambda event: update_map_and_time())

# Time Display
clock_label = Label(root, font=("Arial", 25, "bold"))
clock_label.pack(pady=20)

root.mainloop()

Output

GUI Window − A window titled "OPEN World" appears.

GUI Window

Continent Selection − The user selects a continent from the list

Continent Selection

We click Asia continent you can show Four country, now we clicked India

Continent Selection

Its show India real time and also it will automatically opened web browser for this location in Google

GMaps

Now we clicked Europe for Portugal.

Continent Selection Continent Selection

It will also open the google map.

google map

Time Display − The current local time of the selected country is displayed.

Time Display

Google Maps − The selected country's location opens in Google Maps

google maps

IF you want more country, you can add your country in the code.

python_reference.htm
Advertisements