In this Python TKinter article we want to learn How to Integrate Yahoo Finance with Python TKinter, Yahoo Finance API is an excellent source of financial data that provides real time and historical market data for stocks, currencies, commodities and many more. It is used by traders, investors and researchers worldwide to access financial data programmatically. in this article we want to learn how to use Yahoo Finance API with Python TKinter. so Tkinter is popular Python GUI toolkit.
How to Integrate Yahoo Finance with Python TKinter
TKinter is already installed with Python, but we need to install Yahoo Finance API.
1 |
pip install yfinance |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import tkinter as tk import yfinance as yf root = tk.Tk() root.title("Stock Price - Geekscoders.com") label = tk.Label(root, font=("Arial", 20)) label.pack(pady=20) def get_stock_price(): stock = yf.Ticker("AAPL") stock_price = stock.info["regularMarketPrice"] label.config(text=f"Apple Inc. Stock Price: {stock_price}") root.after(5000, get_stock_price) get_stock_price() root.mainloop() |
This code creates Tkinter window with label to display the stock price. after that we have defined a function get_stock_price() that retrieves the stock price using yf.Ticker() method from yfinance library and updates the label with new price. this function is scheduled to run every 5 seconds using root.after() method. and lastly the function is called to start retrieving and displaying the stock price.
Note that in this example, we are using yf.Ticker() method to retrieve the stock price for Apple Inc. (AAPL). you can change this to any other stock symbol to retrieve prices for different stock.
Run the complete code and this will be the result
Learn More on Python GUI
- How to Create Print Dialog in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6