创建刷新按钮

2024-06-06 17:51:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个刷新按钮来实现来自API的信息,但不知道如何实现,我想实现的代码如下:

def templ(widget):
    global templl,varil
    templl=Gtk.Window(title='Weather City Of London')
    varil=0
    print('ver')
    print(varil)
    while varil==0:
        requisicao3=requests.get('http://api.openweathermap.org/data/2.5/weather?q=City of London&appid=a5d84c2b0dbdc187d1521773a2bd3a22')
        tempo3=json.loads(requisicao3.text)
        reqt3=(tempo3['weather'][0]['main'])
        atl3=Gtk.Button("atualize")
        print(reqt3)
        vbox3=Gtk.VBox()
        templl.add(vbox3)
        templl.add(vbox3)
        lblreqt3=Gtk.Label(reqt3)
        vbox3.add(lblreqt3)
        vbox3.add(atl3)
        templl.set_default_size(640,480)
        templl.show_all()
        varil=0
        print("funcionalop")
londonb.connect("clicked",templ)

Tags: addcitygtktemplprintweatherlondonatl3
1条回答
网友
1楼 · 发布于 2024-06-06 17:51:46

下面是一个函数,当调用它时,它获取天气并将其放入窗口中。它与按钮相连。你知道吗

# widget setup

def fetch_weather(widget):
    requisicao3 = requests.get('http://api.openweathermap.org/data/2.5/weather?q=City of London&appid=a5d84c2b0dbdc187d1521773a2bd3a22')
    tempo3 = json.loads(requisicao3.text)
    reqt3 = tempo3['weather'][0]['main']
    lblreqt3.set_label(reqt3)

if should_fetch_before_show:
    fetch_weather(atl3)

atl3.connect("clicked", fetch_weather)

# show the window

相关问题 更多 >