Fetcher App

Fetcher App Using Python and Tkinter

Are you ready to build a fun and useful Python application? In this blog post, we will create a simple graphical user interface (GUI) app that fetches random advice from the web and displays it to the user. This project is a great way to practice working with APIs, error handling, and the Tkinter library.

What Will You Learn?

  • How to fetch data from a public API using the requests library.
  • The basics of creating a GUI application using the tkinter library.
  • How to handle errors gracefully in Python.

The Plan

  1. Fetch random advice from the Advice Slip API.
  2. Display the advice in a Tkinter window.
  3. Allow the user to fetch new advice at the click of a button.

Prerequisites

  • requests: For fetching advice from the API.
  • tkinter: For building the GUI (this comes pre-installed with Python).

The Code

Here’s the complete Python code for our app:

# Importing libraries
import requests
import tkinter as tk
from tkinter import messagebox

# Fetching advice from the advice API
def advice():
    try:
        res = requests.get("https://api.adviceslip.com/advice").json()
        advice_text.set(res["slip"]["advice"])
    except requests.exceptions.RequestException:
        messagebox.showerror(
            "Error", "Failed to fetch advice. Please check your internet connection."
        )

# Create the main window
root = tk.Tk()
root.title("Random Advisor Application")

# Create and configure widgets
advice_text = tk.StringVar()
advice_label = tk.Label(
    root, textvariable=advice_text, wraplength=400, font=("Arial", 14)
)
get_advice_button = tk.Button(root, text="Get Advice", command=advice)

# Pack widgets
advice_label.pack(pady=20)
get_advice_button.pack(pady=10)

# Initial advice fetching
advice()

# Run the main event loop
root.mainloop()

How It Works

1. Importing Libraries

  • requests to send HTTP requests and fetch advice from the API.
  • tkinter and messagebox to create the GUI and display error messages.

2. Fetching Advice

  • Sends a GET request to the Advice Slip API.
  • Parses the JSON response to extract the advice text.
  • Updates the advice_text variable to display the advice in the label.
  • Handles errors by showing a popup if the API request fails.

3. Building the GUI

  • root: The main application window.
  • advice_label: A label to display the advice text, dynamically updated using a StringVar.
  • get_advice_button: A button that fetches new advice when clicked.

4. Running the Application

The root.mainloop() method starts the event loop, keeping the application responsive to user interactions.

Features of the App

  • Dynamic Updates: The advice text updates automatically when fetched from the API.
  • Error Handling: Graceful error messages appear if the app fails to fetch advice due to network issues.
  • User-Friendly Interface: A simple and intuitive interface makes it easy to use.

Running the App

  1. Save the code to a file (e.g., advice_app.py).
  2. Open a terminal or command prompt and navigate to the file’s directory.
  3. Run the file using: python advice_app.py
  4. The app window will appear, displaying random advice. Click the button to fetch new advice!

Possible Extensions

  • Add Styling: Use colors, custom fonts, or images to enhance the UI.
  • Save Advice: Allow users to save their favorite pieces of advice to a file.
  • Multiple APIs: Fetch data from other APIs for jokes, quotes, or fun facts.

Post Comment