-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
148 lines (111 loc) · 4.46 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from tkinter import *
from PIL import Image, ImageTk
import tkinter.messagebox as tmb
import requests
import urllib.parse
import io
apiKey = ''
url = 'https://newsapi.org/v2/top-headlines?country=in&category=technology&pageSize=10&apiKey=' + apiKey
r = requests.get(url).json()
# create instance
window = Tk()
# set default window size
window.geometry('900x550')
# set min window size
window.minsize(700, 400)
# set max window size
window.maxsize(1100, 600)
# add a title
window.title("News Blog")
def subscribeUser():
with open('NewsBlogSubscribers.txt', 'a') as f:
f.write(f"{userName.get(), userEmail.get(), userPhone.get()}\n")
def rateUs():
a = tmb.askokcancel("Rate", "Would you like to rate us now?")
if a==True:
tmb.showinfo("Rated", "Thank you for taking your time to rate us.")
else:
tmb.showinfo("Not Rated", "We are looking forward to serve in much better way.\nThank You!")
def help():
tmb.showinfo("Help","We are glad that you asked for help.\nWe will soon resolve your issue. Kindly wait for some time.\nThank You")
def error():
tmb.showerror("Error", "This is an error message.")\
def submit():
#tmb.showinfo("Slider", f"You've selected {myslider.get()} on slider.")
tmb._show(title='Slider', message=f"You've selected {myslider.get()} on slider.", icon='info')
def getFormattedText(text):
count = 1
formattedText = ''
for ch in text:
if(count%80 == 0):
formattedText += '\n'
formattedText += ch
count += 1
return formattedText
frames = []
photos = []
def populate():
#for i in range(len(r['articles'])):
for i in range(2):
frame = Frame(window, borderwidth=4, relief=RIDGE)
frame.pack(fill=Y, pady=10, padx=10)
print(r['articles'][i]['urlToImage'])
raw_data = urllib.request.urlopen(r['articles'][i]['urlToImage']).read()
image = Image.open(io.BytesIO(raw_data))
image = image.resize((100, 100), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
photos.append(photo)
image_label = Label(
master=frame,
image=photo,
)
image_label.pack(side=LEFT, padx=2, pady=2)
text_frame = Frame(frame)
text_frame.pack(side=RIGHT)
title = Label(text_frame, fg='red',
text = getFormattedText(r['articles'][i]['title']),
font=("comicsans", 12, "bold"),
)
title.pack(padx=5, pady=5)
desc = Label(text_frame, text = getFormattedText(r['articles'][i]['description']))
#desc = Label(text_frame, text = r['articles'][i]['content'])
desc.pack(pady=10)
date = Label(text_frame, text=r['articles'][i]['publishedAt'])
date.pack(side=RIGHT, padx=2)
frames.append(frame)
frames[i].pack()
# --------------------- Menu Bar ---------------------
myMenuBar = Menu(window)
m1 = Menu(myMenuBar, tearoff=0)
m1.add_command(label='Rate Us', command=rateUs)
m1.add_command(label='Help', command=help)
m1.add_command(label='Error', command=error)
myMenuBar.add_cascade(label='Options', menu=m1)
myMenuBar.add_command(label='Exit', command=quit)
window.config(menu=myMenuBar)
# --------------------- Slider with Button ---------------------
slider_frame = Frame(window)
slider_frame.pack()
#Creating Slider/Scroll Bar
myslider = Scale(slider_frame, from_ = 0, to = 10, tickinterval=2, orient = HORIZONTAL)
myslider.pack(side=LEFT)
#To change the By-Default Value from 0 to 10
myslider.set(1)
Button(slider_frame, text='Submit', command=submit).pack(side=RIGHT)
# --------------------- Add news item to the window ---------------------
populate()
# --------------------- Subscription Form ---------------------
Label(window, text='Subscribe to our blog', font=("comicsans", 15, "bold")).pack()
form_frame = Frame(window)
form_frame.pack(side=LEFT)
userName = StringVar()
userEmail = StringVar()
userPhone = StringVar()
Label(form_frame, text='Name: ', padx=3, pady=3).grid(row=1, column=1)
Label(form_frame, text='Email: ', padx=3, pady=3).grid(row=2, column=1)
Label(form_frame, text='Phone: ', padx=3, pady=3).grid(row=3, column=1)
Entry(form_frame, textvariable=userName).grid(row=1, column=2)
Entry(form_frame, textvariable=userEmail).grid(row=2, column=2)
Entry(form_frame, textvariable=userPhone).grid(row=3, column=2)
Button(form_frame, text='Subscribe', command=subscribeUser).grid(row=4, column=1, padx=10, pady=10)
window.mainloop()