Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import wikipedia
- def search_wikipedia(event=None):
- """Retrieve user query from a Text widget and display the Wikipedia summary."""
- query = query_text.get("1.0", tk.END).strip()
- result_text.delete("1.0", tk.END)
- if not query:
- result_text.insert("1.0", "Please enter a search term.")
- return
- try:
- # Get Wikipedia summary for the entered query
- summary = wikipedia.summary(query, sentences=50)
- result_text.insert("1.0", summary)
- except wikipedia.exceptions.DisambiguationError as e:
- result_text.insert("1.0", f"Disambiguation error: {e}")
- except wikipedia.exceptions.PageError:
- result_text.insert("1.0", "No page found for your query.")
- except Exception as e:
- result_text.insert("1.0", f"An unexpected error occurred: {e}")
- # Main window
- root = tk.Tk()
- root.title("WikiSearch")
- root.geometry("600x700")
- # Frame for input and button
- frame = tk.Frame(root)
- frame.pack(pady=10)
- # Create a Text widget for user query
- query_text = tk.Text(frame, width=60, height=8)
- query_text.pack()
- # Bind "Enter" key to the search function
- query_text.bind("<Return>", search_wikipedia)
- # Create a button to initiate the search
- search_button = tk.Button(frame, text="Search", command=search_wikipedia)
- search_button.pack(pady=5)
- # Text widget to display search results
- result_text = tk.Text(root, wrap=tk.WORD, width=70, height=30)
- result_text.pack(pady=10)
- # Start the mainloop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement