Kivy Weather App Как распечатать вывод на этикетку? - PullRequest
0 голосов
/ 18 февраля 2020

Я пытаюсь выяснить, как напечатать результат погоды из класса tellweather () в текстовую метку в kivy. Но это не сработает. Ошибка: TypeError: __init __ () принимает 1 позиционный аргумент, но 2 были даны . Обычно результат "его 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() 


1 Ответ

0 голосов
/ 18 февраля 2020

Метка работает по-другому, чтобы поместить туда текст, вам просто нужно использовать параметр «текст», поэтому ваш код должен выглядеть так:

# 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...