Tkinter Real-Time Data Tracker with Threading

In this project we combine Python Tkinter with threading to repeatedly fetch external data without freezing the desktop interface.

Our example retrieves the latest available intraday stock price using yfinance. A background worker thread performs the network request, while Tkinter remains responsive.

The worker sends its result through a Python Queue. The Tkinter main thread then reads the Queue and updates the displayed price.

Stock data note: The value returned by yfinance should be treated as the latest available intraday value. It is not guaranteed to be exchange-level real-time data. Availability and delay can depend on the market and upstream data source.
External data
      |
      v
Worker thread
      |
      v
Queue
      |
      v
Tkinter main thread
      |
      +--> Price
      +--> Time
      +--> Status

Why Use a Background Thread? Top ↑

Tkinter uses an event loop to process Button clicks, keyboard input, window drawing and other GUI events.

If a network request runs directly on the Tkinter main thread, the window can stop responding while Python waits for the request to finish.

A worker thread allows the network operation to run separately:

Tkinter main thread
      |
      +--> GUI remains responsive

Worker thread
      |
      +--> Fetch external data

However, Tkinter widgets should be updated from the Tkinter main thread. Therefore we should not directly update a StringVar from the worker thread.

Install yfinance Top ↑

Install the yfinance package before running the project.

pip install yfinance

Matplotlib is not required for this page. We will use it later in the real-time stock graph project.

Tkinter displaying the latest available Apple stock price

Fetch the Latest Available Stock Price Top ↑

The data-fetching function receives the stock symbol and returns normal Python values. It does not update any Tkinter widget.

def fetch_stock_price(symbol):
    stock=yf.Ticker(symbol)

    data=stock.history(
        period='1d',
        interval='1m'
    )

    if data.empty:
        raise ValueError(
            'No intraday data was returned.'
        )

    close_values=data['Close'].dropna()

    if close_values.empty:
        raise ValueError(
            'No price data is available.'
        )

    price=float(
        close_values.iloc[-1]
    )

    timestamp=close_values.index[-1]

    return price,timestamp

iloc[-1] returns the most recent value in the Close series.

Using Queue with Tkinter Top ↑

A Python Queue provides a simple way to transfer the result from the worker thread back to the main Tkinter thread.

result_queue=queue.Queue()

The worker places either a successful result or an error in the Queue.

def fetch_worker(symbol):

    try:
        price,timestamp=fetch_stock_price(
            symbol
        )

        result_queue.put(
            (
                'ok',
                symbol,
                price,
                timestamp
            )
        )

    except Exception as e:

        result_queue.put(
            (
                'error',
                symbol,
                str(e),
                None
            )
        )

The worker does not call:

price_var.set()
status_var.set()
Label.config()

Those GUI operations are performed by the main thread.

Refresh the Stock Data Top ↑

The example requests one-minute intraday data, so we use a 60-second refresh interval:

REFRESH_MS=60000

After receiving a result, Tkinter schedules the next request with after().

if tracking:

    schedule_id=root.after(
        REFRESH_MS,
        start_fetch
    )

This is preferable to continuously running a while True loop with time.sleep().

Complete Tkinter Stock Price Tracker Top ↑

The complete example allows the user to enter a ticker symbol, start tracking, stop tracking and view the timestamp of the latest returned data.

How the Program Works Top ↑

1. Enter the Stock Symbol

A Tkinter Entry widget lets the user enter a ticker such as:

AAPL
MSFT
GOOG

The program converts the input to uppercase before requesting the data.

2. Start the Worker Thread

The Start Tracking Button calls:

start_tracking()

This launches a daemon thread that executes:

fetch_worker(symbol)

3. Fetch the Data

The worker creates a yfinance Ticker object and requests one-minute intraday history.

4. Send the Result to Queue

The result is not applied directly to the GUI. Instead it is placed in result_queue.

5. Tkinter Checks the Queue

The main thread runs:

root.after(
    200,
    process_queue
)

This checks for completed worker results without blocking the GUI.

6. Schedule the Next Request

When tracking remains active, another request is scheduled after 60 seconds.

Handling Empty Data and Errors Top ↑

A requested ticker may be invalid, the network may be unavailable, the upstream service may return an error, or no intraday data may be available.

Before reading the latest Close value, the program checks:

if data.empty:
    raise ValueError(
        'No intraday data was returned.'
    )

It also checks the Close series:

if close_values.empty:
    raise ValueError(
        'No price data is available.'
    )

The worker sends the error through the same Queue used for successful results.

Why the Original Five-Second Loop Was Changed Top ↑

The earlier example used:

while True:
    ...
    time.sleep(5)

and requested:

interval='1m'

This can request the same one-minute data many times before a new data point is available.

The revised example uses:

REFRESH_MS=60000

and lets Tkinter schedule each new request using after().

Next Steps for the Real-Time Data Project Top ↑

This application gives us a reusable foundation for more practical Tkinter projects.

Store the Price History

Save the stock symbol, price and timestamp to SQLite and display the saved records using Treeview.

View Stored Stock Data

Display a Real-Time Graph

Use Matplotlib to visualize the collected price history.

Real-Time Stock Monitoring with Graphs

Apply the Pattern to Mutual Fund Data

The same background-update architecture can be reused with other financial data sources.

AMFI Fund Explorer Tutorial Mutual Fund NAV Tracker using Tkinter GUI

Frequently Asked Questions Top ↑

Q1: Why does the stock request run in a separate thread?

A network request can take time. Running it in a worker thread keeps the Tkinter event loop responsive.

Q2: Why not update StringVar directly from the worker thread?

Tkinter GUI operations should stay on the Tkinter main thread. The worker passes normal Python data through a Queue and the main thread updates the StringVars and widgets.

Q3: Is the yfinance price guaranteed to be real-time?

No. The application displays the latest available intraday value returned by the data source. Availability and delays may vary.

Q4: Why does the example refresh every 60 seconds?

The example requests one-minute interval data. Requesting it every five seconds usually creates unnecessary repeated requests.

Q5: Can another stock be tracked instead of Apple?

Yes. Enter another supported ticker symbol in the Entry field before starting the tracker.

Q6: What happens if no data is returned?

The program detects an empty result and sends an error back to the Tkinter interface instead of trying to read a missing last row.

Q7: Can this architecture be used for other APIs?

Yes. The same worker-thread, Queue and root.after pattern can be used for periodic API calls, databases, sensors, files and other external data sources.


Stored Real-Time Data Real-Time Data Graph Threading in Python

More Projects using Tkinter


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer