Advertisement
collinsanele

A Wikipedia Search Desktop App Made Using Tkinter

Nov 21st, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import requests
  2. import json
  3. from bs4 import BeautifulSoup
  4. from pprint import pprint
  5.  
  6. import tkinter as tk
  7.  
  8. window = tk.Tk()
  9. window.configure(bg='black')
  10. window.title('Dictionary Search')
  11.  
  12. #Font for the Text Widget
  13. font_0 = ('times', 12, 'bold')
  14. #font_out = ('times', 16, 'bold')
  15.  
  16.  
  17. def process():
  18. ''' A Wikipedia Search App That Searches And Displays The Summary Of A Search Word.
  19. This App Was Written In Python 3.6 Using tkinter For The UI.
  20. Created By:collinsanele@gmail.com '''
  21. scrollbar = tk.Scrollbar(window, bg='white')
  22. scrollbar.place(x=1300, y=264)
  23. output = tk.Text(window, width=150, height=20,bg='gray', fg='black', font=font_0,
  24. yscrollcommand=scrollbar.set)
  25. output.place(x=84, y=260, in_=window)
  26. scrollbar.config(command=output.yview)
  27. search_input = str(entry_search.get())
  28. u1 ='http://en.wikipedia.org/w/api.php?action=query&prop=extracts&'
  29. u2 = 'format=json&exintro=&titles='+search_input
  30. url = u1 + u2
  31. r = requests.get(url).json()
  32. #content = r['query']['pages']
  33. #content = json.dumps(content)
  34. try:
  35. for k,v in r['query']['pages'].items():
  36. key = str(k)
  37. input_go.insert(tk.END, key)
  38. title = r['query']['pages'][key]['title']
  39. extract = r['query']['pages'][key]['extract']
  40. page = title +'\n' + '\n' + extract
  41. soup = BeautifulSoup(page, 'html.parser')
  42. main_page = soup.text
  43. output.insert(tk.END, main_page)
  44. except Exception as e:
  45. result = e
  46. output.insert(tk.END, result)
  47.  
  48.  
  49. def display():
  50. pass
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. def abort():
  59. window.destroy()
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. myfont = ('times', 15, 'bold')
  67. font_1 = ('times', 13, 'bold')
  68. font_2 = ('times', 18, 'bold')
  69. font_3 = ('times', 14, 'bold')
  70.  
  71.  
  72.  
  73.  
  74.  
  75. label_banner = tk.Label(text='My Wiki Search', bg='black', fg='gray')
  76. label_banner.config(font=font_2)
  77. label_banner.place(x=230, y=20, in_=window)
  78.  
  79. label_search = tk.Label(text='Enter word:',
  80. bg='black', fg='white')
  81. label_search.config(font=font_1)
  82. label_search.place(x=15, y=80, in_=window)
  83.  
  84. entry_search = tk.Entry(width=60, bg='gray')
  85. entry_search.config(font=myfont)
  86. entry_search.place(x=120, y=80, in_=window)
  87.  
  88. input_go = tk.Text(width=25, height=2, bg='gray')
  89. input_go.config(font=myfont)
  90. input_go.place(x=780, y=80, in_=window)
  91.  
  92. btn_display = tk.Button(text='Display',
  93. bg='blue', fg='black', bd=5, command=display)
  94. btn_display.config(font=font_3)
  95. btn_display.place(x=1050, y=80, in_=window)
  96.  
  97.  
  98. btn_search = tk.Button(text='Search',
  99. bg='blue', fg='black', bd=5, command=process)
  100. btn_search.config(font=font_3)
  101. btn_search.place(x=280, y=150, in_=window)
  102.  
  103. btn_exit = tk.Button(text='Exit',
  104. bg='red', fg='black', bd=5, command=abort)
  105. btn_exit.config(font=font_3)
  106. btn_exit.place(x=380, y=150, in_=window)
  107.  
  108.  
  109.  
  110.  
  111.  
  112. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement