使用 Python 開發的即時世界時間應用程式



即時世界時間專案是一個 GUI 應用程式,使用基本的 Python 和名為 Tkinter 的庫進行編碼。該選項提供了一個輸入大陸和國家以顯示當前本地時間並在地圖上開啟 Google 地圖顯示該國家/地區位置的功能。

所需庫

首先,確保已安裝以下庫:

  • **Tkinter** - 它與 Python 一起提供,用於定義和設計圖形使用者介面應用程式。
  • **pytz** - 此庫用於時區管理。
  • **webbrowser** - 此庫用於開啟 URL 的網頁瀏覽器。

安裝

可以使用 PIP 或透過直接命令提示符進行安裝。以下是安裝所需庫的程式碼語句。

要安裝 tkinter,請使用以下命令:

pip install tkinter

要安裝 pytz,請使用以下命令:

pip install pytz

您可以從命令提示符下官方文件獲取 webbrowser 模組,如下所示:

python -m webbrowser -t "https://python.club.tw"

建立即時世界時間應用程式的步驟

步驟 1:匯入必要的庫

使用以下程式碼語句匯入所需的庫:

  • 匯入 datetime 用於處理日期和時間。
  • 匯入 pytz 用於時區轉換。
  • 匯入 tkinter 和 ttk 用於 GUI 元素。
  • 匯入 webbrowser 以開啟 URL。

步驟 2:初始化 Tkinter 視窗

要**初始化 tkinter 視窗**:

  • 使用 Tk() 建立一個視窗。
  • 設定視窗的標題和大小。

步驟 3:定義函式

以下是用於在專案中完成不同任務的函式:

  • **open_map()** - 此函式根據所選國家/地區開啟 Google 地圖。
  • **update_map_and_time()** - 此函式更新顯示的時間並開啟 Google 地圖。
  • **update_countries(event)** - 此函式在選擇大洲時更新國家/地區列表。

步驟 4:建立下拉選單

您必須建立以下下拉選單:

  • 選擇大陸。
  • 國家/地區選擇,根據大陸填充。

步驟 5:顯示當前時間

要顯示當前時間,請使用標籤顯示所選國家/地區的本地時間。

步驟 6:將函式繫結到事件

為以下內容繫結函式:

  • 大陸下拉選單以更新國家/地區列表。
  • 國家/地區下拉選單以更新時間和地圖。

步驟 7:執行應用程式

使用 root.mainloop() 啟動 Tkinter 事件迴圈。

建立即時世界時間應用程式的程式碼

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()

輸出

**GUI 視窗** - 將出現一個標題為“OPEN World”的視窗。

GUI Window

**大陸選擇** - 使用者從列表中選擇一個大陸

Continent Selection

我們點選亞洲大陸,您可以顯示四個國家/地區,現在我們點選印度

Continent Selection

它顯示印度的即時時間,並且還會自動為 Google 中的此位置開啟網路瀏覽器

GMaps

現在我們點選歐洲的葡萄牙。

Continent Selection Continent Selection

它也會開啟谷歌地圖。

google map

**時間顯示** - 顯示所選國家/地區的當前本地時間。

Time Display

**Google 地圖** - 所選國家/地區的位置在 Google 地圖中開啟

google maps

如果您想要更多國家/地區,可以在程式碼中新增您的國家/地區。

python_reference.htm
廣告