-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews app with Tkinter and Newsapi.py
86 lines (75 loc) · 3.28 KB
/
News app with Tkinter and Newsapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from tkinter import *
from tkinter import messagebox
import requests
import json
type = 'sports'
apiKey = 'Use your own API-KEY'
#To get a API for news we will use newsapi.org. we will create account and take API key by clicking on get API button.
BASE_URL = f'http://newsapi.org/v2/top-headlines?country=in&category={type}&apiKey='+apiKey
class NewsApp:
global apiKey, type
def __init__(self, root):
self.root = root
self.root.geometry('1350x700+0+0')
self.root.title("eNewsPaper")
#====variables========#
self.newsCatButton = []
self.newsCat = ["general", "entertainment",
"business", "sports", "technology", "health"]
#========title frame===========#
bg_color = "#404040"
text_area_bg = "#b8e0c4"
basic_font_color = "#ccc4c4"
title = Label(self.root, text="NewsPaper Software", font=("times new roman", 30, "bold"),
pady=2, bd=12, relief=GROOVE, bg=bg_color, fg=basic_font_color).pack(fill=X)
F1 = LabelFrame(self.root, text="Category", font=(
"times new roman", 20, "bold"), bg=bg_color, fg=basic_font_color, bd=10, relief=GROOVE)
F1.place(x=0, y=80, width=300, relheight=0.88)
for i in range(len(self.newsCat)):
b = Button(F1, text=self.newsCat[i].upper(
), width=20, bd=7, font="arial 15 bold")
b.grid(row=i, column=0, padx=10, pady=5)
b.bind('<Button-1>', self.Newsarea)
self.newsCatButton.append(b)
#=======news frame=======#
F2 = Frame(self.root, bd=7, relief=GROOVE)
F2.place(x=320, y=80, relwidth=0.7, relheight=0.8)
news_title = Label(F2, text="News Area", font=(
"arial", 20, "bold"), bd=7, relief=GROOVE).pack(fill=X)
scroll_y = Scrollbar(F2, orient=VERTICAL)
self.txtarea = Text(F2, yscrollcommand=scroll_y.set, font=(
"times new roman", 15, "bold"), bg=text_area_bg, fg="#3206b8")
scroll_y.pack(side=RIGHT, fill=Y)
scroll_y.config(command=self.txtarea.yview)
self.txtarea.insert(
END, "PLEASE SELECT ANY CATEGORY TO SHOW HEADLINES AND PLEASE BE PATIENT IT DEPENDS ON UR INTERNET CONNECTIONS!!!!")
self.txtarea.pack(fill=BOTH, expand=1)
def Newsarea(self, event):
type = event.widget.cget('text').lower()
BASE_URL = f'http://newsapi.org/v2/top-headlines?country=in&category={type}&apiKey='+apiKey
self.txtarea.delete("1.0", END)
self.txtarea.insert(END, f"\n Welcome to GFG news paper\n")
self.txtarea.insert(
END, "--------------------------------------------------------------------\n")
try:
articles = (requests.get(BASE_URL).json())['articles']
if(articles != 0):
for i in range(len(articles)):
self.txtarea.insert(END, f"{articles[i]['title']}\n")
self.txtarea.insert(
END, f"{articles[i]['description']}\n\n")
self.txtarea.insert(END, f"{articles[i]['content']}\n\n")
self.txtarea.insert(
END, f"read more...{articles[i]['url']}\n")
self.txtarea.insert(
END, "--------------------------------------------------------------------\n")
self.txtarea.insert(
END, "--------------------------------------------------------------------\n")
else:
self.txtarea.insert(END, "Sorry no news available")
except Exception as e:
messagebox.showerror(
'ERROR', "Sorry cant connect to internet or some issues with newsapp :'(")
root = Tk()
obj = NewsApp(root)
root.mainloop()