How do I import a library that prints something off the Internet and shows it on Tkinter?

Asked

Viewed 66 times

-2

I’m doing a graphical interface on Tkinter and I have a library that shows the weather forecast (when I import this library and run the program it already shows me the results). How do I show this library result in a Tk text box?

weather forecast library code:

#!/usr/bin/env python3
"""
Autor: Fábio Berbert de Paula <[email protected]>
Data : 27/11/2018
"""

def previsao(local=''):
    #pip install requests-html
    from requests_html import HTMLSession
    import re

    session = HTMLSession()

    url = 'https://www.google.com/search?q=previsao+do+tempo&oq=previsao+do+tempo&ie=UTF-8'
    if local != '':
        local = local.replace(' ', '+')
        url = url.replace('tempo', 'tempo+' + local)

    #URL resultado da busca no Google por: previsao do tempo
    r = session.get(url)

    #abaixo defino os seletores CSS de cada elemento da pagina
    #e armazeno nas devidas variaveis
    selector_city = '#wob_loc'
    city = r.html.find(selector_city, first=True).text

    selector_date = '#wob_dts'
    date = r.html.find(selector_date, first=True).text

    selector_state = '#wob_dc'
    state = r.html.find(selector_state, first=True).text

    selector_temp = '#wob_tm'
    temp = r.html.find(selector_temp, first=True).text

    #regex para limpar informacoes irrelevantes
    regex = re.compile(r'\nTemperatura.*$', re.DOTALL)
    selector_dtl = 'div.wob-dtl'
    dtl = r.html.find(selector_dtl, first=True).text
    dtl = regex.sub("", dtl)

    return("%s\n%s\t%s°C (%s)\n\n%s" %(city, date, temp, state, dtl))

#leitura da localidade (parâmetro via linha de comando)
import sys

local=''
if len(sys.argv)>0:
    sys.argv.pop(0)
    local = ' '.join(sys.argv)

print(previsao(local))
  • It depends on how your Tkinter code is and what the code of that other library is like. Without code we cannot say.

  • About the Tkinter code I still have nothing practically done. But I added in my question above the library code that I want to put in the interface of Tk. I’m not finding a method to put the library results on the interface. The most I could get on the interface was the library address.

  • So there’s not much secret, you have a function previsao that returns a string, simply display it in the Tk interface you create. When you have something more concrete you can [Edit] the question and add more details. Good luck.

1 answer

1

Modify the function previsao to return only the values of the weather forecast and then display these variables in the property text widget Label tkinter.

To test this code save on a novo_arquivo.py.

from tkinter import *
from requests_html import HTMLSession
import re
import sys

app = Tk()
app.title("Previsão do Tempo")
app.geometry("300x150+200+100")


def previsao(local=''):

    session = HTMLSession()

    url = 'https://www.google.com/search?q=previsao+do+tempo&oq=previsao+do+tempo&ie=UTF-8'
    if local != '':
        local = local.replace(' ', '+')
        url = url.replace('tempo', 'tempo+' + local)

    #URL resultado da busca no Google por: previsao do tempo
    r = session.get(url)

    #abaixo defino os seletores CSS de cada elemento da pagina
    #e armazeno nas devidas variaveis
    selector_city = '#wob_loc'
    city = r.html.find(selector_city, first=True).text

    selector_date = '#wob_dts'
    date = r.html.find(selector_date, first=True).text

    selector_state = '#wob_dc'
    state = r.html.find(selector_state, first=True).text

    selector_temp = '#wob_tm'
    temp = r.html.find(selector_temp, first=True).text

    #regex para limpar informacoes irrelevantes
    regex = re.compile(r'\nTemperatura.*$', re.DOTALL)
    selector_dtl = 'div.wob-dtl'
    dtl = r.html.find(selector_dtl, first=True).text
    dtl = regex.sub("", dtl)

    #return("%s\n%s\t%s°C (%s)\n\n%s" %(city, date, temp, state, dtl))
    return(city, date, temp, state, dtl)

#leitura da localidade (parâmetro via linha de comando)


local=''
if len(sys.argv)>0:
    sys.argv.pop(0)
    local = ' '.join(sys.argv)

#print(previsao(local))


city, date, temp, state, dtl = previsao(local)


Label(app,text=city).pack(side="top",padx=10)
Label(app,text=date).pack(side="top",padx=10)
Label(app,text=temp).pack(side="top",padx=10)
Label(app,text=state).pack(side="top",padx=10)
Label(app,text=dtl).pack(side="top",padx=10)

app.mainloop()

Exit

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.