我尝试用Python制作一个基于GUI的搜索引擎,但由于某种原因我遇到了两个错误。
###编辑
在大家的帮助下,我对代码进行了修改,但还是不行。新代码在这里:
import requests,webbrowser
from bs4 import BeautifulSoup
from tkinter import *
structure=Tk()
structure.geometry("1280*1280")
structure.title("Cgo Engine")
label=Label(structure,text="Cgo Engine",bg="dark grey",fg="Gold",font=("ComicSans",60,"italic"))
label.pack(side=TOP)
structure.config(background="dark grey")
text=StringVar()
###
def searches():
data=requests.get('https://www.google.com/search?q='+text.get())
soup=BeautifulSoup(data.content,"html.parser")
result=soup.select(".kCrYT a")
for ans in result[:6]:
search=ans.get("href")
search=search[7:]
search=search.split("&")
webbrowser.open(search[0])
###
label=Label(structure,font=("Times",15,"bold"),text="Enter here to search",bg="black",fg="yellow")
label.place(x=600,y=350)
enter=Entry(structure,font=("Times",15,"bold"),textvar=text,width=60,bd=2,bg="white")
enter.place(x=600,y=500)
button=Button(structure,text="Search",font=("Times",15,"bold"),width=10,bd=2,bg="white",command=search)
button.place(x=100,y=200)
structure.mainloop()
必须更改这两件事才能正常运行您的代码:
结构.几何(“1230*1230”)
将变成:'struct.geometry("1230x1230")'
button=按钮(结构,text=“搜索”,font=(“时代”,15,“粗体”),宽度=30,bd=2,bg=“白色”,命令=搜索)
将变为: 'button=Button(struct,text="Search",font=("Times",15,"bold"),width=30,bd=2,bg="white",command=searches )'
然后你的代码就可以正常运行了。