I have made a program that takes the price of bitcoins (by using beautifulsoup) and displays it to the user. However, I want the price to get updated every 30 seconds or so, so I used the "threading" module and used its Timer. No matter how many seconds I type into the timer parameter, the program calls itself 5 times a second no matter what the seconds parameter is. Here is the code:
from bs4 import BeautifulSoup
from tkinter import *
import requests
import threading
root = Tk()
def bitcoinPrice():
url = 'http://www.coindesk.com/price/'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
btcPrice = soup.find('div', attrs=
{'class' : 'bpi-value bpiUSD'}
)
btcIndexDown = soup.find('span', attrs=
{'class' : 'bpi-change changeUSD data-down'}
)
btcIndexUp = soup.find('span', attrs=
{'class' : 'bpi-change changeUSD data-up'}
)
if(btcIndexDown is None):
return btcPrice.text + "(" + btcIndexUp.text + ")"
else:
return btcPrice.text + "(" + btcIndexDown.text + ")"
def bitcoinLabel():
theLabel = Label(root, text = "-")
theLabel.config(font = 'bold')
theLabel.pack()
updateBtcPrice(theLabel)
def updateBtcPrice(theLabel):
if '-' in theLabel.cget("text"):
theLabel.config(fg = 'red')
else:
theLabel.config(fg = 'green')
theLabel.configure(text = bitcoinPrice())
root.update()
print("Hello")
threading.Timer(5.0, updateBtcPrice(theLabel)).start()
try:
bitcoinLabel()
except:
pass