tkinter模块的button控件不点击也运行command
想做个GUI爬虫,有个按钮,输入框。刚运行输入框无网页链接,按钮没按,就读取Entry控件,用在requsts的get中,报错。错误信息:Traceback(mostre...
想做个GUI爬虫,有个按钮,输入框。刚运行输入框无网页链接,按钮没按,就读取Entry控件,用在requsts的get中,报错。错误信息:Traceback (most recent call last): File "C:/Users/Lenovo/Desktop/a.py", line 17, in <module> button=Button(root,text="下载",font="微软雅黑",command=downlaod_song()) File "C:/Users/Lenovo/Desktop/a.py", line 6, in downlaod_song res=resq.get(ur) File "E:\python3.7.5\lib\site-packages\requests\api.py", line 75, in get return request('get', url, params=params, **kwargs) File "E:\python3.7.5\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "E:\python3.7.5\lib\site-packages\requests\sessions.py", line 510, in request prep = self.prepare_request(req) File "E:\python3.7.5\lib\site-packages\requests\sessions.py", line 453, in prepare_request hooks=merge_hooks(request.hooks, self.hooks), File "E:\python3.7.5\lib\site-packages\requests\models.py", line 313, in prepare self.prepare_url(url, params) File "E:\python3.7.5\lib\site-packages\requests\models.py", line 387, in prepare_url raise MissingSchema(error)requests.exceptions.MissingSchema: Invalid URL '': No schema supplied. Perhaps you meant http://?
源码可能看不清:
from tkinter import *
import requests as resq
abc=0
def downlaod_song():
ur=entry.get()
res=resq.get(ur)
root=Tk()
root.title('网易云音乐爬虫')
root.geometry("430x250+400+200")
label = Label(root,text="输入要爬取的URL:",font=('宋体',15))
label.grid()
entry=Entry(root,font=("微软雅黑",15))
entry.grid(row=0,column=1)
li=Listbox(root,font=('微软雅黑',10))
li.grid(row=1,column=0)
button=Button(root,text="下载",font="微软雅黑",command=downlaod_song())
button.grid(row=1,column=1)
root.mainloop() 展开
源码可能看不清:
from tkinter import *
import requests as resq
abc=0
def downlaod_song():
ur=entry.get()
res=resq.get(ur)
root=Tk()
root.title('网易云音乐爬虫')
root.geometry("430x250+400+200")
label = Label(root,text="输入要爬取的URL:",font=('宋体',15))
label.grid()
entry=Entry(root,font=("微软雅黑",15))
entry.grid(row=0,column=1)
li=Listbox(root,font=('微软雅黑',10))
li.grid(row=1,column=0)
button=Button(root,text="下载",font="微软雅黑",command=downlaod_song())
button.grid(row=1,column=1)
root.mainloop() 展开
2018-10-27 · 知道合伙人互联网行家
关注
展开全部
from tkinter import *
root=Tk()
class count():
def __init__(self):
self.times=0
def __call__(self):
self.times+=1
def gettimes(self):
return self.times
aclick=count()
t=StringVar()
text=Entry(root,textvariable=t)
text.pack()
Button(root,command=aclick,text="buttona").pack()
Button(root,text="get buttona clicktimes",command=lambda:t.set("buttona click %d times"%aclick.gettimes())).pack()
root.mainloop()
root=Tk()
class count():
def __init__(self):
self.times=0
def __call__(self):
self.times+=1
def gettimes(self):
return self.times
aclick=count()
t=StringVar()
text=Entry(root,textvariable=t)
text.pack()
Button(root,command=aclick,text="buttona").pack()
Button(root,text="get buttona clicktimes",command=lambda:t.set("buttona click %d times"%aclick.gettimes())).pack()
root.mainloop()
追问
本人新手,都是按教程写的爬虫,能写一下注释喵?😁
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我碰到过,因为对着书打没有出现过问题,那么肯定是自己的设置造成的。实际上command后面接的函数是不带( )这个的,你删去就好。不知道你解决了没有。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
downlaod_song里面设置标志位控制就行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询