From 0f44cef43af417d37ad73f21fa8f786e769e0f68 Mon Sep 17 00:00:00 2001 From: riakashyap <77228990+riakashyap@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:24:19 +0530 Subject: [PATCH] Update wikipediasearch.py Added resizable window for better user interface in tkinter and better error handing for wikipedia search --- Applications/Search Engine/wikipediasearch.py | 63 ++++++++++++------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/Applications/Search Engine/wikipediasearch.py b/Applications/Search Engine/wikipediasearch.py index e1acedf..4119d35 100644 --- a/Applications/Search Engine/wikipediasearch.py +++ b/Applications/Search Engine/wikipediasearch.py @@ -1,32 +1,51 @@ -from tkinter import * +""" +If wikipedia module has not been installed previously, execute the following command - pip install wikipedia +""" +import tkinter as tk +from tkinter import messagebox, Scrollbar, Text import wikipedia def get_data(): - entry_value = entry.get() - answer.delete(1.0, END) + entry_value = entry.get().strip() + answer.delete(1.0, tk.END) + if not entry_value: + answer.insert(tk.INSERT, "Please enter a valid search term.") + return + try: - answer_value = wikipedia.summary(entry_value) - answer.insert(INSERT, answer_value) - except: - answer.insert(INSERT, "ERROR! Invalid input or poor internet connection") + answer_value = wikipedia.summary(entry_value, sentences=3) # Limit to a few sentences for faster response + answer.insert(tk.INSERT, answer_value) + except wikipedia.exceptions.DisambiguationError as e: + answer.insert(tk.INSERT, f"Multiple results found. Did you mean: {str(e)}") + except wikipedia.exceptions.PageError: + answer.insert(tk.INSERT, "ERROR! Page not found. Please try a different term.") + except Exception as e: + answer.insert(tk.INSERT, f"ERROR! {str(e)}") + messagebox.showerror("Error", "An error occurred. Check your internet connection or input.") -win = Tk() +win = tk.Tk() win.title("Wikipedia Search") -topframe = Frame(win) -entry = Entry(topframe) -entry.pack() -button = Button(topframe, text="search", command=get_data) -button.pack() -topframe.pack(side = TOP) - - -bottomframe = Frame(win) -scroll = Scrollbar(bottomframe) -scroll.pack(side=RIGHT, fill=Y) -answer = Text(bottomframe, width=50, height=20, yscrollcommand = scroll.set, wrap=WORD) +win.geometry("600x400") +win.resizable(True, True) + +top_frame = tk.Frame(win, padx=10, pady=10) +entry = tk.Entry(top_frame, width=40) +entry.grid(row=0, column=0, padx=5) + +button = tk.Button(top_frame, text="Search", command=get_data) +button.grid(row=0, column=1, padx=5) + +top_frame.pack(side=tk.TOP) + +bottom_frame = tk.Frame(win, padx=10, pady=10) +scroll = Scrollbar(bottom_frame) +answer = Text(bottom_frame, width=70, height=20, yscrollcommand=scroll.set, wrap=tk.WORD) scroll.config(command=answer.yview) -answer.pack() -bottomframe.pack() + +scroll.pack(side=tk.RIGHT, fill=tk.Y) +answer.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) + +bottom_frame.pack(fill=tk.BOTH, expand=True) win.mainloop()