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

Here is an article that offers step-by-step instructions to achieve your target with Python and the Tkinter library.

Ethereum api call: Live calculations with tkinter

In this article we will guide you by setting up a simple tkinter application with which API calls at Binance Exchange and live calculations are displayed in real time.

requirements

  • Install Python 3.7 or higher

  • Install the “Requests” library with PIP: PIP installation requirements"

  • Install thetkinterlibrary with PIP: PIP Install TK

Step-by-step instructions

1. Define your API URL and function

Create a new file calledapi.py. This file keeps your API URL and function to make the call.

Python

Import inquiries

Binance Exchange API URL

Api_url = "

DEF get_binance_price (ticker):

API call

Answer = Requests.Get (api_url, params = {"symbol": ticker})

if response.status_code == 200:

return response.json () ["price"]

different:

print (f "error: {response.status_code}"))

none back

Example use

Ticker = "btcusdt"

price = get_binance_price (ticker)

If the price is not:

Print (f "The current price for Bitcoin is: $ {price: .2f}")

2. Create the Tkinter window

Open a new file called main.py and add the following code to create the Tkinter window:

`Python

Import the tkinter as a TK

Initialize Tkinter

root = tk.tk ()

root.title ("Ethereum api call")

Create label to display live calculations

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

Label.pack ()

Create button to carry out the API call

Def button_click ():

Ticker = input ("Enter Bitcoin symbol:")

price = get_binance_price (ticker)

If the price is not:

Label.config (text = f "The current price of {ticker} is: $ {price: .2f}")

Delete the label after 3 seconds

Def Clear_Label ():

root.after (3000, clear_label)

Clear_label ()

Create button to carry out the API call

Button = tk. button (root, text = "execute", command = button_click)

button.pack ()

Start the Tkinter -Evisisschleife

root.mainloop ()

Explanation

In this article:

  • We first import the necessary libraries: “Inquiries” to make API calls and ‘tkinter’ to create the GUI.

  • The file “API.PY” defines a function with which API calls can be made to exchange Binance and returns the current price of a specific Bitcoin symbol.

  • In the “Main.py” file we create a tkinter window with a label with the live calculations and a button to carry out an API call. If you click on the button, you will be asked for your input (Bitcoin icon) and the current price is shown in real time in real time in real time.

  • We use the “After ()method to plan the execution of the functionClear_Label ()` after 3 seconds. This deletes the label and updates the calculation display.

Example application cases

You can run this code on your local computer to see it in action. Simply enter a Bitcoin symbol when you are asked and you will see the current price update live in the window.

Note: Make sure that you have an active internet connection so that API calls work properly.

This is a basic example of how live calculations can be achieved with APIs with a tkinter. You can improve this code by adding more functions or error treatments as required.

Bitcoin Error Input 03c3f3495867xxxx