Ethereum: A request to APIs in python. And displaying in the tkinter window

Home > Ethereum: A request to APIs in python. And displaying in the tkinter window

Here is an article that provides a step -by -step guide on how to achieve its goal using Python and the Tkinter library.

Ethereum API call: live calculations with Tkinter

In this article, we will guide it through the configuration of a simple TKINTER application that makes calls API for Binance Exchange and show live calculations in real time.

Prerequisites

  • Install python 3.7 or higher

  • Install the ‘Applications’ library using PIP: PIP Install applications'

  • Install thetkinterlibrary using pip: pip install tk

Step by step instructions

1. Define its URL and API function

Create a new file calledAPI.py. This file will keep its API UR and work to make the call.

Python

Import requests

Ethereum: A request to APIs in python. And displaying in the tkinter window

Binance Exchange API URL

API_URL = "

Def get_binance_price (Ticker):

Make an API call

Answer = Requets.get (API_URL, Params = {"Symbol": Ticker})

if answer.status_code == 200:

Return response.json () ["Price"]

others:

print (f "Error: {Response.status_code}")

It does not return any

Example use

Ticker = "Btcusdt"

Price = Get_binance_Price (Ticker)

If the price is none:

PRINTING (F "The current price of Bitcoin is: $ {Price: .2f}")

2. Create the Tkinter

window

Open a new file calledMain.pyand add the following code to create the Tkinter window:

Python

import tkinter as tk

Initialize tkinter

root = tk.tk ()

Root.title ("Ethereum Api Call")

Create label to show live calculations

Label = tk.label (root, text = "current price:")

Label.pack ()

Create button to run the so -called API

DEF BUTTON_CLICK () ::

Ticker = Input ("Enter the Bitcoin symbol:")

Price = Get_binance_Price (Ticker)

If the price is none:

label

Delete the label after 3 seconds

Def Clear_Label () ::

Root.fter (3000, Clear_Label)

Clear_Label ()

Create button to run the so -called API

button = tk.button (root, text = "run", commander = button_click)

button.pack ()

Start tkinter event loop

Root.Mainloop ()

Explanation

In this article:

  • First we import the required libraries: 'Requests to make calls API and’ tkinterto create the Gui.

  • TheAPI.pydefines a function for calling API for Binance Exchange and returns the current price of a specified bitcoin symbol.

  • In theMain.pyfile, we create a TKINTER window with a label that shows live calculations and a button to run a call API. When clicking on the button, request your entrance (Bitcoin symbol) and shows the current price in real time using theGet_binance_price ()function.

  • We use theafter () method to program the execution of the` Clear_Label () function after 3 seconds. This deletes the label and updates the calculation screen.

Example use cases

You can execute this code on your local machine to see it in action. Simply enter a bitcoin symbol when asked and see the current live price update in the window.

Note: Be sure to have an active Internet connection for API calls to work properly.

This is a basic example of how to achieve live calculations with Tkinter using API. You can improve this code by adding more functionality or error management as necessary.

ETHEREUM MAINNET DOES

More Posts

Share:

Send Us A Message