首先抱歉我的英语不好。
我正在学习API,我决定在非常基本的KIVY界面上实现天气API。但是,我无法获得包含有关天气信息的Dict的输出值,作为标签文本。我认为这个值不是字符串。你们帮帮我吗?
class Tempo_Func(BoxLayout):
def tempo(self,cidade):
req=requests.get('http://api.openweathermap.org/data/2.5/weather?q='
+cidade+ '&appid=mykey')
self.tempo = json.loads(req.text)
self.x =(self.tempo['weather'][0]['main'])
class WeatherApp(App):
def build(self):
return Tempo_Func()
if __name__=='__main__':
WeatherApp().run()
<Tempo_Func>:
orientation:'vertical'
padding:10
spacing:10
BoxLayout:
orientation:'vertical'
Label:
text:"Qual sua cidade?"
size_hint_y:None
TextInput:
id:entry
Button:
text:'Procurar'
on_press:root.tempo(entry.text)
Label:
text:root.x #I want to show the value of self.x here!!
答案 0 :(得分:0)
将.kv文件更改为:
<Tempo_Func>:
orientation: 'vertical'
padding: 10
spacing: 10
Label:
text: "Qual sua cidade?"
size_hint_y: None
TextInput:
id: entry
Button:
text: 'Procurar'
on_press: root.tempo(entry.text)
Label:
text: str(root.x)
这似乎有效。我收到错误,因为我没有API密钥所以你必须检查自己。