Python Forum

Full Version: beginner send value from function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
i am a beginner, but love python
I can't figure out how to get the value "place" in my function into this "placevar1.set()"
I need this value to be shown in my tkinter label. But can't get the value

Any help here? :-)





import requests
from bs4 import BeautifulSoup
import tkinter
from tkinter import *


def kb():
    page = requests.get("https://weather.com/da-DK/vejret/idag/l/DAXX0009:1:DA")
    soup = BeautifulSoup(page.content, 'html.parser')
    seven_day = soup.find(class_="today_nowcard-container")
    forecast_items = seven_day.find_all(class_="today_nowcard-main")
    details_items = seven_day.find(class_="today_nowcard-section")
    place = seven_day.find(class_="today_nowcard-location").get_text()
    daily_temp = seven_day.find(class_="today_nowcard-temp").get_text()
    cloud = seven_day.find(class_="today_nowcard-phrase").get_text()
    print(place)
    print(daily_temp)
    print(cloud)
    data = ("place" + "cloud")
kb()





top = tkinter.Tk()
placevar1 = StringVar()


placevar1.set()

B = tkinter.Button(top, text ="Hello", command = kb)
label = Label(top, textvariable=placevar1 )




label.pack()
B.pack()

top.mainloop()
It's quite simple actually. The function needs to return your value "place", which is done like this:

def kb():
    page = requests.get("https://weather.com/da-DK/vejret/idag/l/DAXX0009:1:DA")
    soup = BeautifulSoup(page.content, 'html.parser')
    seven_day = soup.find(class_="today_nowcard-container")
    forecast_items = seven_day.find_all(class_="today_nowcard-main")
    details_items = seven_day.find(class_="today_nowcard-section")
    place = seven_day.find(class_="today_nowcard-location").get_text()
    daily_temp = seven_day.find(class_="today_nowcard-temp").get_text()
    cloud = seven_day.find(class_="today_nowcard-phrase").get_text()
    print(place)
    print(daily_temp)
    print(cloud)
    data = ("place" + "cloud")

    return place