Open In App

How to get a new API response in a Tkinter textbox?

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.

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.




import requests
  
# Response Object
r = requests.get('https://api.quotable.io/random')

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:




import requests
  
r = requests.get('https://api.quotable.io/random')
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




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:




def get_quote():
    r = requests.get('https://api.quotable.io/random')
    data = r.json()
    quote = data['content']
    text_box.delete('1.0', END)
    text_box.insert(END, quote)   

Code Implementation




# import library
import requests
import tkinter as tk
from tkinter import END, Text
from tkinter.ttk import Button
  
# create a main window
root = tk.Tk()
root.title('Quoter')
  
# function that will get the data
# from the API
def get_quote():
    # API request
    r = requests.get('https://api.quotable.io/random')
    data = r.json()
    quote = data['content']
      
    # deletes all the text that is currently
    # in the TextBox
    text_box.delete('1.0', END)
      
    # inserts new data into the TextBox
    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:

 


Article Tags :