Exception protection if something happens in the drawing of a figure. Added to code to ensure previously packed canvas is not packed again.
This commit is contained in:
parent
8ef45d6643
commit
244cff57a4
|
@ -824,15 +824,26 @@ def AxesGrid():
|
||||||
|
|
||||||
# The magic function that makes it possible.... glues together tkinter and pyplot using Canvas Widget
|
# The magic function that makes it possible.... glues together tkinter and pyplot using Canvas Widget
|
||||||
def draw_figure(canvas, figure):
|
def draw_figure(canvas, figure):
|
||||||
|
if not hasattr(draw_figure, 'canvas_packed'):
|
||||||
|
draw_figure.canvas_packed = {}
|
||||||
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
||||||
figure_canvas_agg.draw()
|
figure_canvas_agg.draw()
|
||||||
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
widget = figure_canvas_agg.get_tk_widget()
|
||||||
|
if widget not in draw_figure.canvas_packed:
|
||||||
|
draw_figure.canvas_packed[widget] = figure
|
||||||
|
widget.pack(side='top', fill='both', expand=1)
|
||||||
return figure_canvas_agg
|
return figure_canvas_agg
|
||||||
|
|
||||||
|
|
||||||
def delete_figure_agg(figure_agg):
|
def delete_figure_agg(figure_agg):
|
||||||
figure_agg.get_tk_widget().forget()
|
figure_agg.get_tk_widget().forget()
|
||||||
|
try:
|
||||||
|
draw_figure.canvas_packed.pop(figure_agg.get_tk_widget())
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Error removing {figure_agg} from list', e)
|
||||||
plt.close('all')
|
plt.close('all')
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------- GUI Starts Here -------------------------------#
|
# -------------------------------- GUI Starts Here -------------------------------#
|
||||||
# fig = your figure you want to display. Assumption is that 'fig' holds the #
|
# fig = your figure you want to display. Assumption is that 'fig' holds the #
|
||||||
# information to display. #
|
# information to display. #
|
||||||
|
@ -873,6 +884,9 @@ while True:
|
||||||
choice = values['-LISTBOX-'][0] # get first listbox item chosen (returned as a list)
|
choice = values['-LISTBOX-'][0] # get first listbox item chosen (returned as a list)
|
||||||
func = fig_dict[choice] # get function to call from the dictionary
|
func = fig_dict[choice] # get function to call from the dictionary
|
||||||
window['-MULTILINE-'].update(inspect.getsource(func)) # show source code to function in multiline
|
window['-MULTILINE-'].update(inspect.getsource(func)) # show source code to function in multiline
|
||||||
fig = func() # call function to get the figure
|
try:
|
||||||
figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure
|
fig = func() # call function to get the figure
|
||||||
|
figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure
|
||||||
|
except Exception as e:
|
||||||
|
print('Exception in fucntion', e)
|
||||||
window.close()
|
window.close()
|
|
@ -837,14 +837,23 @@ def AxesGrid():
|
||||||
|
|
||||||
|
|
||||||
def draw_figure(canvas, figure):
|
def draw_figure(canvas, figure):
|
||||||
|
if not hasattr(draw_figure, 'canvas_packed'):
|
||||||
|
draw_figure.canvas_packed = {}
|
||||||
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
|
||||||
figure_canvas_agg.draw()
|
figure_canvas_agg.draw()
|
||||||
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
|
widget = figure_canvas_agg.get_tk_widget()
|
||||||
|
if widget not in draw_figure.canvas_packed:
|
||||||
|
draw_figure.canvas_packed[widget] = figure
|
||||||
|
widget.pack(side='top', fill='both', expand=1)
|
||||||
return figure_canvas_agg
|
return figure_canvas_agg
|
||||||
|
|
||||||
|
|
||||||
def delete_figure_agg(figure_agg):
|
def delete_figure_agg(figure_agg):
|
||||||
figure_agg.get_tk_widget().forget()
|
figure_agg.get_tk_widget().forget()
|
||||||
|
try:
|
||||||
|
draw_figure.canvas_packed.pop(figure_agg.get_tk_widget())
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Error removing {figure_agg} from list', e)
|
||||||
plt.close('all')
|
plt.close('all')
|
||||||
|
|
||||||
|
|
||||||
|
@ -881,13 +890,11 @@ layout = [[sg.Text('Matplotlib Plot Test', font=('ANY 18'))],
|
||||||
[sg.Col(col_listbox), col_instructions], ]
|
[sg.Col(col_listbox), col_instructions], ]
|
||||||
|
|
||||||
# create the form and show it without the plot
|
# create the form and show it without the plot
|
||||||
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI',
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, resizable=True, finalize=True)
|
||||||
layout, resizable=True, finalize=True)
|
|
||||||
|
|
||||||
canvas_elem = window['-CANVAS-']
|
canvas_elem = window['-CANVAS-']
|
||||||
multiline_elem = window['-MULTILINE-']
|
multiline_elem = window['-MULTILINE-']
|
||||||
figure_agg = None
|
figure_agg = None
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
event, values = window.read()
|
event, values = window.read()
|
||||||
if event in (sg.WIN_CLOSED, 'Exit'):
|
if event in (sg.WIN_CLOSED, 'Exit'):
|
||||||
|
@ -902,6 +909,8 @@ while True:
|
||||||
func = fig_dict[choice]
|
func = fig_dict[choice]
|
||||||
# show source code to function in multiline
|
# show source code to function in multiline
|
||||||
window['-MULTILINE-'].update(inspect.getsource(func))
|
window['-MULTILINE-'].update(inspect.getsource(func))
|
||||||
fig = func() # call function to get the figure
|
try:
|
||||||
figure_agg = draw_figure(
|
fig = func() # call function to get the figure
|
||||||
window['-CANVAS-'].TKCanvas, fig) # draw the figure
|
figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure
|
||||||
|
except Exception as e:
|
||||||
|
print('Error in plotting', e)
|
||||||
|
|
Loading…
Reference in New Issue