Demo Matplotlib, Canvas Element changes, new Frame Element, added pad to Text, Slider
Plus a few other tweaks & bug fixes
This commit is contained in:
parent
4062b2b41c
commit
0def4bf436
4 changed files with 111 additions and 25 deletions
73
Demo_Matplotlib.py
Normal file
73
Demo_Matplotlib.py
Normal file
|
@ -0,0 +1,73 @@
|
|||
import PySimpleGUI as g
|
||||
import matplotlib
|
||||
matplotlib.use('TkAgg')
|
||||
from numpy import arange, sin, pi
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
|
||||
from matplotlib.figure import Figure
|
||||
import matplotlib.backends.tkagg as tkagg
|
||||
import sys
|
||||
import tkinter as Tk
|
||||
|
||||
"""
|
||||
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
|
||||
|
||||
Basic steps are:
|
||||
* Create a Canvas Element
|
||||
* Layout form
|
||||
* Display form (NON BLOCKING)
|
||||
* Draw plots onto convas
|
||||
* Display form (BLOCKING)
|
||||
"""
|
||||
|
||||
|
||||
def draw_figure(canvas, figure, loc=(0, 0)):
|
||||
""" Draw a matplotlib figure onto a Tk canvas
|
||||
|
||||
loc: location of top-left corner of figure on canvas in pixels.
|
||||
|
||||
Inspired by matplotlib source: lib/matplotlib/backends/backend_tkagg.py
|
||||
"""
|
||||
figure_canvas_agg = FigureCanvasAgg(figure)
|
||||
figure_canvas_agg.draw()
|
||||
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
|
||||
figure_w, figure_h = int(figure_w), int(figure_h)
|
||||
photo = Tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
|
||||
|
||||
# Position: convert from top-left anchor to center anchor
|
||||
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
|
||||
|
||||
# Unfortunately, there's no accessor for the pointer to the native renderer
|
||||
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
|
||||
|
||||
# Return a handle which contains a reference to the photo object
|
||||
# which must be kept live or else the picture disappears
|
||||
return photo
|
||||
|
||||
f = Figure(figsize=(5, 4), dpi=100)
|
||||
a = f.add_subplot(111)
|
||||
t = arange(0.0, 3.0, 0.01)
|
||||
s = sin(2*pi*t)
|
||||
|
||||
a.plot(t, s)
|
||||
a.set_title('Tk embedding')
|
||||
a.set_xlabel('X axis label')
|
||||
a.set_ylabel('Y label')
|
||||
|
||||
# -------------------------------- GUI Starts Here --------------------------------
|
||||
canvas_elem = g.Canvas(size=(500, 400)) # get the canvas we'll be drawing on
|
||||
# define the form layout
|
||||
layout = [[g.Text('Plot test')],
|
||||
[canvas_elem],
|
||||
[g.OK(pad=((250,0), 3))]]
|
||||
|
||||
# create the form and show it without the plot
|
||||
form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI')
|
||||
form.Layout(layout)
|
||||
form.ReadNonBlocking()
|
||||
|
||||
# add the plot to the window
|
||||
fig_photo = draw_figure(canvas_elem.TKCanvas, f)
|
||||
|
||||
# show it all again and get buttons
|
||||
button, values = form.Read()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue