使用tkinter combobox实现百度搜索框
时间: 2023-11-05 14:57:12 浏览: 42
可以使用以下代码实现:
```python
import tkinter as tk
import webbrowser
root = tk.Tk()
root.title("百度搜索框")
# 创建一个下拉框
options = ["百度一下", "谷歌搜索", "必应搜索"]
search_engine = tk.StringVar()
search_engine_cb = tk.ttk.Combobox(root, textvariable=search_engine, values=options)
search_engine_cb.pack(side=tk.LEFT, padx=5, pady=5)
# 创建一个输入框
search_entry = tk.Entry(root, width=30)
search_entry.pack(side=tk.LEFT, padx=5, pady=5)
# 创建一个搜索按钮
def search():
keyword = search_entry.get()
engine = search_engine.get()
if engine == "百度一下":
webbrowser.open("https://www.baidu.com/s?wd=" + keyword)
elif engine == "谷歌搜索":
webbrowser.open("https://www.google.com/search?q=" + keyword)
elif engine == "必应搜索":
webbrowser.open("https://cn.bing.com/search?q=" + keyword)
search_button = tk.Button(root, text="搜索", command=search)
search_button.pack(side=tk.LEFT, padx=5, pady=5)
root.mainloop()
```
该程序创建了一个下拉框,可以选择搜索引擎,一个输入框用于输入搜索关键字,和一个搜索按钮。当点击搜索按钮时,根据选择的搜索引擎,在浏览器中打开相应的搜索结果页面。