在Python的滚动表中显示实时库存数据

在Python的滚动表中显示实时库存数据

本文介绍了在Python的滚动表中显示实时库存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个独立的GUI应用程序,该应用程序显示来自股票价格API的实时价格数据.由于有了API,使该程序基于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.

来自某个股票域的股票数据从API进行流式传输,并以设定的时间间隔(大约500毫秒的atm)存储.

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)

例如:

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.

目前,我一直在使用Tkinter,但是我对GUI相关的编程经验不足,因此我不确定它是否能够实现我想要的功能.我从虚拟数据开始,在最坏的情况下(每100毫秒添加一次)开始.它似乎已经相当缓慢,因此我不确定这是否可行,特别是需要滚动条解决方法时():

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()

我的问题是:

1.)Tkinter能够实现我的目标吗?

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

a.)如果是:是否存在一种允许该更新表的好/更好的方法(即网格方法是最好的方法)?

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

b.)如果不是:是否可以使用更好的GUI应用程序来实现这一目标?

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

非常感谢任何建议.

谢谢!

推荐答案

显示成千上万个字符串的唯一方法是使用文本小部件.我不确定上限是多少,但是我用200,000行进行了测试,它似乎工作正常.您绝对不能创建200,000个标签小部件,并期望tkinter以可用的方式执行.画布在其可以管理的项目数量上也有限制.

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.

这篇关于在Python的滚动表中显示实时库存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:21