如何解决将tkinter的下拉列表选项链接到我的网络浏览器
因此,该程序的总体思路只是一个应用程序,我可以通过在下拉列表中替换它来搜索其他类型的网站:
import webbrowser
import tkinter as tk
optionlist=["youtube","google","amazon","ebay"]
root=tk.Tk()
root.title("WEB BROWSER")
root.geometry("600x200")
x=tk.StringVar()
variable = tk.StringVar(root)
variable.set(optionlist[0])
def fb():
webbrowser.open_new("https://www.facebook.com/")
def yt():
webbrowser.open_new("https://www.youtube.com/")
def ig():
webbrowser.open_new("https://www.instagram.com/")
def tw():
webbrowser.open_new("https://twitter.com/")
def search():
word=x.get()
search="https://www.google.com/search?q="+word
webbrowser.open_new(search)
x=tk.StringVar()
b1=tk.Button(root,text="Facebook",fg="white",bg="#3343BA",command=fb)
b2=tk.Button(root,text="Youtube",bg="#FF0422",command=yt)
b3=tk.Button(root,text="Instagram",bg="#F7378C",command=ig)
b4=tk.Button(root,text="Twitter",bg="#12AED8",command=tw)
b6=tk.Button(root,text="Search",bg="#121011",command=search)
b7=tk.OptionMenu(root,variable,*optionlist)
b7.config(width=90,font=('Helvetica',12))
b7.pack(side="top")
b1.place(x=10,y=70,width=80,height=30)
b2.place(x=100,height=30)
b3.place(x=190,height=30)
b4.place(x=280,width=70,height=30)
b6.place(x=307,y=10,width=90,height=50)
b7.place(x=360,width=200,height=30)
el=tk.Entry(root,textvariable=x)
el.place(x=10,width=300,height=50)
labelTest = tk.Label(text="",12),fg='black')
labelTest.pack(side="top")
labelTest.place(x=360,y=105)
def callback(*args):
labelTest.configure(text="You chose to search in: {}".format(variable.get()))
variable.trace("w",callback)
root.mainloop()
因此,如您所见,我有一个正在运行的搜索应用程序,并且我在下拉列表中添加了以下内容(ebay Amazon Google和youtube) 我得到了在每个人中搜索的网址,情况并非如此, 我面临的问题是我如何将tkinter中的选择绑定到我要搜索的平台上
解决方法
检查这是否是您想要的:
def search():
word = x.get()
if variable.get() == 'youtube':
search="https://www.youtube.com/results?search_query="+word
webbrowser.open_new(search)
elif variable.get() == 'google':
search="https://www.google.com/search?q="+word
webbrowser.open_new(search)
elif variable.get() == 'amazon':
search="https://www.amazon.com/s?k="+word
webbrowser.open_new(search)
else:
search="https://www.ebay.com/sch/"+word
webbrowser.open_new(search)
由于大多数站点都使用了“搜索技术”,因此只需对您的功能进行此更改,即使是多个单词也可以在此处正常工作。
希望这对您有所帮助,请让我知道是否有任何错误或疑问。
欢呼
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。