commit
712c146f7f
|
@ -1,9 +1,7 @@
|
||||||
import PySimpleGUI as g
|
import PySimpleGUI as g
|
||||||
import matplotlib
|
import matplotlib
|
||||||
matplotlib.use('TkAgg')
|
matplotlib.use('TkAgg')
|
||||||
from numpy import arange, sin, pi
|
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
|
||||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg
|
|
||||||
from matplotlib.figure import Figure
|
|
||||||
import matplotlib.backends.tkagg as tkagg
|
import matplotlib.backends.tkagg as tkagg
|
||||||
import tkinter as Tk
|
import tkinter as Tk
|
||||||
|
|
||||||
|
@ -42,22 +40,30 @@ def draw_figure(canvas, figure, loc=(0, 0)):
|
||||||
# which must be kept live or else the picture disappears
|
# which must be kept live or else the picture disappears
|
||||||
return photo
|
return photo
|
||||||
|
|
||||||
f = Figure(figsize=(5, 4), dpi=100)
|
#------------------------------- PASTE YOUR MATPLOTLIB CODE HERE -------------------------------
|
||||||
a = f.add_subplot(111)
|
import numpy as np
|
||||||
t = arange(0.0, 3.0, 0.01)
|
import matplotlib
|
||||||
s = sin(2*pi*t)
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
a.plot(t, s)
|
# Fixing random state for reproducibility
|
||||||
a.set_title('Tk embedding')
|
np.random.seed(19680801)
|
||||||
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
|
matplotlib.rcParams['axes.unicode_minus'] = False
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
|
||||||
|
ax.set_title('Using hyphen instead of Unicode minus')
|
||||||
|
|
||||||
|
# -------------------------------- GUI Starts Here -------------------------------#
|
||||||
|
# fig = your figure you want to display. Assumption is that 'fig' holds the #
|
||||||
|
# information to display. #
|
||||||
|
# --------------------------------------------------------------------------------#
|
||||||
|
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
|
||||||
|
canvas_elem = g.Canvas(size=(figure_w, figure_h)) # get the canvas we'll be drawing on
|
||||||
# define the form layout
|
# define the form layout
|
||||||
layout = [[g.Text('Plot test')],
|
layout = [[g.Text('Plot test')],
|
||||||
[canvas_elem],
|
[canvas_elem],
|
||||||
[g.OK(pad=((250,0), 3))]]
|
[g.OK(pad=((figure_w/2,0), 3), size=(4,2))]]
|
||||||
|
|
||||||
# create the form and show it without the plot
|
# create the form and show it without the plot
|
||||||
form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI')
|
form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI')
|
||||||
|
@ -65,7 +71,7 @@ form.Layout(layout)
|
||||||
form.ReadNonBlocking()
|
form.ReadNonBlocking()
|
||||||
|
|
||||||
# add the plot to the window
|
# add the plot to the window
|
||||||
fig_photo = draw_figure(canvas_elem.TKCanvas, f)
|
fig_photo = draw_figure(canvas_elem.TKCanvas, fig)
|
||||||
|
|
||||||
# show it all again and get buttons
|
# show it all again and get buttons
|
||||||
button, values = form.Read()
|
button, values = form.Read()
|
||||||
|
|
|
@ -850,7 +850,7 @@ class Frame(Element):
|
||||||
# Slider #
|
# Slider #
|
||||||
# ---------------------------------------------------------------------- #
|
# ---------------------------------------------------------------------- #
|
||||||
class Slider(Element):
|
class Slider(Element):
|
||||||
def __init__(self, range=(None,None), default_value=None, orientation=None, border_width=None, relief=None, scale=(None, None), size=(None, None), font=None, background_color=None, text_color=None, key=None, pad=None):
|
def __init__(self, range=(None,None), default_value=None, resolution=None, orientation=None, border_width=None, relief=None, scale=(None, None), size=(None, None), font=None, background_color=None, text_color=None, key=None, pad=None):
|
||||||
'''
|
'''
|
||||||
Slider Element
|
Slider Element
|
||||||
:param range:
|
:param range:
|
||||||
|
@ -869,6 +869,8 @@ class Slider(Element):
|
||||||
self.Orientation = orientation if orientation else DEFAULT_SLIDER_ORIENTATION
|
self.Orientation = orientation if orientation else DEFAULT_SLIDER_ORIENTATION
|
||||||
self.BorderWidth = border_width if border_width else DEFAULT_SLIDER_BORDER_WIDTH
|
self.BorderWidth = border_width if border_width else DEFAULT_SLIDER_BORDER_WIDTH
|
||||||
self.Relief = relief if relief else DEFAULT_SLIDER_RELIEF
|
self.Relief = relief if relief else DEFAULT_SLIDER_RELIEF
|
||||||
|
self.Resolution = 1 if resolution is None else resolution
|
||||||
|
|
||||||
super().__init__(ELEM_TYPE_INPUT_SLIDER, scale=scale, size=size, font=font, background_color=background_color, text_color=text_color, key=key, pad=pad)
|
super().__init__(ELEM_TYPE_INPUT_SLIDER, scale=scale, size=size, font=font, background_color=background_color, text_color=text_color, key=key, pad=pad)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -1084,6 +1086,9 @@ class FlexForm:
|
||||||
if self.RootNeedsDestroying:
|
if self.RootNeedsDestroying:
|
||||||
self.TKroot.destroy()
|
self.TKroot.destroy()
|
||||||
_my_windows.Decrement()
|
_my_windows.Decrement()
|
||||||
|
# if form was closed with X
|
||||||
|
if self.LastButtonClicked is None and self.LastKeyboardEvent is None and self.ReturnValues[0] is None:
|
||||||
|
_my_windows.Decrement()
|
||||||
if self.LastKeyboardEvent is not None or self.LastButtonClicked is not None:
|
if self.LastKeyboardEvent is not None or self.LastButtonClicked is not None:
|
||||||
return BuildResults(self, False, self)
|
return BuildResults(self, False, self)
|
||||||
else:
|
else:
|
||||||
|
@ -1805,7 +1810,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form):
|
||||||
else:
|
else:
|
||||||
range_from = element.Range[0]
|
range_from = element.Range[0]
|
||||||
range_to = element.Range[1]
|
range_to = element.Range[1]
|
||||||
tkscale = tk.Scale(tk_row_frame, orient=element.Orientation, variable=element.TKIntVar, from_=range_from, to_=range_to, length=slider_length, width=slider_width , bd=element.BorderWidth, relief=element.Relief, font=font)
|
tkscale = tk.Scale(tk_row_frame, orient=element.Orientation, variable=element.TKIntVar, from_=range_from, to_=range_to, resolution = element.Resolution, length=slider_length, width=slider_width , bd=element.BorderWidth, relief=element.Relief, font=font)
|
||||||
# tktext_label.configure(anchor=tk.NW, image=photo)
|
# tktext_label.configure(anchor=tk.NW, image=photo)
|
||||||
tkscale.config(highlightthickness=0)
|
tkscale.config(highlightthickness=0)
|
||||||
if element.BackgroundColor is not None:
|
if element.BackgroundColor is not None:
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue