将 tkinter 的下拉列表选项链接到我的网络浏览器

linking dropdown list option from tkinter into my webbrowser

所以这个程序背后的总体思路只是一个应用程序,我可以通过在下拉列表中替换它来搜索不同类型的网站:

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",fg="white",bg="#FF0422",command=yt)
b3=tk.Button(root,text="Instagram",fg="white",bg="#F7378C",command=ig)
b4=tk.Button(root,text="Twitter",fg="white",bg="#12AED8",command=tw)
b6=tk.Button(root,text="Search",fg="white",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,y=70,width=80,height=30)
b3.place(x=190,y=70,width=80,height=30)
b4.place(x=280,y=70,width=70,height=30)
b6.place(x=307,y=10,width=90,height=50)
b7.place(x=360,y=70,width=200,height=30)
el=tk.Entry(root,textvariable=x)
el.place(x=10,y=10,width=300,height=50)


labelTest = tk.Label(text="", font=('Helvetica', 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) 我得到了 url 用于搜索它们中的每一个,但事实并非如此, 我面临的问题是如何将 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)

只需对您的功能进行此更改,多亏了大多数网站使用的“搜索技术”,即使是多个单词也能正常工作,在这里。

希望对您有所帮助,如有任何错误或疑问,请告诉我。

干杯