当前位置: 首页 > 工具软件 > TQSDK > 使用案例 >

在TqSdk任务中驱动Gui消息循环

李鸿
2023-12-01
import asyncio
import sys
import PySimpleGUI as sg
from tqsdk import TqApi

loop = asyncio.get_event_loop()
api = TqApi(loop=loop)
quote_a = api.get_quote('SHFE.rb1910')
quote_b = api.get_quote('SHFE.rb2001')


async def gui_task():
    layout = [
        [sg.Text('rb1910'), sg.Text('999999', key='rb1910.last')],
        [sg.Text('rb2001'), sg.Text('999999', key='rb2001.last')],
        [sg.Text('spread'), sg.Text('999999', key='spread')],
    ]

    window = sg.Window('价差显示', layout)

    while True:
        event, values = window.Read(timeout=0)
        if event is None or event == 'Exit':
            sys.exit(0)

        window.Element('rb1910.last').Update(quote_a.last_price)
        window.Element('rb2001.last').Update(quote_b.last_price)
        window.Element('spread').Update(
            quote_a.last_price - quote_b.last_price)
        await asyncio.sleep(1)  # 注意,这里必须使用asyncio.sleep,不能用time.sleep

loop.create_task(gui_task())

while True:
    api.wait_update()

 类似资料: