Lesson 8: APIs and Building GUI with Tkinter#
Owner: (sx Shirsat, Rushikesh)
Prerequisite topics:
Hyatt, Matt
suggestion only
Hyatt, Matt other suggestion:
Suggested Chapter Title -> Building GUI Apps with Tkinter and APIs in Python
NEW UPDATES
We have separated this chapter into two parts Part 1 is Understanding API’s & Part 2 is Building GUI with Tkinter
INDEX
Part 1: Understanding APIs#
1. Introduction to APIs#
What is an API (Application Programming Interface)?
Why APIs are used (communication between software applications)
Examples of real-world APIs (weather data, currency converter, YouTube, etc.)
2. Types of APIs#
Web APIs (REST, SOAP)
Local APIs / Library APIs
Public vs. Private APIs
3. How Web APIs Work#
Client-Server model
Endpoints and HTTP methods (GET , POST , PUT , DELETE)
Understanding JSON (JavaScript Object Notation) format
Example of an API request and response
4. Using APIs in Python#
Installing and importing
requestsmoduleSending a GET request
Parsing JSON response
Handling errors and exceptions
5. Example Projects#
Fetching data from a public API (e.g., OpenWeatherMap, Cat Facts API, etc.)
Displaying API data in the terminal
Part 2: Building GUIs with Tkinter#
6. Introduction to Tkinter#
What is Tkinter?
Setting up the environment
Basic structure of a Tkinter program
Creating the main window (
Tk())
7. Widgets in Tkinter#
Labels, Buttons, Entry, Text, Frame, Canvas, Listbox
Widget options (text, font, color, etc.)
Geometry managers (
pack,grid,place)
8. Handling Events#
Button click events (
commandparameter)Getting user input from Entry
Updating label text dynamically
9. Layout and Styling#
Frames and organizing the window
Using padding, alignment, and resizing
Introduction to
ttkfor modern-looking widgets
10. Integrating APIs with Tkinter GUI#
Combining Tkinter with an API call
Displaying live data (like weather info) in a GUI
Handling user input (e.g., user enters a city name → app fetches data via API)
11. Example Mini Projects#
Weather App using OpenWeatherMap API
Random Cat Facts App
Currency Converter App
Movie Search App using OMDb API
12. Error Handling and Debugging#
Handling invalid user inputs
Managing failed API requests (timeouts, invalid keys, etc.)
Showing error messages in GUI ( messagebox )
13. Packaging and Running#
Running your GUI application
Converting your Python GUI to an executable (.exe) using
pyinstaller
Lesson : APIs and Tkinter Set Started
Introduction
In modern programming, applications rarely work in isolation. They often need to interact with other programs, services, or users. Two important concepts that help us build powerful and interactive applications are APIs and Graphical User Interfaces (GUIs).
APIs allow your program to communicate with other software, services, or data providers over the internet.
Tkinter lets you create graphical interfaces, so your program is easier to use and more interactive.
1. Introduction to APIs#
What is an API?#
API (Application Programming Interface) is a set of rules that allows one piece of software to interact with another.
Think of it as a bridge between two systems that lets them share data and functionality.
Real-Life Analogy#
Imagine a restaurant:
You (the user) → make a request.
Waiter (the API) → takes your order to the kitchen.
Kitchen (the server) → prepares the food (data) and gives it back to you via the waiter.
Similarly, in programming:
The client sends a request.
The server processes it and sends a response.
Request
Client Response
API
Server
Why APIs Are Important#
They enable data sharing between apps (e.g., Google Maps integrated in ride-sharing apps).
Help developers build feature-rich apps faster without reinventing the wheel.
Provide access to third-party services like weather info, social media, news, etc.
Common Examples#
Weather Apps – use APIs like OpenWeatherMap
Currency Converters – use ExchangeRate API
1.1 Types of APIs#
1. Web APIs#
The most common type — used to interact with web servers via the Internet.
Use HTTP methods (GET, POST, PUT, DELETE).
Examples:
2. Library or Local APIs#
Built into programming languages or libraries.
Example: Python’s
mathmodule (math.sqrt()) is an API that lets you use mathematical functions.
3. Public vs. Private APIs#
Type |
Description |
Example |
|---|---|---|
Public API |
Open to everyone, usually free or with a key |
OpenWeatherMap |
Private API |
Restricted for internal use within an organization |
Google’s internal APIs |
Partner API |
Shared between companies under agreements |
PayPal merchant APIs |
1.3. How Web APIs Work#
Client-Server Model#
Client (your app) sends a request.
Server processes it.
Server sends back a response (usually in JSON).
API Request and Response Example#
Request URL:
Response (JSON):
HTTP Methods#
Method |
Purpose |
Example |
|---|---|---|
GET |
Retrieve data |
Get weather data |
POST |
Send new data |
Submit a form |
PUT |
Update existing data |
Edit a profile |
DELETE |
Remove data |
Delete a note |
Endpoints#
Each URL path is an endpoint (specific resource on the server).
Example:
https://api.github.com/users/octocat/repos → endpoint for user repositories.
Using APIs in Python#
To use APIs, we often use the requests library.
Step 1: Install and Import#
pip install requests
import requests
Step 2: Send a GET Request#
response = requests.get("https://api.github.com/users/octocat")
Step 3: Check Status#
print(response.status_code) # 200 means success
Step 4: Parse JSON Data#
data = response.json()
print(data["name"])
Output Example:
The Octocat
Step 5: Handle Errors#
if response.status_code != 200:
print("Error:", response.status_code)
else:
print(data["name"])
5. Example Projects (for Practice)#
Example 1: Simple Weather Fetcher#
Example 2: Random Cat Fact#
Example 3: Currency Converter#
TIPS#
APIs let your app communicate with other systems.
Use the
requestsmodule for HTTP operations.JSON is the most common data format for APIs.
Always handle errors and check
status_code.Practice with public APIs before working with private ones.
Example: Getting Weather Data#
Imagine you want to build a program that shows the current temperature in a city. Instead of collecting and updating weather data yourself, you can use a weather API like OpenWeatherMap.
Step-by-step: Using OpenWeatherMap API in Python#
Sign up at OpenWeatherMap https://openweathermap.org/
Understand the API endpoint (URL) you need to call: https://api.openweathermap.org/data/2.5/weather?q=city_name&appid=API_key&units=metric
Write Python code to send a request and process the response.
What are we doing in the code#
We used the requests.get() method to send an HTTP GET request to the API.
We passed parameters (city, api_key, units) to the API.
We checked if the request was successful (status_code == 200).
We converted the JSON response into a Python dictionary using.json().
We extracted and printed useful information: temperature and weather description.
Part A: Multiple-Choice Questions (MCQs)#
Choose the correct answer
What does API stand for?
Application Programming Interface
Advanced Program Integration
Automated Process Input
Applied Programming Interaction
Which of the following is NOT a common HTTP method used in APIs?
GET
POST
FETCH
DELETE
What is the purpose of an API key?
To encrypt JSON data
To identify and authenticate the user or app making a request
To make the response faster
To store client data
Which of the following Python libraries is commonly used to work with APIs?
os
math
requests
pandas
What is the most common data format used in API responses?
XML
HTML
JSON
CSV
In the URL [https://api.github.com/users/octocat, what is the endpoint?]
What does the status code ``404`` represent?
Success
Server error
Not found
Bad request
Which function from the ``requests`` library retrieves the JSON data?
response.text()
response.html()
response.json()
response.data()
If ``response.status_code == 200``, what does that mean?
Request failed
Request succeeded
Server busy
Unauthorized access
10 What will happen if you call an API without an internet connection?
Returns empty JSON
Raises a requests.exceptions.ConnectionError
Returns 404
Prints nothing
☑ Answer Key (for instructors/self-check):
1-a, 2-c, 3-b, 4-c, 5-c, 6-b, 7-c, 8-c, 9-b, 10-b
Short Answer Questions#
Write short answers (2–3 sentences each):
Explain what an API is and give one real-world example.
Differentiate between public and private APIs.
What is JSON, and why is it preferred in APIs?
What does the
requests.get()function do in Python?What is the role of endpoints in an API?
What happens when an invalid city name is sent to a weather API?
How can you handle API errors in Python?
What is the difference between
GETandPOSTrequests?
Part C: Coding Exercises#
Exercise 1: Get a Joke from an API#
Task:
Write a Python program that fetches and prints a random joke using this API:
url = "https://official-joke-api.appspot.com/random_joke"
Hint:
The JSON response looks like:
{
"setup": "Why did the chicken cross the road?",
"punchline": "To get to the other side!"
}
Expected Output:
Why did the chicken cross the road?
To get to the other side!
Exercise 2: Cat Facts API#
Task:
Use the API [https://catfact.ninja/fact] to display a random cat fact.
If the request fails, print [“Error fetching data.”]
Exercise 3: Weather Data (Mini Project)#
Task:
Ask the user for a city name and display the temperature and condition using:
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY&units=metric"
(This API doesn’t require a key)
Example Output:
Enter city name: London
Temperature: 15°C, Condition: cloudy
Exercise 4: Explore JSON Keys#
Task:
Fetch data from
url = "https://api.github.com/users/octocat"
and print only the following:
login
public_repos
followers
Exercise 5: Error Handling Practice#
Task:
Write a Python script that requests data from an invalid API endpoint.
Use a [try-except] block to handle the RequestException and print [“Network error or invalid URL.”]
Introduction to GUI (Graphical User Interface)#
A Graphical User Interface (GUI) is a type of user interface that allows people to interact with a computer using visual elements such as windows, buttons, text boxes, icons, sliders, dropdowns, and images.
Unlike command-line programs where users must type instructions, a GUI lets users perform actions using the mouse, keyboard, or touch, making applications more user-friendly, intuitive, and accessible to beginners.
Why Do We Use GUIs?#
They are easier to use than typing commands.
They provide visual feedback, so users immediately see results.
They make software look modern and professional.
They allow users to interact without knowing programming commands.
They minimize user mistakes through menus, buttons, and prompts.
Real-World Examples of GUIs#
You interact with GUIs every day: - Windows and macOS desktops - Mobile apps - Calculator apps - File Explorer / Finder - Web browsers like Chrome and Firefox - Video players, drawing tools, games, dashboards, etc.
Every app with buttons, colors, menus, or pop-ups is a GUI application.
6. What is Tkinter?#
Tkinter is Python’s built-in library for creating Graphical User Interfaces (GUIs).
It allows you to build desktop applications with buttons, text boxes, menus, and more.
It comes pre-installed with Python, so no extra installation is needed.
How Tkinter Works#
Tkinter acts as a bridge between Python and the Tcl/Tk GUI toolkit.
The main window of the application is created using the
Tk()object.
Structure of a Simple Tkinter Program#
import tkinter as tk
root = tk.Tk()
root.title("My First GUI")
label = tk.Label(root, text="Hello, Tkinter!")
label.pack()
root.mainloop()
Explanation 1. tk.Tk() → Creates the main window. 2. Label →
A widget that displays text. 3. pack() → Places the widget in the
window. 4. mainloop() → Keeps the window open and listens for user
actions.
7. Tkinter Widgets#
Widgets are the building blocks of a GUI.
Commonly Used Widgets#
Widget |
Purpose |
|---|---|
Label |
Display text or images |
Button |
Perform an action when clicked |
Entry |
Accept single-line text input |
Text |
Multi-line text input |
Frame |
Container for grouping widgets |
Listbox |
Display a list of items |
Canvas |
Draw shapes or graphics |
Checkbutton |
Create checkboxes |
8. Handling Events and User Input#
Events are user interactions (like button clicks or typing text).
Getting Input from Entry#
import tkinter as tk
def show_text():
user_input = entry.get()
label.config(text=f"Hello, {user_input}!")
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
btn = tk.Button(root, text="Submit", command=show_text)
btn.pack()
label = tk.Label(root, text="")
label.pack()
root.mainloop()
Explanation - entry.get() fetches text from the Entry widget. - label.config(text=“…”) updates the text dynamically.
9. Layout and Styling#
Tkinter offers geometry managers to position widgets.
Geometry Managers#
Manager |
Description |
|---|---|
pack() |
Simplest, stacks widgets vertically/horizontally |
grid() |
Arranges widgets in a table-like structure |
place() |
Positions widgets using exact coordinates |
12. Error Handling and Debugging in Tkinter#
Handling Invalid Inputs#
Use messagebox for displaying error messages.
from tkinter import messagebox
messagebox.showerror("Error", "Invalid input!")
Common Issues#
Problem |
Cause |
Solution |
|---|---|---|
13. Packaging and Running#
Steps#
Install PyInstaller:
pip install pyinstaller
Build executable:
pyinstaller --onefile your_script.py
Find the
.exefile in thedistfolder.
14. Mini GUI + API Projects (Practice Ideas)#
= 1 2 3 4 5 =
Example Tkinter Projects#
1. Simple Calculator#
Basic math operations using buttons and entry widget.
Key Concepts:
Button click events (bind)
eval()for expression evaluationDynamic Entry widget updates
2. Weather App (Using OpenWeatherMap API)#
Fetch weather data from an API and show it in GUI.
Key Concepts:
Integrating an API with Tkinter
Handling network requests ( requests )
Updating GUI elements dynamically
3. Random Cat Facts App#
Unit 2 : Multiple-Choice Questions (MCQs)#
Choose the correct option
What is Tkinter in Python?
A data analysis library
A GUI library for building desktop applications
A web framework
A library for working with databases
Which command creates the main application window in Tkinter?
window = Tk()
root = Tk()
main = tk()
main_window()
Which function keeps the Tkinter window open and responsive?
root.show()
root.display()
root.mainloop()
root.loop()
Which of the following is NOT a geometry manager in Tkinter?
pack()
grid()
place()
align()
What does the ``command`` attribute in a Button widget do?
To define the function to execute when clicked
To change window size
Which method retrieves the text from an Entry widget?
.get()
.text()
.read()
.fetch()
What is the role of the Label widget?
Accepts user input
Displays text or images
Displays menus
Creates buttons
Which module provides themed (modern) widgets in Tkinter?
ttk
themed
modern_ui
gui_theme
What does ``pack()`` do in Tkinter?
Defines exact position of widgets
Organizes widgets into a window by stacking them
Removes widgets from a window
Closes the GUI
Answer Key: 1-b, 2-b, 3-c, 4-d, 5-a, 6-a, 7-b, 8-a, 9-b
Unit 2- Short Answer Questions#
Answer in 2–3 sentences.
What is the purpose of the
mainloop()function in Tkinter?Explain the difference between the
pack()andgrid()geometry managers.What does the
commandattribute in a Button do?How can you change the title of the Tkinter window?
What is the difference between
LabelandEntrywidgets?How can you organize multiple widgets inside a window neatly?
What is
ttkin Tkinter, and why is it used?How do you display a message box warning in Tkinter?
How can you make the window size fixed so users can’t resize it?
How do you close a Tkinter window programmatically?
Suggested Answers (for self-check):#
Keeps the window open and listens for user events.
pack()stacks widgets vertically/horizontally;grid()places them in rows and columns.Executes a function when the button is clicked.
Using
root.title("Window Title").Labelshows text/images;Entryaccepts one-line user input.Use frames, grid layout, or padding (
padx,pady).ttkgives modern, themed widgets.Using from
tkinter import messagebox→messagebox.showwarning("Title", "Message").root.resizable(False, False)Using
root.destroy()
Part C: Coding Exercises#
Exercise 1: Hello GUI#
Task:
Create a simple Tkinter window that displays the text:
Welcome to Tkinter!
and a button that closes the application when clicked.
Hint:
Use a [Label] for the text and [Button] with [root.destroy()].
Exercise 2: Greeting App#
Task:
Create a window with:
An Entry box for the user’s name
A Button that says “Greet Me”
A Label that displays [Hello, ] after the button is clicked
Expected Output:
Exercise 3: Simple Login Window#
Task:
Build a login form with:
Username and Password labels and entry boxes
A “Login” button that prints “Login Successful” in the console when clicked
Extra:
Use show="*" for password masking.
Exercise 4: Counter App#
Task:
Create a GUI counter with:
A label showing the number
“+” and “-” buttons to increase/decrease the count
Hint:
Use a variable and label.config(text=variable) to update the
display.
Exercise 5: Color Changer#
Task:
Make a window with three buttons: “Red”, “Green”, and “Blue”.
Clicking each button should change the background color of the window.
Hint:
Use root.config(bg="color").
Exercise 6: Simple Calculator#
Task:
Create a GUI calculator that can add two numbers:
Two Entry fields for inputs
A “Calculate” button
A Label to show the result
Expected Output: