1+ import tkinter as tk
2+ import requests
3+ from PIL import Image , ImageTk
4+
5+ # 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
16+ news_str = ""
17+ for article in result ['articles' ]:
18+ news_str += f"Title: { article ['title' ]} \n Description: { article ['description' ]} \n \n "
19+ return news_str
20+
21+ def save ():
22+ global Content , label
23+ Content = str (search .get ())
24+ news_content = news ()
25+ label .config (text = news_content )
26+
27+ window = tk .Tk ()
28+ window .geometry ("800x600" )
29+ window .title ("News_App" )
30+
31+ # Label for displaying news
32+ label = tk .Label (window , text = "" , wraplength = 750 , justify = "left" )
33+ label .place (x = 20 , y = 100 )
34+
35+ # Title label
36+ title_label = tk .Label (window , text = "NewsHub" , justify = "left" , font = ("Helvetica" , 50 ))
37+ title_label .place (x = 10 , y = 10 )
38+
39+ # 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
47+
48+ # Search entry field
49+ search = tk .Entry (window , font = ("Arial" , 20 ))
50+ search .place (x = 300 , y = 40 )
51+
52+ # Search button
53+ find = tk .Button (window , borderwidth = 3 , cursor = "hand2" , text = "Search" , highlightbackground = "black" , command = save )
54+ find .place (x = 650 , y = 40 )
55+
56+ window .mainloop ()
0 commit comments