pyowm: как вытащить в результате только detailed_status

from pyowm import OWM
from tkinter import*
import pyttsx3
from pyowm.utils.config import get_default_config
from PIL import ImageTk, Image
import os

config_dict = get_default_config()
config_dict['language'] = 'ru'
owm = OWM('ba791ca513afc4f135cdc07cbe59815b', config_dict)

def click():
#pyowm
    inp = str(entry_input.get())
    mgr = owm.weather_manager()
    observation = mgr.weather_at_place(inp)
    data_weather = observation.weather
    current = str(data_weather)
    entry_result.insert(0, str(current))
#pyttsx3
    engine = pyttsx3.init()
    engine.setProperty('rate', 125)
    engine.say(entry_result)
    engine.runAndWait()

root = Tk()
root.title("weather_speak")
# root.iconbitmap("cloud.ico")
root_input = Frame(root)
root_input = LabelFrame(text = "")
root_button = Frame(root)
root_button = LabelFrame(text = "")
root_result = Frame(root)
root_result = LabelFrame(text = "")
root["bg"] = "#F0F0F0"
root_input["bg"] = "#F0F0F0"
root_button["bg"] = "#F0F0F0"
root_result["bg"] = "#F0F0F0"

#image
img = ImageTk.PhotoImage(Image.open("meteotrend_sun_and_cloud2.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

label_input = Label(root_input,
    text = "Город >> ",
    bg = "#F0F0F0",
    fg = "#0182D2",
    font = "Consolas 13")
label_input.pack(side="left")

entry_input = Entry(root_input,
    width = 15,
    bg = "#0182D2",
    fg = "white",
    font = "Consolas 13")
entry_input.pack(side="left")

button_click = Button(root_button,
    text = "Узнать погоду",
    command = click,
    font = "Consolas 13",
    bg = "#0182D2",
    fg = "white",
    relief = "raised",
    activebackground = "white",
    activeforeground = "#0182D2")
button_click.pack()

entry_result = Entry(root_result,
    width = 30,
    bg = "#0182D2",
    fg = "white",
    font = "Consolas 13")
entry_result.pack(side="bottom")

root_input.pack()
root_button.pack()
root_result.pack()
root.mainloop()

суть программы: пользователь вводит название города, и выводиться осадок (к примеру результат: облачно, идет дождь и т.д.) и после идет озвучивание результата.

проблема: 1) я не знаю как правильно вытащить конкретный результат, сейчас выводиться все подряд и температура и все остальное а нужны только осадки (detailed_status). 2) озвучивает еще до того как появился результат в окошке, а должны быть наоборот.


Ответы (1 шт):

Автор решения: Александр
# Сначала импортируются стандартные модули
#-----------------------------
import os                    #
from threading import Thread #
from tkinter import*         #
#-----------------------------
# А потом все остальные
from pyowm import OWM
import pyttsx3
from pyowm.utils.config import get_default_config
from PIL import ImageTk, Image

config_dict = get_default_config()
config_dict['language'] = 'ru'
owm = OWM('ba791ca513afc4f135cdc07cbe59815b', config_dict)

def show_and_say_info():
    #pyowm
    entry_result.insert(0, "                                               ")
    inp = str(entry_input.get())
    mgr = owm.weather_manager()
    observation = mgr.weather_at_place(inp)
    detailed_status = observation.weather.detailed_status
    entry_result.insert(0, str(detailed_status))


    def say():
        #pyttsx3 блокирует основной поток по этому сначала произносится
        # фраза потом обновляется Label чтобы этого не происходило
        # нужно вынести в отдельный поток
        engine = pyttsx3.init()
        engine.setProperty('rate', 125)
        engine.say(detailed_status)
        engine.runAndWait()
    Thread(target=say).start()

def click():
    Thread(target=show_and_say_info).start()

root = Tk()
root.title("weather_speak")
# root.iconbitmap("cloud.ico")
root_input = Frame(root)
root_input = LabelFrame(text = "")
root_button = Frame(root)
root_button = LabelFrame(text = "")
root_result = Frame(root)
root_result = LabelFrame(text = "")
root["bg"] = "#F0F0F0"
root_input["bg"] = "#F0F0F0"
root_button["bg"] = "#F0F0F0"
root_result["bg"] = "#F0F0F0"

#image
img = ImageTk.PhotoImage(Image.open("meteotrend_sun_and_cloud2.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

label_input = Label(root_input,
    text = "Город >> ",
    bg = "#F0F0F0",
    fg = "#0182D2",
    font = "Consolas 13")
label_input.pack(side="left")

entry_input = Entry(root_input,
    width = 15,
    bg = "#0182D2",
    fg = "white",
    font = "Consolas 13")
entry_input.pack(side="left")

button_click = Button(root_button,
    text = "Узнать погоду",
    command = click,
    font = "Consolas 13",
    bg = "#0182D2",
    fg = "white",
    relief = "raised",
    activebackground = "white",
    activeforeground = "#0182D2")
button_click.pack()

entry_result = Entry(root_result,
    width = 30,
    bg = "#0182D2",
    fg = "white",
    font = "Consolas 13")
entry_result.pack(side="bottom")

root_input.pack()
root_button.pack()
root_result.pack()
root.mainloop()

Документация pyowm

Документация threading

→ Ссылка