1- import tkinter as tk
2- import requests
1+ import tkinter as Tk
32from PIL import Image , ImageTk
43
54# Defining global variables
6- Content = ""
7- News_run = ""
8-
9- def news ():
10- global Content
11- ApiKey = "Your_Api_Key" # Get your API key
12- url = f"https://newsapi.org/v2/everything?q={ Content } &from=2025-01-27&sortBy=popularity&apiKey={ ApiKey } "
13- response = requests .get (url )
14- result = response .json ()
15- # Create a formatted string from the result
5+ content = ""
6+ news_content = ""
7+
8+ def fetch_news ():
9+ global content
10+ api_key = "Your_Api_Key"
11+ url = f"https://newsapi.org/v2/everything?q={ content } &from=2025-01-27&sortBy=popularity&apiKey={ api_key } "
12+
13+ try :
14+ response = requests .get (url )
15+ response .raise_for_status ()
16+ result = response .json ()
17+ except requests .exceptions .RequestException as e :
18+ return f"An error occurred: { e } "
19+
1620 news_str = ""
17- for article in result [ 'articles' ] :
21+ for article in result . get ( 'articles' , []) :
1822 news_str += f"Title: { article ['title' ]} \n Description: { article ['description' ]} \n \n "
1923 return news_str
2024
2125def save ():
22- global Content , label
23- Content = str ( search .get () )
24- news_content = news ()
25- label .config (text = news_content )
26+ global content , label
27+ content = search .get ()
28+ news_str = fetch_news ()
29+ label .config (text = news_str )
2630
27- window = tk .Tk ()
31+ window = Tk .Tk ()
2832window .geometry ("800x600" )
29- window .title ("News_App " )
33+ window .title ("News App " )
3034
3135# Label for displaying news
32- label = tk .Label (window , text = "" , wraplength = 750 , justify = "left" )
36+ label = Tk .Label (window , text = "" , wraplength = 750 , justify = "left" )
3337label .place (x = 20 , y = 100 )
3438
3539# Title label
36- title_label = tk .Label (window , text = "NewsHub" , justify = "left" , font = ("Helvetica" , 50 ))
40+ title_label = Tk .Label (window , text = "NewsHub" , justify = "left" , font = ("Helvetica" , 50 ))
3741title_label .place (x = 10 , y = 10 )
3842
3943# Display image
40- img = Image .open ("D:\\ Downloads\\ img.png" )
41- photo_img = ImageTk .PhotoImage (img )
42- my_img = tk .Label (window , image = photo_img , justify = "right" )
43- my_img .place (x = 850 , y = 0 )
44-
45- # Keep a reference to the image to avoid garbage collection
46- photo_img .image = photo_img
44+ try :
45+ img = Image .open ("D:\\ Downloads\\ img.png" )
46+ photo_img = ImageTk .PhotoImage (img )
47+ my_img = Tk .Label (window , image = photo_img , justify = "right" )
48+ my_img .place (x = 850 , y = 0 )
49+ # Keep a reference to the image to avoid garbage collection
50+ photo_img .image = photo_img
51+ except Exception as e :
52+ print (f"Error loading image: { e } " )
4753
4854# Search entry field
49- search = tk .Entry (window , font = ("Arial" , 20 ))
55+ search = Tk .Entry (window , font = ("Arial" , 20 ))
5056search .place (x = 300 , y = 40 )
5157
5258# Search button
53- find = tk .Button (window , borderwidth = 3 , cursor = "hand2" , text = "Search" , highlightbackground = "black" , command = save )
59+ find = Tk .Button (window , borderwidth = 3 , cursor = "hand2" , text = "Search" , highlightbackground = "black" , command = save )
5460find .place (x = 650 , y = 40 )
5561
56- window .mainloop ()
62+ window .mainloop ()
0 commit comments