How to Build a News Application Using Python

In this fast-paced world, staying updated with the latest news is crucial. Build your very own News application that gets you news across different categories such as entertainment, sports, and technology accessible on any platform right at your fingertips.

You will develop this application using Tkinter, the standard GUI library for Python, and power it using the News API that gets articles from more than 80,000 sources.

4

Install tkinter and requests Modules

Tkinter is a cross-platform, simple, and user-friendly modulethat you can use to create graphical user interfaces rapidly. Some of theapplications you can build using Tkinter include a music player, calendar, weight conversion tool, word jumble game, and so on.

To installtkinterin your system, run the following command in the terminal:

Part of newspaper

Therequestsmodule makes it easier to make HTTP requests. With this,you can develop interesting applications such as a website status checker, web scraper, stock market monitor bot, and website performance tester. To install the requests module, open the terminal and run the following command:

You can find the entire source code of the News application using Python in thisGitHub repository.

Output of News Application

Generate the News API Key

Begin by importing the required libraries. Signup atNews APIand generate your API key. With the free developer account, you can send up to 100 requests daily, which is great to test and get started. Store the API in a variable that you will use later with the base URL to fetch the top headlines according to your country.

Define the Structure of the App

Define a class,NewsApp. Reference theapiKeyandtypevariables globally. Define an object of the class and initialize the root window. Set the dimensions and title of your application. Define two lists, you will use the first list to define the buttons and the second to define to categories of news you want to display in your application.

Define a label that acts as a title to your application. Pass the parent element you want to place it in, the text it should display, the font style, the padding, and the background color it should have. Use thepack()fill option to make the label occupy the entire space in the X direction.

chatgpt openai logo on green background feature

Define aLabelFrameand set its parent element along with the background color it should possess. Use theplace()function to organize it at specified coordinates. Additionally, pass the width and height of the frame.

Define a for loop that runs through the length of thenewsCatlist. Define a button in the frame defined earlier. Pass the text it should display and convert it into uppercase. Pass the width, the border depth, the font style, and the background color of the buttons. Use thegridmanager to arrange the buttons in a columnar format and add padding in the X and Y directions.

A photo of a television displaying the McMillions home page on HBO Max

Use the bind method to attach the button and the news area.defines that theNewsareafunction will occur on the left mouse click by the user. Append these buttons to thenewsCatButtonlist defined earlier.

Define a frame and pass it the parent element you want to place it in and the border depth it should have. Organize it at specified coordinates and pass the relative height and width. Define a vertical scrollbar using theorientparameter and place it in this frame.

Define a text widget. Pass the parent element as this frame along with the font style and the background color it should possess. On setting the value of theyscrollcommandasscroll_y.setit gets the current position of the scrollbar on user interaction.

Use thepack()fill option to place the scrollbar on the right side of the frame and occupy the entire space in the Y direction. On setting the command parameter astxtarea.yview, the scrollbar’s movement gets linked to the up and down functions. So, when the user interacts with the scrollbar, the text area’s views change accordingly.

Use theinsert()method to ask the user to select a category and place it at the end of any existing text. Use thepack()method to ask the text widget to take the space in the X and Y direction and assign additional space if necessary using theexpandparameter.

Extract News From the API Response

Define a function,Newsarea()that accepts the current instance of the class and the button event. Pass the category of the news the user selected in lowercase and store it. Pass the required parameters to the base URL and delete any text present earlier from the first index to the last one. Insert a line for demarcation.

Define atryblock and send an HTTP GET request to the server defined in the base URL. Convert the response into JSON format and extract the contents that have the key asarticlesand store it in a variable.

If the number of articles fetched is not zero, run a loop and insert the articles one by one in the text widget. Display the title, followed by the description, content, and URL in separate lines. Insert two lines for demarcation. If the number of articles is zero, display that no news is available for that particular category.

If the try block fails, use the exception block to display the appropriate error message to the users.

Create an instance of the class and initialize it. Themainloop()function tells Python to run the Tkinter event loop and listen for events until you close the window.

Put all the code together and your application is ready to display news according to different categories.

The Output of the News Application

On running the program, the text area asks to select a category. On clicking any of the buttons, it displays the news with the description, content, and its link (if present) for that category separated by two lines.

Useful APIs for Your Python Project

APIs are super handy to integrate new applications with existing software systems. You can use the OpenWeatherMap API to retrieve real-time weather information of any area and Google Maps API to incorporate maps into your website application. You can automate GitHub tasks via the GitHub API and use Zoom API for integrating video conferencing.

you’re able to also use the power of ChatGPT from within your own apps using OpenAI’s API and create some fascinating AI-powered applications.

Use the power of ChatGPT from within your own apps using OpenAI’s API and this guide.

Not all true crime is about hacking, slashing, and gore.

You’ve been quoting these famous films wrong all along!

It saves me hours and keeps my sanity intact.

Don’t let someone else take over your phone number.

It’s not super flashy, but it can help to keep your computer up and running.

Technology Explained

PC & Mobile