发送网址到功能

时间:2018-10-18 18:15:46

标签: python python-3.x tkinter

我对python很陌生,但是喜欢它。
我有问题了
我希望从tkinter中的按钮解析URL到我的函数KB,因此,当用户单击skanderborg时,它将URL解析为我的函数中的URL,但我无法正常工作。

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

def kb():
    page = requests.get(url)
    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, cloud, daily_temp

var2 = kb()
top = tkinter.Tk()
placevar1 = StringVar()
placevar1.set(var2)
label = Label(top, textvariable=placevar1 )
B = tkinter.Button(top, text ="Copenhagen", command = kb)
B2 = tkinter.Button(top, text ="Skanderborg", command=lambda: kb(url ="https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7"))
label.pack()
B.pack()
B2.pack()
top.mainloop()

2 个答案:

答案 0 :(得分:0)

在我看来,您的kb函数不需要任何参数。

您在哪里

def kb():

您可能需要放

def kb(url) :

相反。这将使您的kb函数使用您调用它的URL,并在整个函数中将其用作变量名url。

答案 1 :(得分:0)

发生了两三件事。

不要忘记@ tomh1012指出的函数中的参数。  如果要更新文本,则kb必须带有两个参数。
成为:

def kb(url, text_widget):

请注意缩进它们是否丢失一两个(我不知道它是否是不好的复制/粘贴)

如果您想使用命令作为lambda表达式,请不要忘记关键字lambda:。 (您在第一个B按钮中忘记了它)

还有一条建议不要犹豫使用python IDE,它简化了很多调试和语法问题(例如:https://www.jetbrains.com/pycharm/

完整的示例

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

def kb(url, text_widget):
    page = requests.get(url)
    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")
    text_widget.set((place, cloud, daily_temp))
    return place, cloud, daily_temp


top = tkinter.Tk()
placevar1 = StringVar()
var2 = kb(url =
"https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7", text_widget=placevar1)
label = Label(top, textvariable=placevar1 )
B = tkinter.Button(top, text ="Copenhagen", command = lambda: kb(url="https://weather.com/weather/today/l/DAXX0009:1:DA", text_widget=placevar1))
B2 = tkinter.Button(top, text ="Skanderborg", command= lambda: kb(url =
"https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7", text_widget=placevar1))

label.pack()
B.pack()
B2.pack()
top.mainloop()