2017-05-23 5 views
0

私の悪い英語の最初の申し訳ありません。Kivy label.textの出力dict値

私はAPIについて学習しており、天気APIを非常に基本的なKIVYインターフェイスに実装することに決めました。しかし、Imは天気に関する情報を含む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() 

KV

<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

トレースバックとは何ですか? – Kanak

答えて

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キーがないのでエラーが表示されるため、自分で確認する必要があります。

+0

ありがとうございます!それは働いた –

+0

問題はありません!他人に見えるように私の答えを受け入れられた答えとしてください。 –

関連する問題