【问题标题】:Plotly Chart at Tkinter Python在 Tkinter Python 绘制图表
【发布时间】:2016-11-20 10:39:09
【问题描述】:

是否可以在 Tkinter GUI 上显示 Plotly 图表?我一直试图让这种情况发生,但无济于事。

这是我的代码(Plotly 代码是从 Plotly 网站复制的):

from tkinter import *
import plotly.plotly as py
import plotly.graph_objs as go 

from datetime import datetime
import pandas.io.data as web


mGui = Tk()

mGui.geometry('651x700+51+51')
mGui.title('Plotly at Tkinter')

df = web.DataReader("AAPL", 'yahoo',
                    datetime(2007, 10, 1),
                    datetime(2016, 7, 11))

trace = go.Scatter(x=df.index,
                   y=df.High)


data = [trace]
layout = dict(
    title='Time series with range slider and selectors',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label='1m',
                     step='month',
                     stepmode='backward'),
                dict(count=6,
                     label='6m',
                     step='month',
                     stepmode='backward'),
                dict(count=1,
                    label='YTD',
                    step='year',
                    stepmode='todate'),
                dict(count=1,
                    label='1y',
                    step='year',
                    stepmode='backward'),
                dict(step='all')
            ])
        ),
        rangeslider=dict(),
        type='date'
    )
)

fig = dict(data=data, layout=layout)
py.iplot(fig)

mGui.mainloop()

提前谢谢你。

【问题讨论】:

  • 如果您已经找到使用 tkinter 使用 plotly kinda plots 的解决方案,请告诉我
  • 您找到解决方案了吗?我在网上找不到任何东西。
  • 我还没有找到解决方案。最终使用 matplotlib 作为替代方案。
  • 是的,我认为这两个库不兼容。需要使用其中一个。

标签: python tkinter plotly


【解决方案1】:

根据: https://plotly.com/python/renderers/ 可用的绘图渲染器是:

 ['plotly_mimetype', 'jupyterlab', 'nteract', 'vscode',
 'notebook', 'notebook_connected', 'kaggle', 'azure', 'colab',
 'cocalc', 'databricks', 'json', 'png', 'jpeg', 'jpg', 'svg',
 'pdf', 'browser', 'firefox', 'chrome', 'chromium', 'iframe',
 'iframe_connected', 'sphinx_gallery']

这意味着不可能让 tkinter 直接处理情节用户交互事件。因此,最接近您想要的是生成非交互式绘图渲染,例如 png/jpg 并将其显示在 tkinter 画布中。 至少画布将允许平移/扫描和缩放。 另一种选择是从 tkinter 应用程序打开网络浏览器,但这意味着用户不会直接与原始 tkinter 应用程序交互。

【讨论】:

    【解决方案2】:

    自从

    • plotly 可以在浏览器中查看,并且
    • this answer 说你可以在 Tkinter 中嵌入一个“成熟的铬浏览器”,

    我想在 Tktinter 的嵌入式浏览器中呈现交互式绘图图是可能的。

    如果我得到一个工作示例,我会在这里发布。

    【讨论】:

    • 你有例子吗?
    • 不,我们的项目从 Tkinter 移走了,抱歉。
    【解决方案3】:

    一个可能的事情是将你的情节转换成你可以很容易地使用图像到 Tkinter 的图像

    情节到图像 首先安装万花筒

    pip install -U kaleido
    

    比使用这个代码你不需要导入 kaleido

    fig.write_image("fig1.png")
    
    

    在 tkinter 中你可以使用任何标签来显示图像

      img = Image.open('image_name.png')
      tkimage = ImageTk.PhotoImage(img)
      lbl = Label(mGui, image = tkimage)
      lbl.place(x=0, y=0)
    
      # if not work than use commented line below
      # img = PhotoImage(file='logo.gif')
      # lbl = Label(mGui, image=img)
      # lbl.place(x=0, y=0)
    

    另一种方法是 转换为 localhost 网站并将其嵌入到 tkinter 中

    # for installing dash use command 'pip install dash'
    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    
        # open in website
        app = dash.Dash()
        app.layout = html.Div([
            dcc.Graph(figure=fig)
        ])
    
        app.run_server(debug=True, use_reloader=False)
    

    现在将它嵌入到 Tkinter 我不知道它是怎么可能的,但你可以谷歌它。

    编辑:你可以看到它Is it possible to render HTML in Tkinter?

    【讨论】:

      猜你喜欢
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 2020-06-21
      • 2022-07-05
      • 2021-04-09
      • 2020-07-10
      相关资源
      最近更新 更多