简体   繁体   English

将 Matplotlib 图形和工具栏嵌入 PySimpleGUI 后进入平移模式

[英]Entering pan mode after embedding Matplotlib figure and toolbar into PySimpleGUI

Is there a way to programmatically enter Matplotlib's pan mode, given that the figure is embedded into PySimpleGUI?鉴于图形嵌入到 PySimpleGUI 中,有没有办法以编程方式进入 Matplotlib 的平移模式? just like this像这样

I'm basically looking for the equivalent of我基本上是在寻找相当于

fig.canvas.manager.toolbar.pan()

(which doesn't seem to work with PySimpleGUI, as fig.canvas.manager is None in that case). (这似乎不适用于 PySimpleGUI,因为fig.canvas.manager在这种情况下为None )。

Here is a simple code to embed Matplotlib into PySimpleGUI with the toolbar:下面是使用工具栏将 Matplotlib 嵌入 PySimpleGUI 的简单代码:

import matplotlib.pyplot as plt
import PySimpleGUI as sg
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk



"""----------------------------- matplotlib stuff -----------------------------"""
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
x_data = np.linspace(-42, 42, 300)
ax.plot(x_data, np.sin(x_data))



"""----------------------------- PySimpleGUI stuff -----------------------------"""
window_layout = [
    [sg.Canvas(key="-CANVAS_TOOLBAR-")],
    [sg.Canvas(key="-CANVAS_FIG-")]
]

window = sg.Window('Window', window_layout, resizable=True, finalize=True)



"""--------------------- embedding matplotlib into the GUI ---------------------"""
class Toolbar(NavigationToolbar2Tk):
    def __init__(self, *args, **kwargs):
        super(Toolbar, self).__init__(*args, **kwargs)

def draw_figure_w_toolbar(canvas_fig, canvas_toolbar, figure):
    figure_canvas_agg = FigureCanvasTkAgg(figure, canvas_fig)
    toolbar = Toolbar(figure_canvas_agg, canvas_toolbar)
    figure_canvas_agg.draw()
    toolbar.update()
    figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=True)
    return figure_canvas_agg

fig_agg = draw_figure_w_toolbar(window['-CANVAS_FIG-'].TKCanvas, window['-CANVAS_TOOLBAR-'].TKCanvas, fig)
# print(type(fig_agg))
# print(type(fig.canvas))



"""-------------------------- reading the GUI window --------------------------"""
while True:
    event, values = window.read()
    print("event", event, "\nvalues", values)
    if event in (sg.WIN_CLOSED, "-ESCAPE-"):
        break

window.close()

I've looked at this question , but I wish to use Matplotlib's toolbar.我看过这个问题,但我想使用 Matplotlib 的工具栏。

Even this doesn't work because plt.get_current_fig_manager() returns None in my case.即使这样也不起作用,因为在我的情况下plt.get_current_fig_manager()返回None

When printing type(fig_agg) from the code above, I get从上面的代码中打印type(fig_agg)时,我得到

<class 'matplotlib.backends.backend_tkagg.FigureCanvasTkAgg'>

which is also the type of fig.canvas .这也是fig.canvas的类型。

By the way I thought about a trick to do it.顺便说一句,我想到了一个技巧来做到这一点。 Pressing p on the keyboard normally activates pan mode, so pressing the button programmatically does the trick: But that would be .nethical cheating (:按键盘上的p通常会激活平移模式,因此以编程方式按下按钮可以达到目的:但这将是 .nethical 作弊(:

I found an answer to my question.我找到了问题的答案。 In case it helps someone, the solution is simply如果它对某人有帮助,解决方案很简单

fig.canvas.toolbar.pan()

ie we remove manager from the original call mentioned above.即我们从上面提到的原始呼叫中删除manager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM