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