2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2018-09-28 18:57:37 +00:00
|
|
|
if sys.version_info[0] >= 3:
|
2018-09-27 20:24:09 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-28 18:57:37 +00:00
|
|
|
else:
|
|
|
|
import PySimpleGUI27 as sg
|
|
|
|
|
2018-08-27 03:23:21 +00:00
|
|
|
from random import randint
|
|
|
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
|
|
|
|
from matplotlib.figure import Figure
|
|
|
|
import matplotlib.backends.tkagg as tkagg
|
2018-08-27 20:10:32 +00:00
|
|
|
import tkinter as tk
|
2018-08-27 03:23:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
fig = Figure()
|
|
|
|
|
|
|
|
ax = fig.add_subplot(111)
|
|
|
|
ax.set_xlabel("X axis")
|
|
|
|
ax.set_ylabel("Y axis")
|
|
|
|
ax.grid()
|
|
|
|
|
|
|
|
# define the form layout
|
2018-09-06 20:20:37 +00:00
|
|
|
layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')],
|
2018-09-24 22:01:00 +00:00
|
|
|
[sg.Canvas(size=(640, 480), key='canvas')],
|
|
|
|
[sg.Slider(range=(0, 10000), size=(60, 10), orientation='h', key='slider')],
|
|
|
|
[sg.ReadButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]]
|
2018-08-27 03:23:21 +00:00
|
|
|
|
|
|
|
# create the form and show it without the plot
|
2018-09-24 22:01:00 +00:00
|
|
|
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI').Layout(layout).Finalize()
|
2018-08-27 03:23:21 +00:00
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
canvas_elem = window.FindElement('canvas')
|
|
|
|
slider_elem = window.FindElement('slider')
|
2018-08-27 03:23:21 +00:00
|
|
|
graph = FigureCanvasTkAgg(fig, master=canvas_elem.TKCanvas)
|
|
|
|
canvas = canvas_elem.TKCanvas
|
|
|
|
|
|
|
|
dpts = [randint(0, 10) for x in range(10000)]
|
|
|
|
for i in range(len(dpts)):
|
2018-09-24 22:01:00 +00:00
|
|
|
button, values = window.ReadNonBlocking()
|
2018-08-27 03:23:21 +00:00
|
|
|
if button is 'Exit' or values is None:
|
|
|
|
exit(69)
|
|
|
|
|
2018-08-27 20:10:32 +00:00
|
|
|
slider_elem.Update(i)
|
2018-08-27 03:23:21 +00:00
|
|
|
ax.cla()
|
|
|
|
ax.grid()
|
2018-08-27 20:10:32 +00:00
|
|
|
DATA_POINTS_PER_SCREEN = 40
|
|
|
|
ax.plot(range(DATA_POINTS_PER_SCREEN), dpts[i:i+DATA_POINTS_PER_SCREEN], color='purple')
|
2018-08-27 03:23:21 +00:00
|
|
|
graph.draw()
|
|
|
|
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
|
|
|
|
figure_w, figure_h = int(figure_w), int(figure_h)
|
2018-08-27 20:10:32 +00:00
|
|
|
photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
|
2018-08-27 03:23:21 +00:00
|
|
|
|
|
|
|
canvas.create_image(640/2, 480/2, image=photo)
|
|
|
|
|
|
|
|
figure_canvas_agg = FigureCanvasAgg(fig)
|
|
|
|
figure_canvas_agg.draw()
|
|
|
|
|
|
|
|
# Unfortunately, there's no accessor for the pointer to the native renderer
|
|
|
|
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
|
|
|
|
|
|
|
|
# time.sleep(.1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|