'How to start a thread to update network data in tkinter when window opens without using a button
The following code updates a label with the price of BTCUSDT downloaded from the Binance public API. The thread is started with a button, but I can't work out how to start it without the button, when the window opens.
from tkinter import *
import time
import requests
import threading
def get_price():
while True:
root_url = 'https://api.binance.com/api/v3/avgPrice'
symbol = 'BTCUSDT'
url = root_url + '?symbol=' + symbol
data = requests.get(url).json()
my_label.config(text=data['price'])
time.sleep(60)
root = Tk()
root.geometry('400x200')
my_label = Label(root, text='Hello There')
my_label.pack(pady=20)
my_button = Button(root, text='Start', command=lambda:threading.Thread(target=get_price).start())
my_button.pack(pady=20)
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
