英文:
Tkinter with openweather
问题
I tried creating a dropdown box in Tkinter. I was hoping that every time I selected a country and clicked onto the left button, it would display the weather of the country that I have selected. Apparently, I have to manually change this line of code (in the define weather function) weather_status = output['list'][2]['sys']
every time I want to get the country's weather. How do I get the country's weather without having to change that line of code manually?
英文:
I tried creating a dropdown box in Tkinter. I was hoping that everytime i selected a country and click onto the left button, it'll display the weather of the country that I have selected. Apparently, I have to manually change this line of code(in the define weather function) weather_status = output['list'][2]['sys']
every time i want to get the country's weather. How do I get the country's weathers without having to change that line of code manually?
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()
答案1
得分: 3
如果我理解了你的问题,你可以通过“追踪”对名为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))
然后,通过调用这个修改过的版本来添加追踪的代码:
...
## 添加一个下拉框
OptionList= [
'Singapore',
'London',
'Bangkok',
'Moscow'
]
city_list = StringVar(window)
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()
...
英文:
If I have understood your question, you can do it by "tracing" changes to the StringVar
named city_list
and having it call the weather()
function everytime its value is changed.
Tracing is described in the document The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar).
To make it so you don't have to change the weather_status = output['list'][2]['sys']
statement in the weather()
function, you can just make it reference the current city
value you already have:
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))
And then add the code to do the tracing by calling this modified version of it:
...
## 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()
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论