使用 Tkinter 模块的Python天气应用程序
在本文中,我们将讨论如何使用tkinter创建天气应用程序。 GUI 应用程序将告诉我们特定城市的当前天气以及温度详细信息和其他详细信息。
所需模块:
- Tkinter:它是一个内置的Python库,用于使用tkinter工具包制作 GUI。
- 请求:它是一个库,可帮助在 URL 的帮助下获取数据。可以使用以下命令安装它:
pip install requests
方法:
首先,我们必须使用天气 API 通过生成 API 密钥从 Open Weather Map 网站获取数据,然后我们需要创建一个配置文件来存储密钥。最后在Python脚本中使用该配置文件。
生成 API 密钥的步骤:
- 登录 Open Weather Map
- 转到API部分。然后在Current Weather Data部分单击Api doc 。
- 现在在API 调用部分,我们有api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}的链接
- 单击链接上的API 密钥,它将定向到您可以从中获取密钥的页面。生成的密钥如下所示:
创建配置文件的步骤:
- 创建一个名为 config.ini 的文件。
- 写键名 在这里[gfg] 中用括号括起来。
- 创建一个变量键(这里是键)并粘贴您复制的键,如下所示:
创建Python脚本的步骤:
- 导入模块。
Python3
# import required modules
from configparser import ConfigParser
import requests
from tkinter import *
from tkinter import messagebox
Python3
# create object
app = Tk()
# add title
app.title("Weather App")
# adjust window size
app.geometry("300x300")
# add labels, buttons and text
city_text = StringVar()
city_entry = Entry(app, textvariable=city_text)
city_entry.pack()
Search_btn = Button(app, text="Search Weather",
width=12, command=search)
Search_btn.pack()
location_lbl = Label(app, text="Location",
font={'bold', 20})
location_lbl.pack()
temperature_label = Label(app, text="")
temperature_label.pack()
weather_l = Label(app, text="")
weather_l.pack()
app.mainloop()
Python3
# extract key from the
# configuration file
config_file = "config.ini"
config = ConfigParser()
config.read(config_file)
api_key = config['gfg']['api']
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid={}'
Python3
# explicit function to get
# weather details
def getweather(city):
result = requests.get(url.format(city, api_key))
if result:
json = result.json()
city = json['name']
country = json['sys']
temp_kelvin = json['main']['temp']
temp_celsius = temp_kelvin-273.15
weather1 = json['weather'][0]['main']
final = [city, country, temp_kelvin,
temp_celsius, weather1]
return final
else:
print("NO Content Found")
Python3
# explicit function to
# search city
def search():
city = city_text.get()
weather = getweather(city)
if weather:
location_lbl['text'] = '{} ,{}'.format(weather[0], weather[1])
temperature_label['text'] = str(weather[3])+" Degree Celsius"
weather_l['text'] = weather[4]
else:
messagebox.showerror('Error', "Cannot find {}".format(city))
Python3
# import required modules
from configparser import ConfigParser
import requests
from tkinter import *
from tkinter import messagebox
# extract key from the
# configuration file
config_file = "config.ini"
config = ConfigParser()
config.read(config_file)
api_key = config['gfg']['api']
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid={}'
# explicit function to get
# weather details
def getweather(city):
result = requests.get(url.format(city, api_key))
if result:
json = result.json()
city = json['name']
country = json['sys']
temp_kelvin = json['main']['temp']
temp_celsius = temp_kelvin-273.15
weather1 = json['weather'][0]['main']
final = [city, country, temp_kelvin,
temp_celsius, weather1]
return final
else:
print("NO Content Found")
# explicit function to
# search city
def search():
city = city_text.get()
weather = getweather(city)
if weather:
location_lbl['text'] = '{} ,{}'.format(weather[0], weather[1])
temperature_label['text'] = str(weather[3])+" Degree Celsius"
weather_l['text'] = weather[4]
else:
messagebox.showerror('Error', "Cannot find {}".format(city))
# Driver Code
# create object
app = Tk()
# add title
app.title("Weather App")
# adjust window size
app.geometry("300x300")
# add labels, buttons and text
city_text = StringVar()
city_entry = Entry(app, textvariable=city_text)
city_entry.pack()
Search_btn = Button(app, text="Search Weather",
width=12, command=search)
Search_btn.pack()
location_lbl = Label(app, text="Location", font={'bold', 20})
location_lbl.pack()
temperature_label = Label(app, text="")
temperature_label.pack()
weather_l = Label(app, text="")
weather_l.pack()
app.mainloop()
- 我们必须首先在tkinter的帮助下制作 GUI 的主体。
蟒蛇3
# create object
app = Tk()
# add title
app.title("Weather App")
# adjust window size
app.geometry("300x300")
# add labels, buttons and text
city_text = StringVar()
city_entry = Entry(app, textvariable=city_text)
city_entry.pack()
Search_btn = Button(app, text="Search Weather",
width=12, command=search)
Search_btn.pack()
location_lbl = Label(app, text="Location",
font={'bold', 20})
location_lbl.pack()
temperature_label = Label(app, text="")
temperature_label.pack()
weather_l = Label(app, text="")
weather_l.pack()
app.mainloop()
- 读取 config.ini 文件,然后在程序中加载密钥和 URL。
蟒蛇3
# extract key from the
# configuration file
config_file = "config.ini"
config = ConfigParser()
config.read(config_file)
api_key = config['gfg']['api']
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid={}'
- 使用getweather()函数获取特定位置的天气。
蟒蛇3
# explicit function to get
# weather details
def getweather(city):
result = requests.get(url.format(city, api_key))
if result:
json = result.json()
city = json['name']
country = json['sys']
temp_kelvin = json['main']['temp']
temp_celsius = temp_kelvin-273.15
weather1 = json['weather'][0]['main']
final = [city, country, temp_kelvin,
temp_celsius, weather1]
return final
else:
print("NO Content Found")
- 搜索函数,以便我们可以获取输出天气详细信息。
蟒蛇3
# explicit function to
# search city
def search():
city = city_text.get()
weather = getweather(city)
if weather:
location_lbl['text'] = '{} ,{}'.format(weather[0], weather[1])
temperature_label['text'] = str(weather[3])+" Degree Celsius"
weather_l['text'] = weather[4]
else:
messagebox.showerror('Error', "Cannot find {}".format(city))
下面是完整的程序:
蟒蛇3
# import required modules
from configparser import ConfigParser
import requests
from tkinter import *
from tkinter import messagebox
# extract key from the
# configuration file
config_file = "config.ini"
config = ConfigParser()
config.read(config_file)
api_key = config['gfg']['api']
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid={}'
# explicit function to get
# weather details
def getweather(city):
result = requests.get(url.format(city, api_key))
if result:
json = result.json()
city = json['name']
country = json['sys']
temp_kelvin = json['main']['temp']
temp_celsius = temp_kelvin-273.15
weather1 = json['weather'][0]['main']
final = [city, country, temp_kelvin,
temp_celsius, weather1]
return final
else:
print("NO Content Found")
# explicit function to
# search city
def search():
city = city_text.get()
weather = getweather(city)
if weather:
location_lbl['text'] = '{} ,{}'.format(weather[0], weather[1])
temperature_label['text'] = str(weather[3])+" Degree Celsius"
weather_l['text'] = weather[4]
else:
messagebox.showerror('Error', "Cannot find {}".format(city))
# Driver Code
# create object
app = Tk()
# add title
app.title("Weather App")
# adjust window size
app.geometry("300x300")
# add labels, buttons and text
city_text = StringVar()
city_entry = Entry(app, textvariable=city_text)
city_entry.pack()
Search_btn = Button(app, text="Search Weather",
width=12, command=search)
Search_btn.pack()
location_lbl = Label(app, text="Location", font={'bold', 20})
location_lbl.pack()
temperature_label = Label(app, text="")
temperature_label.pack()
weather_l = Label(app, text="")
weather_l.pack()
app.mainloop()
输出: