2013-11-28 57 views
0

我有一個問題的條目。我希望用戶在
API鏈接中寫任何他們想要的城市。我知道我不能轉換爲str。所以,當過用戶選擇進入一個城市,他只需要點擊按鈕的預測後,他在進入進入城市和天氣將印:API tkinter python條目

from tkinter import * 
import requests 
import json 

class Application(Frame): 

    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.root = master 
     self.pack() 
     self.create_widgets() 

    def create_widgets(self): 
     self.v = StringVar() 
     self.e = Entry(self, textvariable=self.v) 
     self.e.pack(side="left") 

     self.v.set("Enter City") 
     self.s = self.v.get() 
     self.e.focus_set() 


     self.butn = Button(self) 
     self.butn["text"] = "Forecast" 
     self.butn["command"] = self.make_request 
     self.butn.pack(side="left") 

     self.QUIT = Button(self, text="QUIT", command=self.root.destroy) 
     self.QUIT.pack(side="right") 


    def make_request(self): 
     r = requests.get("http://api.wunderground.com/api/ab78bcbaca641959/forecast/q/Sweden/" + ???? + ".json") 
     data = r.json() 
     for day in data['forecast']['simpleforecast']['forecastday']: 
      print (day['date']['weekday'] + ":") 
      print ("Conditions: ", day['conditions']) 
      print ("High: ", day['high']['celsius'] + "C", "Low: ", day['low']['celsius'] + "C", '\n') 
     return data 






rot = Tk() 
rot.geometry("900x650+200+50") 
rot.title("The Trip") 

app = Application(master=rot) 
app.mainloop() 

回答

0

使用self.v.get()代替????和它的作品

r = requests.get("http://api.wunderground.com/api/ab78bcbaca641959/forecast/q/Sweden/" + self.v.get() + ".json") 

順便說一句:你不需要self.s = self.v.get()了。