Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
A simple News app with Tkinter and Newsapi
Tkinter is a Python library that is used to create desktop applications for Windows and UNIX-based operating systems. Tkinter provides many widgets to build interactive applications with graphical user interfaces.
Today there are vast sources of information available on the internet. News is constantly coming from global sources to local sources. Keeping track of the latest news is a daunting task. In this article, we will build a simple News app with Tkinter and NewsAPI.
What is NewsAPI?
NewsAPI is an Application Programming Interface (API) that provides access to news articles and breaking news across the world through JSON web API. The NewsAPI provides a REST API which developers can use to get all the news articles, headlines, etc. in JSON format.
How to Create Your API Key
To use NewsAPI you need to create your API key by following these steps ?
Go to the NewsAPI website
Click on "Get API Key" button in the top right corner
Fill your basic details in the registration form
Once you submit your details, registration will be completed and you will receive your API key
Installation Requirements
Before starting to implement the news app, you need to install the required libraries. Open the command prompt or terminal and run ?
pip install tk pip install newsapi-python
Complete News App Code
Here's the complete implementation of a simple news app using Tkinter and NewsAPI ?
import tkinter as tk
from newsapi import NewsApiClient
# Initialize NewsAPI client (replace with your actual API key)
newsapi = NewsApiClient(api_key='demo_key_for_testing')
def get_news():
try:
# Retrieve the top headlines
top_headlines = newsapi.get_top_headlines(language='en', page_size=10)
# Clear the text widget
text.delete(1.0, tk.END)
# Display the top headlines
if top_headlines['articles']:
for i, article in enumerate(top_headlines['articles'], 1):
title = article.get('title', 'No title available')
source = article.get('source', {}).get('name', 'Unknown source')
text.insert(tk.END, f"{i}. {title}\n")
text.insert(tk.END, f" Source: {source}\n\n")
else:
text.insert(tk.END, "No news articles found.")
except Exception as e:
text.delete(1.0, tk.END)
text.insert(tk.END, f"Error fetching news: {str(e)}")
# Create the main window
root = tk.Tk()
root.title('News App')
root.geometry('600x500')
# Create title label
title_label = tk.Label(root, text="Latest News Headlines",
font=('Arial', 16, 'bold'), pady=10)
title_label.pack()
# Create the text widget with scrollbar
frame = tk.Frame(root)
frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=5)
text = tk.Text(frame, height=20, width=70, wrap=tk.WORD, font=('Arial', 10))
scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=text.yview)
text.config(yscrollcommand=scrollbar.set)
text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Create the button
button = tk.Button(root, text='Get Latest News', command=get_news,
font=('Arial', 12), bg='#4CAF50', fg='white', pady=5)
button.pack(pady=10)
# Initial message
text.insert(tk.END, "Click 'Get Latest News' to fetch the latest headlines.")
# Run the main loop
root.mainloop()
Latest News Headlines 1. Sample News Title 1 Source: BBC News 2. Sample News Title 2 Source: CNN 3. Sample News Title 3 Source: Reuters
Key Features
The news app includes the following features ?
API Integration: Uses NewsAPI to fetch real-time news headlines
User Interface: Clean Tkinter GUI with text widget and scrollbar
Error Handling: Handles API errors and connection issues gracefully
Source Information: Displays both news title and source name
Responsive Design: Includes scrollbar for viewing multiple articles
How It Works
The application works in the following steps ?
Initialize NewsAPI client with your API key
Create Tkinter window with text widget and button
When button is clicked,
get_news()function is calledFunction fetches top headlines using
get_top_headlines()Headlines are displayed in the text widget with source information
Possible Enhancements
You can extend this app with additional features ?
Filter news by category (sports, technology, business)
Search news by keywords
Display article descriptions and publication dates
Add clickable links to full articles
Implement news caching for offline viewing
Conclusion
We successfully created a simple news application using Tkinter and NewsAPI. The app fetches real-time news headlines and displays them in a user-friendly interface. This project demonstrates how to integrate external APIs with desktop GUI applications in Python.
