A Simple News App with Tkinter and Newsapi
In this tutorial, we will create a simple news application using Tkinter and Newsapi. The app will display the top headlines from around the world.
But first, we need to understand what Tkinter and Newsapi are.
What is Tkinter?
Tkinter is a standard Python library used to create graphical user interfaces (GUIs). It provides a variety of tools for creating windows, buttons, sliders, and other GUI elements. Tkinter is easy to use and comes pre-installed with Python.
What is Newsapi?
Newsapi is a REST API used to get breaking news headlines and articles from around the world. With Newsapi, you can get the latest news from over 30,000 sources.
Installing Dependencies
Before we start coding, let’s install the necessary dependencies. First, we need to install requests and newsapi-python libraries using pip. Type the following command in your command prompt to install these libraries:
!pip install requests
!pip install newsapi-python
Creating the GUI
The first step in building our news app is to create the GUI. We will use the Tkinter library to create the GUI.
Create a new Python file and import the following libraries:
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
Next, create a new tkinter window and give it a title:
# create the window
window = tk.Tk()
# set the title
window.title("News App")
Now, let’s create the widgets for our app. We will create a combo box to select the news source and a button to get the top headlines. We will also create a scrolled text widget to display the headlines.
# select news source label
source_label = ttk.Label(window, text="Select News Source:")
source_label.grid(column=0, row=0)
# news source combo box
source_combo = ttk.Combobox(window, width=25)
source_combo['values'] = ('ABC News', 'BBC News', 'CNN', 'Fox News', 'Reuters')
source_combo.current(0)
source_combo.grid(column=1, row=0)
# get news button
news_button = ttk.Button(window, text="Get News")
news_button.grid(column=2, row=0)
# scrolled text widget
news_text = scrolledtext.ScrolledText(window, width=60, height=10)
news_text.grid(column=0, row=1, columnspan=3)
Now that our widgets are in place, we need to define the function to get the top headlines. We will use the Newsapi library to get the top headlines based on the selected news source.
from newsapi import NewsApiClient
import requests
def get_headlines():
# get the selected news source
selected_source = source_combo.get()
# create a news api client object
newsapi = NewsApiClient(api_key='YOUR_API_KEY')
# get the top headlines based on the selected news source
top_headlines = newsapi.get_top_headlines(sources=selected_source.lower())
# display the headlines in the scrolled text widget
for headline in top_headlines['articles']:
news_text.insert(tk.END, f"{headline['title']}\n\n{headline['description']}\n\n")
We will also need to bind the get_headlines function to the news_button widget:
news_button.config(command=get_headlines)
Lastly, let’s run the main loop so the GUI window remains visible:
window.mainloop()
Full Code
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from newsapi import NewsApiClient
def get_headlines():
# get the selected news source
selected_source = source_combo.get()
# create a news api client object
newsapi = NewsApiClient(api_key='YOUR_API_KEY')
# get the top headlines based on the selected news source
top_headlines = newsapi.get_top_headlines(sources=selected_source.lower())
# display the headlines in the scrolled text widget
for headline in top_headlines['articles']:
news_text.insert(tk.END, f"{headline['title']}\n\n{headline['description']}\n\n")
# create the window
window = tk.Tk()
# set the title
window.title("News App")
# select news source label
source_label = ttk.Label(window, text="Select News Source:")
source_label.grid(column=0, row=0)
# news source combo box
source_combo = ttk.Combobox(window, width=25)
source_combo['values'] = ('ABC News', 'BBC News', 'CNN', 'Fox News', 'Reuters')
source_combo.current(0)
source_combo.grid(column=1, row=0)
# get news button
news_button = ttk.Button(window, text="Get News")
news_button.grid(column=2, row=0)
news_button.config(command=get_headlines)
# scrolled text widget
news_text = scrolledtext.ScrolledText(window, width=60, height=10)
news_text.grid(column=0, row=1, columnspan=3)
# run the main loop
window.mainloop()
Note: Replace ‘YOUR_API_KEY’ with your own Newsapi API key.
Save the file as ‘news_app.py’ and run it in the command prompt by typing the following command:
python news_app.py
Conclusion
In this tutorial, we learned how to use Tkinter and Newsapi to create a simple news app. We created a GUI using Tkinter and used the Newsapi library to get the top headlines based on the selected news source. We then displayed the headlines in a scrolled text widget. With a little bit of practice and experimentation, you can add more features to the app, such as filtering news by category or adding a search bar.
Happy coding!