📜  创建一个 GUI 以使用Python获取日落和日出时间。

📅  最后修改于: 2022-05-13 01:54:46.845000             🧑  作者: Mango

创建一个 GUI 以使用Python获取日落和日出时间。

先决条件: Tkinter 模块

在本文中,我们将编写Python脚本来获取日落和日出时间并将其与 GUI 应用程序绑定。在开始之前我们需要了解suntimegeopy模块

需要的模块:

suntime 模块:该模块将返回日落和日出时间计算Python库。在终端中运行此命令进行安装。

pip install suntime

geopy 模块:该模块使用第三方地理编码器和其他数据源定位全球地址、城市、国家和地标的坐标。 Nominatim 是用于 OpenStreetMap 数据的地理编码器。在终端中运行此命令进行安装。

pip install geopy

方法:

  • 导入所有需要的模块。
  • 使用 geopy 获取纬度和经度。
  • 使用Sun(latitude, longitude).get_sunrise_time()方法获取日出时间。
  • 使用Sun(latitude, longitude).get_sunset_time()方法获取黄昏时间。

下面是实现:

Python3
# import required modules
import datetime
from suntime import Sun
from geopy.geocoders import Nominatim
  
# Nominatim API to get latitude and longitude
geolocator = Nominatim(user_agent="geoapiExercises")
  
# input place
place = "delhi"
location = geolocator.geocode(place)
  
# latitude and longitude fetch
latitude = location.latitude
longitude = location.longitude
sun = Sun(latitude, longitude)
  
# date in your machine's local time zone
time_zone = datetime.date(2020, 9,13)
sun_rise = sun.get_local_sunrise_time(time_zone)
sun_dusk = sun.get_local_sunset_time(time_zone)
  
# display
print("Sun rise at : ", sun_rise.strftime('%H:%M'))
print("Dusk at : ",sun_dusk.strftime('%H:%M'))


Python3
# import modules
from tkinter import *
from geopy.geocoders import Nominatim
import datetime
from suntime import Sun
  
# user defined funtion
def sun():
      
    try:
          
        geolocator = Nominatim(user_agent="geoapiExercises")
         
        ladd1 = str(e.get())
        location1 = geolocator.geocode(ladd1)
          
        latitude = location1.latitude
        longitude = location1.longitude
          
        sun = Sun(latitude, longitude)
  
        time_zone = datetime.datetime.now()
          
        sun_rise = sun.get_local_sunrise_time(time_zone)
        sun_dusk = sun.get_local_sunset_time(time_zone)
          
        res_rise = sun_rise.strftime('%H:%M')
        res_dusk = sun_dusk.strftime('%H:%M')
          
        result1.set(res_rise)
        result2.set(res_dusk)
      
    except:
        result1.set("oops! something get wrong")
  
# object of tkinter
# and background set to light grey
master = Tk()
master.configure(bg='light grey')
master.title("Sun")
  
# Variable Classes in tkinter
result1 = StringVar();
result2 = StringVar();
  
  
# Creating label for each information
# name using widget Label 
Label(master, text="Enter place : " ,
      bg = "light grey").grid(row=1, sticky=W)
Label(master, text="Sunrise :",
      bg = "light grey").grid(row=3, sticky=W)
Label(master, text="Dusk :",
      bg = "light grey").grid(row=4, sticky=W)
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=result1,
      bg = "light grey").grid(row=3,column=1, sticky=W)
Label(master, text="", textvariable=result2,
      bg = "light grey").grid(row=4,column=1, sticky=W)
  
  
e = Entry(master,width = 50)
e.grid(row=1, column=1)
  
  
# creating a button using the widget  
b = Button(master, text="Check", 
           command=sun, bg = "white")
b.grid(row=1, column=2,columnspan=2, 
       rowspan=2,padx=5, pady=5,)
  
mainloop()


输出:

Sun rise at :  06:05
Dusk at :  18:28

与 GUI 应用程序绑定

蟒蛇3

# import modules
from tkinter import *
from geopy.geocoders import Nominatim
import datetime
from suntime import Sun
  
# user defined funtion
def sun():
      
    try:
          
        geolocator = Nominatim(user_agent="geoapiExercises")
         
        ladd1 = str(e.get())
        location1 = geolocator.geocode(ladd1)
          
        latitude = location1.latitude
        longitude = location1.longitude
          
        sun = Sun(latitude, longitude)
  
        time_zone = datetime.datetime.now()
          
        sun_rise = sun.get_local_sunrise_time(time_zone)
        sun_dusk = sun.get_local_sunset_time(time_zone)
          
        res_rise = sun_rise.strftime('%H:%M')
        res_dusk = sun_dusk.strftime('%H:%M')
          
        result1.set(res_rise)
        result2.set(res_dusk)
      
    except:
        result1.set("oops! something get wrong")
  
# object of tkinter
# and background set to light grey
master = Tk()
master.configure(bg='light grey')
master.title("Sun")
  
# Variable Classes in tkinter
result1 = StringVar();
result2 = StringVar();
  
  
# Creating label for each information
# name using widget Label 
Label(master, text="Enter place : " ,
      bg = "light grey").grid(row=1, sticky=W)
Label(master, text="Sunrise :",
      bg = "light grey").grid(row=3, sticky=W)
Label(master, text="Dusk :",
      bg = "light grey").grid(row=4, sticky=W)
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=result1,
      bg = "light grey").grid(row=3,column=1, sticky=W)
Label(master, text="", textvariable=result2,
      bg = "light grey").grid(row=4,column=1, sticky=W)
  
  
e = Entry(master,width = 50)
e.grid(row=1, column=1)
  
  
# creating a button using the widget  
b = Button(master, text="Check", 
           command=sun, bg = "white")
b.grid(row=1, column=2,columnspan=2, 
       rowspan=2,padx=5, pady=5,)
  
mainloop()

输出: