You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Python中lightweight charts的图表窗口设置标题?

为lightweight charts窗口添加标题的简便方法

问题背景

你使用以下Python代码生成lightweight charts窗口,但窗口无标题,需要添加标题以便通过pyautogui进行操作:

import pandas as pd
from lightweight_charts import Chart

SYMBOL = 'GOOGL'

def get_bar_data(symbol, timeframe):
    if symbol not in ('AAPL', 'GOOGL', 'TSLA'):
        print(f'No data for "{symbol}"')
        return pd.DataFrame()
    return pd.read_csv(f'parttime_larry/lightweight-chart/bar_data/{symbol}_{timeframe}.csv')

if __name__ == '__main__':
    chart = Chart(toolbox=True)
    chart.legend(True)   

    chart.topbar.textbox('symbol', SYMBOL)   
    df = get_bar_data(SYMBOL, '5min')
    chart.set(df)   

    chart.show(block=True)

解决方案

有两种简便方法可以为窗口设置标题:

1. 初始化Chart时直接指定标题

lightweight_charts的Chart类初始化支持title参数,直接传入标题字符串即可:

if __name__ == '__main__':
    # 初始化时设置窗口标题为"WINDOWS 1"
    chart = Chart(toolbox=True, title="WINDOWS 1")
    chart.legend(True)   

    chart.topbar.textbox('symbol', SYMBOL)   
    df = get_bar_data(SYMBOL, '5min')
    chart.set(df)   

    chart.show(block=True)

2. 动态修改窗口标题

如果需要在运行过程中修改标题,可以通过chart.win获取底层Qt窗口对象,调用setWindowTitle方法:

if __name__ == '__main__':
    chart = Chart(toolbox=True)
    # 动态设置窗口标题
    chart.win.setWindowTitle("WINDOWS 1")
    
    chart.legend(True)   
    chart.topbar.textbox('symbol', SYMBOL)   
    df = get_bar_data(SYMBOL, '5min')
    chart.set(df)   

    chart.show(block=True)

内容的提问来源于stack exchange,提问作者Siegfried Arndt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 00:35:10