import requests
def get_stock_data(symbol):
url = f"https://query1.finance.yahoo.com/v7/finance/quote?symbols={symbol}"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
def parse_stock_data(data):
if data:
quote = data['quoteResponse']['result'][0]
return {
'symbol': quote['symbol'],
'price': quote['regularMarketPrice'],
'change': quote['regularMarketChange'],
'percent_change': quote['regularMarketChangePercent']
}
else:
return None
import tkinter as tk
from tkinter import ttk
class StockApp:
def __init__(self, root):
self.root = root
self.root.title("Stock Price Tracker")
self.symbol_label = ttk.Label(root, text="Symbol: ")
self.symbol_label.grid(column=0, row=0, padx=10, pady=10)
self.symbol_entry = ttk.Entry(root)
self.symbol_entry.grid(column=1, row=0, padx=10, pady=10)
self.track_button = ttk.Button(root, text="Track", command=self.track_stock)
self.track_button.grid(column=2, row=0, padx=10, pady=10)
self.tree = ttk.Treeview(root, columns=('Symbol', 'Price', 'Change', 'Percent Change'), show='headings')
self.tree.heading('Symbol', text='Symbol')
self.tree.heading('Price', text='Price')
self.tree.heading('Change', text='Change')
self.tree.heading('Percent Change', text='Percent Change')
self.tree.grid(column=0, row=1, columnspan=3, padx=10, pady=10)
def track_stock(self):
symbol = self.symbol_entry.get()
data = get_stock_data(symbol)
stock_data = parse_stock_data(data)
if stock_data:
self.tree.insert('', 'end', values=(stock_data['symbol'], stock_data['price'], stock_data['change'], stock_data['percent_change']))
if __name__ == "__main__":
root = tk.Tk()
app = StockApp(root)
root.mainloop()