A simple News app with Tkinter and Newsapi


Tkinter is a Python library that is used to make desktop applications for Windows and UNIX-based Operating systems. Tkinter gives many choices to make a widget for the application. The same widget can be made in different ways using Tkinter.

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?

News API is an (Application programming interface) that provides access to news articles and breaking news across the world through JSON web API. The News API simply gives a REST API which developers can use to simply get all the news articles, headlines, etc. in JSON format.

How to create your API key

  • To use News API you have to create your API key by going to the News API website.

  • Click on GetApiKey button on the top Right corner.

  • Fill your basic details in the form that appears.

  • Once you submit your basic details you registration will be completed and you will be given your API key.

Steps For Making a Webapp Using Tkinter And Newsapi

Step 1 - Install Tkinter And News Api

Before starting to implement the web app you need to install the Tkinter library and news API in Python. Open the command prompt or terminal and type the pip install command.

pip install tk
pip install newsapi-python

Pip is a Python package manager. The above commands install tkinter and newsapi in your local file system.

Step 2 - Import the Required Module

The module tkinter and newsapi are imported to use them in making a simple News application.

import tkinter as tk
from newsapi import NewsApiClient

Step 3 - Create a News API Client Object

Once all the libraries are installed, create a NewsAPI client object and initialize it with the API key you created from the News API website.

newsapi = NewsApiClient(api_key='your_api_key_here')

Replace your_api_key_here with the API you created from the NEWS API website.

Step 4 - Create a Function to Get the Latest News Article

We will create a function get_news() which will retrieve the latest news headlines and display them as a text widget in our app. Also, we need to clear the screen before retrieving new news article headlines.

def get_news():
   # Retrieve the top headlines
   top_headlines = newsapi.get_top_headlines(language='en')

   # Clear the text widget
   text.delete(1.0, tk.END)

   # Display the top headlines
   for article in top_headlines['articles']:
      text.insert(tk.END, article['title'] + '\n\n')

Step 5 - Create a Simple User Interface

Now we will create a simple user interface for our app and display the news headlines as text widget inside the app. A button at the bottom of the app shows “GET NEWS” which will retrieve the news headlines from the news API and display them on the app screen.

# Create the main window
root = tk.Tk()
root.title('News App')

# Create the text widget
text = tk.Text(root, height=20, width=50)
text.pack()

# Create the button
button = tk.Button(root, text='Get News', command=get_news)
button.pack()

# Run the main loop
root.mainloop()

The full code of the Simple web app can be found below −

Example

import tkinter as tk
from newsapi import NewsApiClient
newsapi = NewsApiClient(api_key='your_api_key_here')
def get_news():
   # Retrieve the top headlines
   top_headlines = newsapi.get_top_headlines(language='en')

   # Clear the text widget
   text.delete(1.0, tk.END)

   # Display the top headlines
   for article in top_headlines['articles']:
      text.insert(tk.END, article['title'] + '\n\n')
# Create the main window
root = tk.Tk()
root.title('News App')

# Create the text widget
text = tk.Text(root, height=20, width=50)
text.pack()

# Create the button
button = tk.Button(root, text='Get News', command=get_news)
button.pack()

# Run the main loop
root.mainloop()

Output

Conclusion

In this article, we understood how to build a simple web app using Tkinter and news API in python. We simply called the REST API provided by the news API and displayed the news headlines in a simple text widget in a user-friendly interface. The app can be extended to include more features like filtering news by topic or source, UI can be improved and other things can be added to make the app more interactive for the user.

Updated on: 17-Apr-2023

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements