From 244cff57a46990cb737480edd7047384559cbd4f Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 5 Aug 2022 13:48:11 -0400 Subject: [PATCH] Exception protection if something happens in the drawing of a figure. Added to code to ensure previously packed canvas is not packed again. --- DemoPrograms/Demo_Matplotlib_Browser.py | 20 +++++++++++++--- DemoPrograms/Demo_Matplotlib_Browser_Paned.py | 23 +++++++++++++------ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/DemoPrograms/Demo_Matplotlib_Browser.py b/DemoPrograms/Demo_Matplotlib_Browser.py index 85c51c48..064f57f4 100644 --- a/DemoPrograms/Demo_Matplotlib_Browser.py +++ b/DemoPrograms/Demo_Matplotlib_Browser.py @@ -824,15 +824,26 @@ def AxesGrid(): # The magic function that makes it possible.... glues together tkinter and pyplot using Canvas Widget 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.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 + def delete_figure_agg(figure_agg): 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') + # -------------------------------- GUI Starts Here -------------------------------# # fig = your figure you want to display. Assumption is that 'fig' holds the # # information to display. # @@ -873,6 +884,9 @@ while True: choice = values['-LISTBOX-'][0] # get first listbox item chosen (returned as a list) func = fig_dict[choice] # get function to call from the dictionary window['-MULTILINE-'].update(inspect.getsource(func)) # show source code to function in multiline - fig = func() # call function to get the figure - figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure + try: + 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() \ No newline at end of file diff --git a/DemoPrograms/Demo_Matplotlib_Browser_Paned.py b/DemoPrograms/Demo_Matplotlib_Browser_Paned.py index 6344e89d..66d3e611 100644 --- a/DemoPrograms/Demo_Matplotlib_Browser_Paned.py +++ b/DemoPrograms/Demo_Matplotlib_Browser_Paned.py @@ -837,14 +837,23 @@ def AxesGrid(): 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.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 def delete_figure_agg(figure_agg): 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') @@ -881,13 +890,11 @@ layout = [[sg.Text('Matplotlib Plot Test', font=('ANY 18'))], [sg.Col(col_listbox), col_instructions], ] # create the form and show it without the plot -window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', - layout, resizable=True, finalize=True) +window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, resizable=True, finalize=True) canvas_elem = window['-CANVAS-'] multiline_elem = window['-MULTILINE-'] figure_agg = None - while True: event, values = window.read() if event in (sg.WIN_CLOSED, 'Exit'): @@ -902,6 +909,8 @@ while True: func = fig_dict[choice] # show source code to function in multiline window['-MULTILINE-'].update(inspect.getsource(func)) - fig = func() # call function to get the figure - figure_agg = draw_figure( - window['-CANVAS-'].TKCanvas, fig) # draw the figure + try: + fig = func() # call function to get the figure + figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure + except Exception as e: + print('Error in plotting', e)