我想创建一个按钮,当按下该按钮时,它将在get_btc函数中显示price_text变量的消息。我是一个GUI新手,所以如果我已经尝试搜索并且没有找到可靠的答案的帖子,那么我只是在寻找一些指导道歉。
我当前的代码看起来像这样,并分成一个目录
crypto_price--模型--scraper.py | 图形用户界面
内容是:
Scraper.py:
from bs4 import BeautifulSoup
import datetime
import requests
import time
import lxml
def get_btc():
url = requests.get("https://uk.finance.yahoo.com/quote/BTC-GBP/")
soup = BeautifulSoup(url.text, "lxml")
finds = soup.find_all("div", class_="D(ib) Mend(20px)")[0].find('span')
for find in finds:
price=find
price_text= f"The price of BTC at {datetime.datetime.now()} is £{price}"
print(price_text)
time.sleep(5)
GUI.py:
from tkinter import *
from Model.scraper import get_btc
window = Tk()
window.title("BTC Price")
photo1 = PhotoImage(file="btc.png")
Label(window, image=photo1, bg="black").grid(row=0, column=0, sticky=E)
PriceButton = Button(text="Btc Price", command=get_btc())
PriceButton.place(x=0, y=0)
PriceButton.pack
window.mainloop()
您不应该调用函数,它应该是:
PriceButton = Button(text="Btc Price", command=get_btc)
该()
命令处的功能的必须删除。当您使用()
调用(调用)函数时,python会在看到()
带有函数名称的命令后立即运行该函数,因此将其删除,该按钮将在您每次按下按钮时为您调用该函数。
希望这能消除您的疑虑,如有任何错误,请告诉我。
干杯
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句