0

I want the image to update in the system tray each time a new price is fetched

function 'reloadapi()' is threaded to fetch the price of the crypto again and again. But the pystray function 'run()/run_detached()' has a thread inside it that doesn't allow the image to be updated each time the price gets fetched.

Is there a way to call the num_image() function while the execution is happening to give a new price as input take the image and update the pystray icon reptedly


import requests
import threading
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import pystray

def num_img(I):
    #converts any text input into image
    img = Image.new('RGBA', (100,50), (0,0,0,0))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("OpenSans-Regular.ttf", 40)
    draw.text((15, -3),str(i),(255,255,255),font=font)
    return img

def reloadapi():
    
    key = "https://api.binance.com/api/v3/ticker/price?symbol=ADABUSD"
    data = requests.get(key).json()['price']
    print("ADA :",data)
    icon.icon = num_img(data)
    icon.run_detached()
    threading.Timer(1, reloadapi).start()

icon = pystray.Icon('test name', icon=num_img(0))
print("hi")
reloadapi()

Aadith
  • 1
  • 3

0 Answers0