Skip to content

Commit 1296abf

Browse files
Error Fixed
1 parent 4cd5bd9 commit 1296abf

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

NewsHub_Python/Newshub.py

+31-36
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,57 @@
1-
import tkinter as Tk
1+
import tkinter as tk
2+
import requests
23
from PIL import Image, ImageTk
34

45
# Defining global variables
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-
6+
Content = ""
7+
News_run = ""
8+
9+
def news():
10+
global Content
11+
ApiKey = "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
2016
news_str = ""
21-
for article in result.get('articles', []):
17+
for article in result['articles']:
2218
news_str += f"Title: {article['title']}\nDescription: {article['description']}\n\n"
2319
return news_str
2420

2521
def save():
26-
global content, label
27-
content = search.get()
28-
news_str = fetch_news()
29-
label.config(text=news_str)
22+
global Content, label
23+
Content = str(search.get())
24+
news_content = news()
25+
label.config(text=news_content)
3026

31-
window = Tk.Tk()
27+
window = tk.Tk()
3228
window.geometry("800x600")
33-
window.title("News App")
29+
window.title("News_App")
3430

3531
# Label for displaying news
36-
label = Tk.Label(window, text="", wraplength=750, justify="left")
32+
label = tk.Label(window, text="", wraplength=750, justify="left")
3733
label.place(x=20, y=100)
3834

3935
# Title label
40-
title_label = Tk.Label(window, text="NewsHub", justify="left", font=("Helvetica", 50))
36+
title_label = tk.Label(window, text="NewsHub", justify="left", font=("Helvetica", 50))
4137
title_label.place(x=10, y=10)
4238

4339
# Display image
44-
try:
45-
img = Image.open("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}")
40+
img = Image.open("img.pg")
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
5347

5448
# Search entry field
55-
search = Tk.Entry(window, font=("Arial", 20))
49+
search = tk.Entry(window, font=("Arial", 20))
5650
search.place(x=300, y=40)
5751

5852
# Search button
59-
find = Tk.Button(window, borderwidth=3, cursor="hand2", text="Search", highlightbackground="black", command=save)
53+
find = tk.Button(window, borderwidth=3, cursor="hand2", text="Search", highlightbackground="black", command=save)
6054
find.place(x=650, y=40)
6155

6256
window.mainloop()
57+

0 commit comments

Comments
 (0)