【问题标题】:Output dict value on Kivy label.text在 Kivy label.text 上输出 dict 值
【发布时间】:2017-10-24 00:36:27
【问题描述】:

首先对不起我的英语不好。

我正在学习 API,我决定在一个非常基本的 KIVY 接口上实现一个天气 API。但是,我无法获得包含天气信息的字典的输出值,作为标签文本。我认为该值不是字符串。你们能帮帮我吗?

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!!

【问题讨论】:

  • 什么是回溯?

标签: python label kivy


【解决方案1】:

将您的 .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 密钥,所以你必须自己检查。

【讨论】:

  • 没问题!将我的答案标记为已接受的答案,以便其他人可以看到。
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
  • 2020-12-17
  • 2012-12-01
相关资源
最近更新 更多