Skip to content

Instantly share code, notes, and snippets.

@horstjens
Last active August 1, 2024 07:52
Show Gist options
  • Save horstjens/59a0e976326b13baa6eb012f94f60b28 to your computer and use it in GitHub Desktop.
Save horstjens/59a0e976326b13baa6eb012f94f60b28 to your computer and use it in GitHub Desktop.
freesimplegui and matplotlib
#https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Matplotlib_Browser.py
# !/usr/bin/env python
import FreeSimpleGUI as sg
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import matplotlib.pyplot as plt
def PyplotSimple():
# matplotlib drawing function
# evenly sampled time .2 intervals
#t = np.arange(0., 5., 0.2) # go from 0 to 5 using .2 intervals
hydro_hp = [100, 90, 90,80,55,22,12,10,10,10, 7, 6, 5, 5,5,2,0]
igni_hp = [100,100,100,80,80,80,70,44,44,44,44,22,22,22,5,5,5]
# red dashes, blue squares and green triangles
#plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^')
plt.plot(list(range(1,len(hydro_hp)+1)), hydro_hp, 'r--')
plt.plot(list(range(1,len(igni_hp) +1)), igni_hp, 'g--')
# plt.plot(turn, igni_hp[turn], 'r--')
fig = plt.gcf() # get the figure to show
return fig
# 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()
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. #
# --------------------------------------------------------------------------------#
sg.theme('LightGreen')
figure_w, figure_h = 650, 650
layout = [[sg.Text('Matplotlib Plot Test', font=('current 18'))],
[sg.Canvas(size=(figure_w, figure_h), key='-CANVAS-')],
[sg.Button("OK"),sg.Button("Exit"),],
]
# create the form and show it without the plot
# make sure that FINALIZE = TRUE !!!
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout, grab_anywhere=False, finalize=True)
# create matplotlib diagram (only works after finalize is True)
fig = PyplotSimple() # function call
figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure
# The GUI Event Loop
while True:
event, values = window.read()
# print(event, values) # helps greatly when debugging
if event in (sg.WIN_CLOSED, 'Exit'): # if user closed window or clicked Exit button
break
#if figure_agg:
if event == "OK":
# delete the figure
delete_figure_agg(figure_agg)
# draw new figure:
#try:
# #fig = func() # call function to get the figure
# fig = PyplotSimple() # function call
# figure_agg = draw_figure(window['-CANVAS-'].TKCanvas, fig) # draw the figure
#except Exception as e:
# print('Exception in function', e)
window.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment