22from gnewsclient import gnewsclient
33from gnewsclient import utils
44from tkinter import ttk
5+ from PIL import Image
6+ import urllib .request
7+ import webbrowser
8+
9+ def gotolink (event , x ):
10+ webbrowser .open_new (x )
11+
12+
13+ class resizingCanvas (Canvas ):
14+ def on_resize (self ,event ):
15+ # determine the ratio of old width/height to new width/height
16+ wscale = float (event .width )/ self .width
17+ hscale = float (event .height )/ self .height
18+ self .width = event .width
19+ self .height = event .height
20+ # resize the canvas
21+ self .config (width = self .width , height = self .height )
22+ # rescale all the objects tagged with the "all" tag
23+ self .scale ("all" ,0 ,0 ,wscale ,hscale )
24+ self .configure (scrollregion = self .bbox ('all' ))
25+
26+ # def canvas_configure(self, event):
27+ def __init__ (self ,parent ,** kwargs ):
28+ Canvas .__init__ (self ,parent ,** kwargs )
29+ self .bind ("<Configure>" , self .on_resize )
30+ self .height = self .winfo_reqheight ()
31+ self .width = self .winfo_reqwidth ()
32+
33+
34+
35+ # function to load news in the east frame(level 1)
36+ # see line numbers 189-234 for detailed explanation
37+ def show_news ():
38+ global east_frame , myscrollbar , frame ,canvas , photo
39+ east_frame .destroy () # destroy old news' frame
40+ east_frame = Frame (root , relief = GROOVE , bd = 1 ) # new frame for fresh news
41+ east_frame .pack (side = RIGHT )
42+
43+ canvas = resizingCanvas (east_frame )
44+
45+ frame = Frame (canvas )
46+ myscrollbar = Scrollbar (east_frame , orient = "vertical" , command = canvas .yview )
47+ canvas .configure (yscrollcommand = myscrollbar .set )
48+ myscrollbar .pack (side = "right" , fill = "y" )
49+
50+ canvas .pack (side = "left" , padx = 30 , pady = 30 )
51+ canvas .create_window ((0 , 0 ), window = frame , anchor = 'nw' )
52+
53+ nws = client .get_news ()
54+
55+ l = len (nws )
56+ photo = {}
57+ for i in range (l ):
58+ try :
59+ imzlnk = nws [i ]['img' ]
60+ imz = Image .open (urllib .request .urlopen (imzlnk ))
61+ imz .save ('file' + str (i ) + '.gif' )
62+ f = Frame (frame )
63+ lnk = nws [i ]['link' ]
64+ ttle = nws [i ]['title' ]
65+ photo [i ] = PhotoImage (file = 'file' + str (i ) + '.gif' )
66+ photolabel = Label (f , image = photo [i ]).grid (row = 0 , column = 0 , rowspan = 3 , sticky = W , pady = 10 )
67+ ttlelabel = Label (f , text = str (ttle ), font = ("Helvetica" , 12 )).grid (row = 0 , column = 1 , sticky = W , pady = 10 )
68+ read_more = Label (f , text = "Read more about this" , fg = "blue" , cursor = "hand2" )
69+ read_more .grid (row = 1 , column = 1 , sticky = W )
70+ read_more .bind ("<Button-1>" , lambda event , link = str (lnk ): gotolink (event , link ))
71+ f .grid (column = 1 , row = i , sticky = W )
72+ except AttributeError :
73+ imzlnk = nws [i ]['img' ]
74+ f = Frame (frame )
75+ lnk = nws [i ]['link' ]
76+ ttle = nws [i ]['title' ]
77+ ttlelabel = Label (f , text = str (ttle ), font = ("Helvetica" , 12 )).grid (row = 0 , column = 1 , sticky = W )
78+ read_more = Label (f , text = "Read more about this" , fg = "blue" , cursor = "hand2" )
79+ read_more .grid (row = 1 , column = 1 , sticky = W )
80+ read_more .bind ("<Button-1>" , lambda event , link = str (lnk ): gotolink (event , link ))
81+ f .pack ()
82+ return
83+
84+
85+
586
687
788# function to set attributes(language, location, topic), and then fetch news accordingly
@@ -25,9 +106,7 @@ def news():
25106 client .location = None
26107
27108 update_status (client ,status )
28- for i in client .get_news ():
29- print (i )
30- print ("\n " )
109+ show_news ()
31110 return
32111
33112
@@ -36,7 +115,8 @@ def search():
36115 global search_query ,client ,status
37116 client .query = search_query .get ()
38117 update_status (client ,status )
39- print (client .get_news ())
118+ # print(client.get_news())
119+ show_news ()
40120 return
41121
42122# helper function for status bar
@@ -73,7 +153,7 @@ def update_status(client, status):
73153search_button = Button (search_frame ,text = "search" , command = search ).grid (row = 1 , column = 1 )
74154
75155west_frame = Frame (root )
76- west_frame .pack (side = LEFT )
156+ west_frame .pack (side = LEFT , padx = 10 )
77157
78158
79159# frame for topic, location, and edition filters
@@ -106,12 +186,56 @@ def update_status(client, status):
106186location_query = StringVar ()
107187location = Entry (tle_frame , textvariable = location_query ).grid (row = 4 , column = 1 ,pady = 50 )
108188
109-
189+ # frame to enclose all the stuff related to show news(level 1)
190+ east_frame = Frame (root ,relief = GROOVE ,bd = 1 )
191+ east_frame .pack (side = RIGHT )
192+
193+ # canvas which keeps the news related stuff(level 2)
194+ canvas = resizingCanvas (east_frame )
195+
196+ # the frame inside canvas which shows news and stuff(level 3)
197+ frame = Frame (canvas )
198+ # scrollbar so that the canvas can be scrolled(level 2)
199+ myscrollbar = Scrollbar (east_frame , orient = "vertical" , command = canvas .yview )
200+ canvas .configure (yscrollcommand = myscrollbar .set )
201+ myscrollbar .pack (side = "right" ,fill = "y" )
202+
203+ canvas .pack (side = "left" , padx = 30 , pady = 30 )
204+ canvas .create_window ((0 ,0 ),window = frame ,anchor = 'nw' )
205+
206+ # news list which has dictionaries for different news articles
207+ nws = client .get_news () # has the default filtering parameters
208+ l = len (nws )
209+ photo = {} # dictionary which maps numbers with PhotoImage objects
210+ for i in range (l ):
211+ try :
212+ imzlnk = nws [i ]['img' ]
213+ imz = Image .open (urllib .request .urlopen (imzlnk ))
214+ imz .save ('file' + str (i )+ '.gif' )
215+ f = Frame (frame ) # frame to display a news article(one frame per article)(level 4)
216+ lnk = nws [i ]['link' ]
217+ ttle = nws [i ]['title' ]
218+ photo [i ] = PhotoImage (file = 'file' + str (i )+ '.gif' )
219+ photolabel = Label (f , image = photo [i ]).grid (row = 0 , column = 0 , rowspan = 3 , sticky = W , pady = 10 )
220+ ttlelabel = Label (f , text = str (ttle ), font = ("Helvetica" , 12 )).grid (row = 0 , column = 1 , sticky = W , pady = 10 , padx = 5 )
221+ # ttlelabel.config()
222+ read_more = Label (f , text = "Read more about this" , fg = "blue" , cursor = "hand2" )
223+ read_more .grid (row = 1 , column = 1 , sticky = W , padx = 5 )
224+ read_more .bind ("<Button-1>" , lambda event , link = str (lnk ): gotolink (event , link ))
225+ f .grid (column = 1 , row = i , sticky = W )
226+ except AttributeError :# incase there is no photo found, no photo rendering done here
227+ imzlnk = nws [i ]['img' ]
228+ f = Frame (frame )
229+ lnk = nws [i ]['link' ]
230+ ttle = nws [i ]['title' ]
231+ ttlelabel = Label (f , text = str (ttle ), font = ("Helvetica" , 12 )).grid (row = 0 , column = 1 , sticky = W )
232+ read_more = Label (f , text = "Read more about this" , fg = "blue" , cursor = "hand2" )
233+ read_more .grid (row = 1 , column = 1 , sticky = W )
234+ read_more .bind ("<Button-1>" , lambda event , link = str (lnk ): gotolink (event , link ))
235+ f .pack ()
110236
111237# button to fetch news
112238getnews = Button (tle_frame , command = news ,text = "Get News!" ).grid (row = 6 ,column = 0 ,columnspan = 2 , pady = 25 )
113-
114-
115-
116-
239+ img = PhotoImage (file = 'logo.png' )
240+ root .tk .call ('wm' , 'iconphoto' , root ._w , img )
117241root .mainloop ()
0 commit comments