-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.
– Woss
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.
– user152335
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.– Woss