Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
845 views
in Technique[技术] by (71.8m points)

tkinter - Displaying real time stock data in a scrolling table in Python

I am trying to make a standalone GUI application that displays real time price data from a stock price API. Due to the API, it is much easier to make this program Python based.

The stock data from a domain of stocks is streamed from an API and stored at a set interval (atm around 500ms).

My aim is to insert the price information for the stock, when that stock makes a new high or a new low. Additionally the rows will need to be coloured different colours based on the price (red/blue)

E.g:

Time     | Ticker | Price 
11:00:00 | ABCD   | 109.50
11:00:50 | WXYZ   | 123.30
11:01:00 | ABCD   | 110.01
11:01:50 | EFGH   | 50.38

As you can imagine, very quickly this table will fill up, and it will be running all day during trading hours, so will need to keep up with constant additions.

At the moment, I've been looking at using Tkinter, but I am inexperienced with GUI related programming, so I'm not sure if it is capable of achieving what I would like. I have made a start with dummy data, and a worst case scenario (adding every 100ms). It already seems fairly sluggish, so I'm not sure if this is the way to go, particularly with the scrollbar workaround required (Adding a scrollbar to a group of widgets in Tkinter) :

import tkinter as tk
import tkinter.ttk as ttk


class StockDisplay(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        self.main_frame = tk.Frame()
        self.main_frame.bind("<Configure>", lambda e: self.my_canvas.configure(scrollregion=self.my_canvas.bbox("all")))
        self.main_frame.pack(fill=tk.BOTH, expand=1)

        self.my_canvas = tk.Canvas(self.main_frame)
        self.my_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)

        self.my_scrollbar = ttk.Scrollbar(self.main_frame, orient=tk.VERTICAL, command=self.my_canvas.yview)
        self.my_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        self.my_canvas.configure(yscrollcommand=self.my_scrollbar.set)

        self.second_frame = tk.Frame(self.my_canvas)

        self.my_canvas.create_window((0, 0), window=self.second_frame, anchor="nw")

        self.height = 0
        self.init_table()
        self.add_row()

    def init_table(self):
        tk.Label(self.second_frame, text="Time",).grid(row=self.height, column=0, padx=10)
        tk.Label(self.second_frame, text="Ticker").grid(row=self.height, column=1, padx=10)
        tk.Label(self.second_frame, text="Price").grid(row=self.height, column=2, padx=10)

    def add_row(self):
        self.height += 1
        tk.Label(self.second_frame, text="Time").grid(row=self.height, column=0, padx=10)
        tk.Label(self.second_frame, text="Ticker " + str(self.height)).grid(row=self.height, column=1, padx=10)
        tk.Label(self.second_frame, text="Price").grid(row=self.height, column=2, padx=10)
        self.reset_scrollregion()
        self.after(100, self.add_row)

    def reset_scrollregion(self):
        self.my_canvas.configure(scrollregion=self.my_canvas.bbox("all"))


if __name__ == '__main__':
    root = tk.Tk()
    root.title('Tkicker')
    root.geometry("500x400")
    display = StockDisplay()
    root.mainloop()

My questions are:

1.) Is Tkinter going to be able to acheive my goal?

a.) If yes: Is there a good/better way to allow this updating table (i/e is the grid method the best way)?

b.) If no: Is there a better GUI application that I could use to achieve this?

Any advice is very much appreciated.

Thanks!

question from:https://stackoverflow.com/questions/65938754/displaying-real-time-stock-data-in-a-scrolling-table-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The only way to display hundreds of thousands of strings would be with the text widget. I'm not sure what the upper limit is, but I tested it with 200,000 lines and it seemed to work ok. You definitely can't create 200,000 label widgets and expect tkinter to perform in a usable way. The canvas also has limitations on the number of items it can manage.

You can configure tabstops in the text widget so that your data aligns in columns. You can use tags to add color to a line or parts of a line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...