Tkinter与openweather

huangapple go评论74阅读模式
英文:

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

Tkinter与openweather

答案1

得分: 3

如果我理解了你的问题,你可以通过“追踪”对名为city_listStringVar的更改,并在其值更改时调用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()
...

huangapple
  • 本文由 发表于 2020年1月3日 22:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定