Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tkinter中按下按钮时调用函数_Python_User Interface_Tkinter - Fatal编程技术网

Python 在Tkinter中按下按钮时调用函数

Python 在Tkinter中按下按钮时调用函数,python,user-interface,tkinter,Python,User Interface,Tkinter,我有一个AmazonWebScraper,我想作为GUI运行,然后当单击按钮时,脚本在后台运行 我已经看到了另一个关于这个的帖子,但是我的帖子涉及到从条目中获取文本,所以行为仍然有点混乱 当我运行这段代码时,我会遇到巨大的错误,它们似乎没有告诉我太多,除了最后一个错误是MissingSchema(error)。我是Python的初学者 我想要的就是能够从条目中获取文本,将该文本放入检查价格()和发送邮件()函数中,然后在单击按钮时调用检查价格() 导入请求 从bs4导入BeautifulSoup

我有一个AmazonWebScraper,我想作为GUI运行,然后当单击按钮时,脚本在后台运行

我已经看到了另一个关于这个的帖子,但是我的帖子涉及到从
条目中获取文本,所以行为仍然有点混乱

当我运行这段代码时,我会遇到巨大的错误,它们似乎没有告诉我太多,除了最后一个错误是
MissingSchema(error)
。我是Python的初学者

我想要的就是能够从
条目中获取文本,将该文本放入
检查价格()
发送邮件()
函数中,然后在单击按钮时调用
检查价格()

导入请求
从bs4导入BeautifulSoup
导入smtplib
导入时间
从tkinter进口*
从tkinter导入消息框
root=Tk()
label=label(root,text=“输入浏览器的用户代理”).grid(行=0)
label1=Label(root,text=“输入亚马逊产品的URL”).grid(行=1)
label2=Label(root,text=“价格下跌时我们会给您发电子邮件”).grid(row=2)
label3=Label(root,text=“请输入您的电子邮件”).grid(row=3)
标题=条目(根)
headers.grid(行=0,列=1)
url=条目(根)
网格(行=1,列=1)
阈值=条目(根)
网格(行=2,列=1)
收件人=条目(根)
recipient.grid(行=3,列=1)
def check_price():
page=requests.get(url.get(),requests,headers={“用户代理”:headers.get()})
soup=BeautifulSoup(page.content,'html.parser')
尝试:
title=soup.find(id='productTitle').get_text()
除属性错误外:
messagebox.batherRor(“错误”,“当前无法跟踪您的项目”)
尝试:
price=soup.find(id='priceblock\u ourprice').get\u text()
除属性错误外:
messagebox.batherRor(“错误”,“当前无法跟踪您的项目”)
转换价格=浮动(价格[1:10])
如果(换算价格<72.61):
发送邮件
打印(换算价格)
打印(title.strip())
def send_mail():
server=smtplib.SMTP('SMTP.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('simonshm5@gmail.com','alhmrwxpcmknbztz')
主题=‘价格降低了!’
body='链接:https://www.amazon.com/Cooler-Master-MasterLiquid-Chamber-MLA-D24M-A18PC-R1/dp/B07CRGC899/ref=sr_1_3?crid=27DW5B6SBZO0Y&dchild=1&keywords=pc+液体+冷却器&qid=1596674717&sprefix=pc+液体+%2Caps%2C204&sr=8-3'
msg=f“主题:{Subject}\n\n{body}”
server.sendmail('Censored@gmail.com', 'Censored@gmail.com",msg)
打印(“已发送电子邮件”)
server.quit()
sendButton=Button(root,text=“提交并关闭”,command=check\u price())
sendButton.grid(行=4,列=0)
root.mainloop()
虽然(正确):
核对价格
时间。睡眠(60*60*12)
我明白了

C:\Users\simon\Documents\PythonVS>pythonwebscraper.py
回溯(最近一次呼叫最后一次):
文件“Webscraper.py”,第65行,在
sendButton=Button(root,text=“提交并关闭”,command=check\u price())
文件“Webscraper.py”,第27行,检查价格
page=requests.get(url.get(),requests,headers={“用户代理”:headers.get()})
文件“C:\Users\simon\AppData\Roaming\Python38\site packages\requests\api.py”,第76行,在get中
返回请求('get',url,params=params,**kwargs)
文件“C:\Users\simon\AppData\Roaming\Python38\site packages\requests\api.py”,第61行,在请求中
return session.request(method=method,url=url,**kwargs)
文件“C:\Users\simon\AppData\Roaming\Python38\site packages\requests\sessions.py”,第516行,在请求中
准备=自我准备请求(req)
文件“C:\Users\simon\AppData\Roaming\Python38\site packages\requests\sessions.py”,第449行,在prepare\u请求中
p、 预备(
文件“C:\Users\simon\AppData\Roaming\Python38\site packages\requests\models.py”,第314行,在prepare中
self.prepare_url(url,参数)
文件“C:\Users\simon\AppData\Roaming\Python38\site packages\requests\models.py”,第388行,在prepare\u url中
raise MissingSchema(错误)
requests.exceptions.MissingSchema:无效URL“”:未提供架构。您的意思可能是http://?

我真的不确定我应该做什么,我感觉有点卡住了

当按下一个按钮时,您似乎成功地调用了一个函数,因为错误来自该函数。
requests.exceptions.MissingSchema:无效的URL“”:没有提供架构。也许您的意思是http://?
@JeffUK似乎我无法检索e> url
string并将其传递到
requests.get(url.get(),…)
command=check\u price()
应该是
command=check\u price
。当按下按钮时,您似乎成功调用了一个函数,因为错误来自该函数。
requests.exceptions.MissingSchema:无效URL“”:未提供架构。也许您的意思是http://?
@JeffUK似乎我无法检索
URL
字符串和将它传递到requests.get(url.get(),…)
command=check\u price()
应该是
command=check\u price