簡體   English   中英

Kivy Weather App 如何打印輸出到標簽?

[英]Kivy Weather App How to Print Output to label?

我想弄清楚如何將 tellweather () 類的天氣結果打印到 kivy 中的文本標簽中。但它不會工作 Error: TypeError: __init __ () takes 1 positional argument but 2 were given 通常結果是“它的 24 Temp”。我現在有以下代碼:

import kivy
import requests
import json
from kivy.app import App
from kivy.uix.label import Label 
class tell_weather():
    url ='http://api.openweathermap.org/data/2.5/weather?q=appid=xxxxxx"'     
    json_data = requests.get(url).json()     
    format_add = json_data['main']['temp'] 
    print("Its", format_add, "Temp")

    kivy.require("1.9.1") 

class MyLabelApp(App): 
    def build(self):
        label display the text on screen 
            lbl = Label(tell_weather())
        #lbl = Label(tellweather())
        return lbl 

    # creating the object 

label = MyLabelApp() 
label.run() 


標簽以另一種方式工作,要將文本放在那里,您只需使用參數“文本”,因此您的代碼應如下所示:

# it's not a class, it's a function!
def tell_weather():
    url ='http://api.openweathermap.org/data/2.5/weather?q=appid=xxxxxx"'     
    json_data = requests.get(url).json()     
    format_add = json_data['main']['temp'] 
    # print makes output to console, it's wrong, here you should return string
    return("Its " + str(format_add) + " Temp")

class MyLabelApp(App): 
    def build(self):
        # what's that line doing here? Comment it!
        # label display the text on screen 

            # so here we use text parameter that will be equal to string that function tell_weather returns
            lbl = Label(text=tell_weather())
        return lbl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM