- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在 Tkinter 中创建一个下拉框。我希望每次我选择一个国家并单击左侧按钮时,它都会显示我选择的国家的天气。显然,我必须手动更改这行代码(在定义天气函数中)weather_status = output['list'][2]['sys']
每次我想了解该国的天气。如何在无需手动更改该行代码的情况下获取该国家/地区的天气?
import requests
import tkinter as tk
from tkinter import Tk, BOTH, Menu, StringVar, messagebox, Label
def weather():
city = city_list.get()
url = 'http://api.openweathermap.org/data/2.5/group?id=1880252,524901,703448,2643743&units=metric&appid=de9880142c6480331da965bf187a4b6e'.format(city)
req = requests.get(url)
output = req.json()
weather_status = output['list'][2]['sys']
weather_status_label.configure(text = 'weather status: ' + str(weather_status))
window = tk.Tk()
window.geometry('400x350')
## adding a drop box
OptionList= [
'Singapore',
'London',
'Bangkok',
'Moscow'
]
city_list = StringVar(window, OptionList)
city_list.set('select the city')
opt = tk.OptionMenu(window, city_list, *OptionList)
opt.config(width = 90, font = ('Arial', 12))
opt.pack()
## adding a label on top
lbl = tk.Label(window, text = 'Select your city', bg = 'light green')
lbl.pack()
## adding buttons
def win_quit():
if messagebox.askokcancel('quit','Do you want to quit?'):
window.destroy()
button_1 = tk.Button(window, text = 'Quit', height = 2,
width = 4, command = win_quit, activeforeground = 'red',
relief = 'ridge')
button_1.place(x= 300, y= 60)
button_2 = tk.Button(window, text = 'Click', height = 2,
width = 4, command = weather, activeforeground = 'green',
relief = 'ridge')
button_2.place(x= 70, y= 60)
## adding another label
weather_status_label = Label(window, font=('Arial', 10,'bold' ))
weather_status_label.pack(padx= 10, pady= 60)
window.mainloop()
最佳答案
如果我理解了您的问题,您可以通过“跟踪”名为 city_list
的 StringVar
的更改并让它调用 weather()
每次其值更改时都会起作用。
文档The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar)中描述了跟踪.
这样您就不必更改 weather()
中的 weather_status = output['list'][2]['sys']
语句> 函数,您可以让它引用您已有的当前 city
值:
def weather():
city = city_list.get()
url = 'http://api.openweathermap.org/data/2.5/group?id=1880252,524901,703448,2643743&units=metric&appid=de9880142c6480331da965bf187a4b6e'.format(city)
req = requests.get(url)
output = req.json()
# weather_status = output['list'][2]['sys'] # CHANGED.
weather_status = city
weather_status_label.configure(text = 'weather status: ' + str(weather_status))
然后添加代码以通过调用其修改版本来进行跟踪:
...
## adding a drop box
OptionList= [
'Singapore',
'London',
'Bangkok',
'Moscow'
]
city_list = StringVar(window, OptionList)
city_list.set('select the city')
observer = city_list.trace('w', lambda *_: weather()) # ADDED.
opt = tk.OptionMenu(window, city_list, *OptionList)
opt.config(width = 90, font = ('Arial', 12))
opt.pack()
...
关于python - Tkinter 与 openweather,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580138/
你好,我尝试遵循这个manual采取Python开放天气API 这里是示例手册代码: import openweather from datetime import datetime # create
我制作了一个使用 p5.js 的天气预报网络应用程序。它在我的本地主机上完美运行。为什么这个问题不断出现在 github 页面上: Mixed Content: The page at '' was
我尝试在 Tkinter 中创建一个下拉框。我希望每次我选择一个国家并单击左侧按钮时,它都会显示我选择的国家的天气。显然,我必须手动更改这行代码(在定义天气函数中)weather_status = o
我刚开始学习 Swift 和 iOS,我的天气应用程序出现问题。我正在尝试获取天气数据并在控制台中打印,但仍然是零,我不知道可能出了什么问题。我试图更改模型,粘贴所有 url,但仍然为零。有时app发
这个问题已经有答案了: What is the scope of variables in JavaScript? (27 个回答) 已关闭 3 年前。 我正在构建一个时间和天气应用程序。我有两个功能
我读过很多类似的主题,但它们没有帮助.. 尝试访问此 JSON 对象.. { "coord": { "lon": -83.9, "lat": 33.99
如何让 mu openweather API 与地理定位一起使用?这是我当前的 html 代码:
我找到并修改了简单的代码,以使用 openweather 和 json 格式在 Python 中获取天气状况。但是我有一个问题——我怎么能说这个城市是不正确的呢? 我的意思是,即使我经过了一个错误的、
好吧,我遇到了一些情况,并且在理解如何从 API 返回的 JSON 中提取值方面遇到了障碍。我有以下代码: import requests import json weather_results =
我想从 Openweather API 解析 JSON。 Openweather URL 是:http://samples.openweathermap.org/data/2.5/weather?q=
我的任务是通过 API 显示天气预报。 我有以下代码,但无法显示数据。我今天刚刚开始学习 AngularJS 并使用 API,因此我们将不胜感激!具体来说,我的代码有什么问题导致天气数据无法显示? 这
我正在制作一个使用地理位置和搜索功能的天气应用程序。 地理定位功能正常,但用户搜索失败;控制台中的错误是:GET {api url} 404(未找到)。 首先,不起作用的功能: functio
我正在编写一些 JavaScript 以从包含名称、经度、纬度和 openweather API 调用的 JSON 中提取信息。我需要的是将 API 信息从 API 调用中获取到 HTML 页面中,以
// MODULE var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngResource']); // Directives we
我正在创建一个应用程序,该应用程序使用 JSON 从 OpenWeather 的 API 中提取天气信息。我嵌套了“IF”语句,用于构建当前天气的字符串。目标是构建一串“当前天气(描述),温度为 (t
我正在使用 Swift 调用 Openweather map API,我需要从响应中返回一个特定值作为字符串。 但是,当我尝试返回值时出现错误,因为 JSON 不可转换为字符串。 func callW
我正在尝试使用 Android studio 和 Java 构建一个简单的天气预报应用程序。我按照此处的一些说明( https://www.androdocs.com/java/creating-an
我正在使用 DataTables并想在其中显示我的城市数据,但我没能做到。 例如如果我调用this URL然后我以 JSON 格式获取城市数据(来自 openweathers map api)。我只想
extern crate openweather; use openweather::LocationSpecifier; static API_KEY: &str = "e85e0a3142231d
我从 openweather api 获取一个对象,该对象包含 40 个对象的数组,其中包含 5 天的天气预报,间隔 3 小时。我在 Angular 2 应用程序中显示这 5 天的天气预报时遇到问题。
我是一名优秀的程序员,十分优秀!