我一直在使用一些程序,目前正在尝试使用 Matplotlib 将图形放入使用 PySimpleGui 制作的界面中。我有两个程序试图绘制同一张图,一个有效,另一个无效。我已经显示了下面的代码,但跳过了一些编码,例如导入和“位置”和“完成”,因此可以更轻松地阅读下面的代码,但我在两者中都跳过了相同的代码。
第一个程序在下面并在 pysimplegui 窗口中正确打开图形。 sg 是 pysimplegui.
fig=gantt(location,done) #this is defined in another program, and makes the figure
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
layout = [[sg.Text('Plot test', font='Any 18')],
[sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
[sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]]
window = sg.Window('name', layout, force_toplevel=True, finalize=True)
fig_photo = draw_figure(window['-CANVAS-'].TKCanvas,fig)
event, values = window.read()
window.close()
下一个程序不显示图形。它有一个带有按钮的主窗口,可以打开图形窗口,图形窗口有一个图形。但是,有一个错误。
fig=gantt(location,done)
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
def window_graph():
layout_graph=[
[sg.Text('graph')],
[sg.Button('close graph')],
[sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
]
return sg.Window('graph', layout_graph, margins=(100,50), modal=True)
fig_photo = draw_figure(window_graph['-CANVAS-'].TKCanvas, fig)
def open_window_graph():
graph_window=window_graph()
while True:
event, values = graph_window.read()
if event=="close graph" or event==sg.WIN_CLOSED:
graph_window.close()
break
graph_window.close()
layout_main=[
[sg.Text("first page")],
[sg.Button('open')],
[sg.Button('close')]
]
window_main=sg.Window('main',layout_main, margins=(500,400))
while True:
event,values=window_main.read()
if event=='open':
open_window_graph()
elif event=='close':
break
else:
break
我得到的错误是;
fig_photo = draw_figure(window_graph['-CANVAS-'].TKCanvas, fig)
TypeError: 'function' object is not subscriptable
如果我删除此行,则程序可以运行,但第二个窗口没有图形。它的大小合适,所以我假设 figure_w 和 figure_h 正在工作。