Home Developer Programming Language Framework Articles Contact Me

HTML CSS JAVASCRIPT JAVA PYTHON JQUERY REACTJS VUEJS ANGULARJS EMBERJS SASS LESS STYLUS GITHUB GITLAB BIGBUCKET JAVA KOTLIN HADOOP JDBC SERVLETS JAVA BEANS(EJB) JAVA SERVER PAGES(JSP) UNIX LINUX POSTGRESQL MSSQLSERVER SQLITE MONGODB XML FLASK PYTHON MYSQL JAVA C++ JUNIT SELENIUM

Wikipedia Searching App Using Tkinter in Python

Prerequisite:-

In this article, we will learn how to create a Wikipedia searching app using Tkinter in python.

Libraries Required:-

  1. Tkinter
  2. Wikipedia

Installation:-

      
pip install wikipedia
    
Below is how the GUI looks like:-

Let's Understand Step by Step Implementation:-

Step 1(Create a Normal Tkinter window)

      
# Import Required Module
from tkinter import *

# Create Tkinter Object
root = Tk()

# Set Title
root.title('Wikipedia App')

# Set Geometry
root.geometry("668x600")

# Execute Tkinter
root.mainloop()
    
Output:-

Step 2 (Add Buttons, Scrollbars, Entry boxes, etc...)


# Import Required Module
from tkinter import *

# Create Tkinter Object
root = Tk()

# Set Title
root.title('Wikipedia App')

# Set Geometry
root.geometry("668x600")

# Add Label Frame
label_frame = LabelFrame(root, text="Search Wikipedia")
label_frame.pack(pady=20)

# Add Entry Box
entry = Entry(label_frame, font=("Helvetica", 20), width=37)
entry.pack(pady=20, padx=20)

# Add Text Box Frame
text_box_frame = Frame(root)
text_box_frame.pack(pady=5)

# Button Frame
button_frame = Frame(root)
button_frame.pack()

# Add Vertical Scrollbar
vertical_scrollbar = Scrollbar(text_box_frame)
vertical_scrollbar.pack(side=RIGHT, fill=Y)

# Add Horizontal Scrollbar
horizontal_scrollbar = Scrollbar(text_box_frame, orient='horizontal')
horizontal_scrollbar.pack(side=BOTTOM, fill=X)

# Add Text Box
text_box = Text(text_box_frame, yscrollcommand=vertical_scrollbar.set, 
wrap="none", xscrollcommand=horizontal_scrollbar.set)

text_box.pack()

# Configure Scrollbars
vertical_scrollbar.config(command=text_box.yview)
horizontal_scrollbar.config(command=text_box.xview)

# Add Buttons
search_btn = Button(button_frame, text="Search", font=("Helvetica", 15), fg="#3a3a3a")
search_btn.grid(row=0, column=0)

clear_btn = Button(button_frame, text="Clear", font=("Helvetica", 15), fg="#3a3a3a")
clear_btn.grid(row=0, column=1)

# Execute Tkinter
root.mainloop()
  

Step 3 (Define Functions)

Now we will create two functions; clear_textbox(), search()

      
# Clear the textbox
def clear_textbox():
  entry.delete(0, END)
  text_box.delete(0.0, END)

# Search on wikipedia
def search():
  data = wikipedia.page(entry.get())
  # Clear the text box
  clear_textbox()
  # Output Wikipedia Results To Textbox
  text_box.insert(0.0, data.content)
    
Below is the Implementation:-
      
# Import Required Module
from tkinter import *
import wikipedia

# Create Tkinter Object
root = Tk()

# Set Title
root.title('Wikipedia App')

# Set Geometry
root.geometry("668x600")

# Clear the textbox
def clear_textbox():
  entry.delete(0, END)
  text_box.delete(0.0, END)

# Search on wikipedia
def search():
  data = wikipedia.page(entry.get())
  # Clear the text box
  clear_textbox()
  # Output Wikipedia Results To Textbox
  text_box.insert(0.0, data.content)

# Add Label Frame
label_frame = LabelFrame(root, text="Search Wikipedia")
label_frame.pack(pady=20)

# Add Entry Box
entry = Entry(label_frame, font=("Helvetica", 20), width=37)
entry.pack(pady=20, padx=20)

# Add Text Box Frame
text_box_frame = Frame(root)
text_box_frame.pack(pady=5)

# Button Frame
button_frame = Frame(root)
button_frame.pack()

# Add Vertical Scrollbar
vertical_scrollbar = Scrollbar(text_box_frame)
vertical_scrollbar.pack(side=RIGHT, fill=Y)

# Add Horizontal Scrollbar
horizontal_scrollbar = Scrollbar(text_box_frame, orient='horizontal')
horizontal_scrollbar.pack(side=BOTTOM, fill=X)

# Add Text Box
text_box = Text(text_box_frame, yscrollcommand=vertical_scrollbar.set, wrap="none",
xscrollcommand=horizontal_scrollbar.set)

text_box.pack()

# Configure Scrollbars
vertical_scrollbar.config(command=text_box.yview)
horizontal_scrollbar.config(command=text_box.xview)

# Add Buttons
search_btn = Button(button_frame, text="Search", font=("Helvetica", 15), fg="#3a3a3a",
command=search)

search_btn.grid(row=0, column=0)

clear_btn = Button(button_frame, text="Clear", font=("Helvetica", 15), fg="#3a3a3a",
command=clear_textbox)

clear_btn.grid(row=0, column=1)

# Execute Tkinter
root.mainloop()
    
Output:-