Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner send value from function
#1
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()
Reply
#2
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020