Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- from bs4 import BeautifulSoup
- from pprint import pprint
- import tkinter as tk
- window = tk.Tk()
- window.configure(bg='black')
- window.title('Dictionary Search')
- #Font for the Text Widget
- font_0 = ('times', 12, 'bold')
- #font_out = ('times', 16, 'bold')
- def process():
- ''' A Wikipedia Search App That Searches And Displays The Summary Of A Search Word.
- This App Was Written In Python 3.6 Using tkinter For The UI.
- Created By:collinsanele@gmail.com '''
- scrollbar = tk.Scrollbar(window, bg='white')
- scrollbar.place(x=1300, y=264)
- output = tk.Text(window, width=150, height=20,bg='gray', fg='black', font=font_0,
- yscrollcommand=scrollbar.set)
- output.place(x=84, y=260, in_=window)
- scrollbar.config(command=output.yview)
- search_input = str(entry_search.get())
- u1 ='http://en.wikipedia.org/w/api.php?action=query&prop=extracts&'
- u2 = 'format=json&exintro=&titles='+search_input
- url = u1 + u2
- r = requests.get(url).json()
- #content = r['query']['pages']
- #content = json.dumps(content)
- try:
- for k,v in r['query']['pages'].items():
- key = str(k)
- input_go.insert(tk.END, key)
- title = r['query']['pages'][key]['title']
- extract = r['query']['pages'][key]['extract']
- page = title +'\n' + '\n' + extract
- soup = BeautifulSoup(page, 'html.parser')
- main_page = soup.text
- output.insert(tk.END, main_page)
- except Exception as e:
- result = e
- output.insert(tk.END, result)
- def display():
- pass
- def abort():
- window.destroy()
- myfont = ('times', 15, 'bold')
- font_1 = ('times', 13, 'bold')
- font_2 = ('times', 18, 'bold')
- font_3 = ('times', 14, 'bold')
- label_banner = tk.Label(text='My Wiki Search', bg='black', fg='gray')
- label_banner.config(font=font_2)
- label_banner.place(x=230, y=20, in_=window)
- label_search = tk.Label(text='Enter word:',
- bg='black', fg='white')
- label_search.config(font=font_1)
- label_search.place(x=15, y=80, in_=window)
- entry_search = tk.Entry(width=60, bg='gray')
- entry_search.config(font=myfont)
- entry_search.place(x=120, y=80, in_=window)
- input_go = tk.Text(width=25, height=2, bg='gray')
- input_go.config(font=myfont)
- input_go.place(x=780, y=80, in_=window)
- btn_display = tk.Button(text='Display',
- bg='blue', fg='black', bd=5, command=display)
- btn_display.config(font=font_3)
- btn_display.place(x=1050, y=80, in_=window)
- btn_search = tk.Button(text='Search',
- bg='blue', fg='black', bd=5, command=process)
- btn_search.config(font=font_3)
- btn_search.place(x=280, y=150, in_=window)
- btn_exit = tk.Button(text='Exit',
- bg='red', fg='black', bd=5, command=abort)
- btn_exit.config(font=font_3)
- btn_exit.place(x=380, y=150, in_=window)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement