Welcome Coders,
In this tutorial we are going to make Wikipedia search tool application using Python Tkinter Module.
Lets take a quick view of modules that are required to make this application.
Check The Output Below:
Output:
Module Used:
- tkinter
- PIL
- Wikipedia -For Searching Wikipedia
- threading
- os
- fpdf - For creating Pdfs
Installations:
To Install pillow
pip install pillow
To Install wikipedia
pip install wikipedia
To Install fpdf (For Creaing Pdfs)
pip install fpdf
Source Code
from tkinter import *
from tkinter import messagebox,ttk,simpledialog
from PIL import Image,ImageTk
import wikipedia
import threading
import os
from fpdf import FPDF
root=Tk()
root.title("Wikipedia Search By Coder Community")
#container Frame
window=Frame(root)
window.grid(row=5,column=0,columnspan=2)
#Creating Canvas For Banner
canvas=Canvas(root,width=385,height=100)
canvas.grid(row=0,column=0,columnspan=2)
#Displaying Banner On Canvas
Banner=ImageTk.PhotoImage(Image.open("F:/wiki.jpg"))
canvas.create_image(0,0,image=Banner,anchor=NW)
my_progress=ttk.Progressbar(root,orient=HORIZONTAL,length=300,mode="indeterminate")
Dir_name="c:/"
Directory="Wikipedia Search"
path=os.path.join(Dir_name,Directory)
try:
os.mkdir(path)
except OSError as error:
pass
def extract():
global text
text_container.delete('1.0', END)
os.chdir("c:/Wikipedia Search")
find=search_box.get()
sentence=sentence_box.get()
my_progress.grid(row=1,column=0,columnspan=2)
my_progress.start(10)
try:
text=wikipedia.summary(find,sentences=sentence)
text_container.insert(END,text)
my_progress.grid_forget()
except Exception as e:
messagebox.showerror("Results Not Found","Try using a Different Keywords!")
my_progress.grid_forget()
def save_txt():
name=simpledialog.askstring("Filename","Enter Your File Name")
file=open(f"{name}.txt","wt")
file.write(text)
file.close()
messagebox.showinfo("File Saved","Your Txt File Is Saved!\nC:\Wikipedia Search")
def save_pdf():
name=simpledialog.askstring("Filename","Enter Your File Name")
pdf=FPDF()
pdf.add_page()
pdf.set_font("Arial",size=13)
pdf.multi_cell(200, 10, txt=text,align='L')
pdf.output(f"{name}.pdf")
messagebox.showinfo("File Saved","Your Txt File Is Saved!\nC:\Wikipedia Search")
def thread1():
thread=threading.Thread(target=extract)
thread.start()
search_box=Entry(root,width=40,font=("arial bold",12))
search_box.grid(row=2,column=0,ipady=5,columnspan=2)
search_button=Button(root,text="Search Wikipedia",font=("arial bold",12),bg="#5f9ea0",command=thread1)
search_button.grid(row=3,column=0,columnspan=2,ipadx=80,pady=5)
label1=Label(root,text="No Of Sentences:",font=("arial bold",12))
label1.grid(row=4,column=0,pady=5)
sentence_box=Entry(root,width=10,font=("arial bold",12))
sentence_box.grid(row=4,column=1,ipady=5,pady=5)
scrollbar=Scrollbar(window)
scrollbar.grid(row=0,column=1,sticky=NE+SE)
text_container=Text(window,width=50,height=15,yscrollcommand=scrollbar.set,font=("Arial bold",11))
text_container.grid(row=0,column=0)
scrollbar.config( command = text_container.yview )
txt_save=Button(root,text="Save As Txt",font=("arial bold",12),command=save_txt,bg="#ff7f50")
txt_save.grid(row=6,column=0,ipadx=45,pady=5)
pdf_save=Button(root,text="Save As Pdf",font=("arial bold",12),command=save_pdf,bg="#ff7f50")
pdf_save.grid(row=6,column=1,ipadx=45,pady=5)
quit=Button(root,text="Close",font=("arial bold",12),bg="#5f9ea0",command=root.destroy)
quit.grid(row=7,column=0,ipadx=150,columnspan=2,pady=5)
root.mainloop()
Download Source Code File:
Download Material Used In This Application:
0 Comments