Im trying to get the message box to update in real time, but it just displays the data when its first displayed and never updates after that
import pandas as pd
import time
from binance.client import Client
import tkinter as tk
import threading
import os
# Initialize the Binance client with your API keys
api_key = os.getenv('.....') # Set your API key in your environment variables
api_secret = os.getenv('....') # Set your API secret in your environment variables
client = Client(api_key, api_secret)
def fetch_doge_futures_data():
"""Fetch the last 500 DOGE futures trades from Binance."""
trades = client.futures_recent_trades(symbol='DOGEUSDT', limit=500)
data = {
'trade_time': [trade['time'] for trade in trades],
'price': [float(trade['price']) for trade in trades]
}
return pd.DataFrame(data)
def data_fetching_thread(root, trade_count_label, last_trade_label, last_trade_time_label):
"""Thread function to fetch data and update the GUI."""
all_trades_df = pd.DataFrame(columns=['trade_time', 'price'])
while True:
new_data_df = fetch_doge_futures_data()
all_trades_df = pd.concat([all_trades_df, new_data_df]).drop_duplicates(subset=['trade_time']).reset_index(drop=True)
all_trades_df.sort_values(by='trade_time', inplace=True)
if len(all_trades_df) > 1:
all_trades_df['return'] = (all_trades_df['price'].diff() / all_trades_df['price']).shift(-1)
# Filter the last trade of each second
all_trades_df['trade_time'] = pd.to_datetime(all_trades_df['trade_time'], unit='ms')
last_trade_per_second_df = all_trades_df.groupby(all_trades_df['trade_time'].dt.floor('S')).tail(1).reset_index(drop=True)
if not last_trade_per_second_df.empty:
last_trade = last_trade_per_second_df.iloc[-1]
trade_count = len(all_trades_df.index)
last_trade_price = last_trade['price']
last_trade_time = last_trade['trade_time']
# Update the GUI in the main thread
root.after(0, lambda: update_gui(trade_count_label, last_trade_label, last_trade_time_label, trade_count, last_trade_price, last_trade_time))
time.sleep(1) # Sleep for 1 second before fetching new data
def update_gui(trade_count_label, last_trade_label, last_trade_time_label, trade_count, last_trade_price, last_trade_time):
"""Update the GUI with the latest data."""
trade_count_label.config(text=f"Number of trades: {trade_count}")
last_trade_label.config(text=f"Last trade: {last_trade_price}")
last_trade_time_label.config(text=f"Last trade time: {last_trade_time}")
def main():
"""Set up the tkinter GUI and start the data fetching thread."""
root = tk.Tk()
root.title("DOGE Futures Data")
# Create labels to display the number of trades and the last trade
trade_count_label = tk.Label(root, text="Number of trades: 0", font=('Helvetica', 16))
trade_count_label.pack(pady=10)
last_trade_label = tk.Label(root, text="Last trade: None", font=('Helvetica', 16))
last_trade_label.pack(pady=10)
last_trade_time_label = tk.Label(root, text="Last trade time: None", font=('Helvetica', 16))
last_trade_time_label.pack(pady=10)
# Start the data fetching thread
threading.Thread(target=data_fetching_thread, args=(root, trade_count_label, last_trade_label, last_trade_time_label), daemon=True).start()
# Run the tkinter main loop
root.mainloop()
if __name__ == "__main__":
main()