How to get a new API response in a Tkinter textbox?
Last Updated :
25 Jul, 2022
In this article, we will look into how to make an API request and then insert it into a Tkinter textbox using Python, Before moving further we will cover some basic concepts related to our article that we will use frequently.
- Request: Requests are required to play with the web. If you want to use APIs, download entire Facebook pages, or do anything else nice, you’ll need to make a request to the URL. When it comes to REST APIs and Web scraping, requests are crucial.
- Tkinter: Tkinter is the most commonly used method. It’s a standard Python interface to the Python-supplied Tk GUI toolkit. The fastest and simplest approach to constructing GUI applications is with Python and Tkinter. Using Tkinter to create a GUI is a simple operation.
- API: APIs usually allow us to get information from a server which helps us in our projects. We make requests to APIs using different methods like example GET, POST, etc
Packages Required
To make a GUI we need to install Tkinter.
pip install tkinter
To make an API request, we usually use a python library called requests.
pip install requests
Stepwise Implementation
Step 1: Making an API request
To make a GET request to an API, we use the requests.get() method. For this tutorial, we will be using an Open-Source API called Quotable. It has a route ‘/random’ which gives us a random quote.
Step 2: Getting the data from the API Request
Usually, APIs return the data in a JSON format. But, our requests library makes it easier for us and converts it into a python dictionary object. We can get that data by the r.json() method:
Python3
import requests
data = r.json()
|
Output:
{‘_id’: ‘w4QwG7e8hNr7’, ‘tags’: [‘famous-quotes’], ‘content’: ‘The only Zen you find on the tops
of mountains is the Zen you bring up there.’, ‘author’: ‘Robert M. Pirsig’, ‘authorSlug’: ‘robert-m-pirsig’, ‘length’: 77, ‘dateAdded’: ‘2019-02-17’, ‘dateModified’: ‘2019-02-17’}
Step 4: In this dictionary, we only import data that are present in the ‘content’ key which we can further store in a variable
quote = data['content']
Step 5: Creating a basic Tkinter App
So, here we have a very basic Tkinter app with a Textbox and a button
Python3
import tkinter as tk
from tkinter import END, Text
from tkinter.ttk import Button
root = tk.Tk()
root.title( 'Quoter' )
text_box = Text(root, height = 10 , width = 50 )
get_button = Button(root, text = "Get Quote" , command = get_quote)
text_box.pack()
get_button.pack()
root.mainloop()
|
Step 6: Creating an API Request function
We will now make a function that will get the data from the API, and edit the Text Box with that data:
- The text_box.delete() deletes all the text that is currently in the TextBox widget from the specified Index. In this case, It’s from 1.0 to the end which means the whole TextBox will be cleared.
- The text_box.insert() inserts data into the TextBox from the specified index. As the index is END, which in our case will be the first index, It will insert the quote obtained from the API to the TextBox.
Python3
def get_quote():
data = r.json()
quote = data[ 'content' ]
text_box.delete( '1.0' , END)
text_box.insert(END, quote)
|
Code Implementation
Python3
import requests
import tkinter as tk
from tkinter import END, Text
from tkinter.ttk import Button
root = tk.Tk()
root.title( 'Quoter' )
def get_quote():
data = r.json()
quote = data[ 'content' ]
text_box.delete( '1.0' , END)
text_box.insert(END, quote)
text_box = Text(root, height = 10 , width = 50 )
get_button = Button(root, text = "Get Quote" , command = get_quote)
text_box.pack()
get_button.pack()
root.mainloop()
|
Output:
Clicking the button would generate a new quote:
Please Login to comment...