Skip to content

Commit b5e001d

Browse files
Latest updates from Tony
1 parent 3b63c82 commit b5e001d

File tree

93 files changed

+2579
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2579
-52
lines changed

Colours.gif

-40.4 KB
Binary file not shown.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#matplotlib, numpy, pyplot
2+
#Tony Crewe
3+
#Oct 2018 MacOS
4+
5+
import PySimpleGUI as sg
6+
import matplotlib
7+
# added this to work with MacOs
8+
matplotlib.use('TkAgg')
9+
import matplotlib.pyplot as plt
10+
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
11+
import matplotlib.backends.tkagg as tkagg
12+
import numpy as np
13+
import tkinter as tk
14+
15+
"""
16+
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
17+
Adapted: From https://gitlab.com/lotspaih/PySimpleGUI/tree/master
18+
19+
Basic steps are:
20+
* Create a Canvas Element
21+
* Layout form
22+
* Display form (NON BLOCKING)
23+
* Draw plots onto convas
24+
* Display form (BLOCKING)
25+
"""
26+
27+
#No exactly sure how all this works, but taken from example given as a template.
28+
def draw_figure(canvas, figure, loc = (0,0)):
29+
30+
figure_canvas_agg = FigureCanvasAgg(figure)
31+
figure_canvas_agg.draw()
32+
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
33+
figure_w, figure_h = int(figure_w), int(figure_h)
34+
photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
35+
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
36+
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
37+
return photo
38+
39+
40+
#------------ Matplotlib code ----------------------
41+
#see https://matplotlib.org/
42+
fig = plt.figure()
43+
ax = fig.add_subplot(111)
44+
#x-values
45+
x = np.linspace(-np.pi*2, np.pi*2, 100)
46+
#y-values
47+
y = np.sin(x)
48+
plt.plot(x,y)
49+
50+
ax.set_title('sin(x)')
51+
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
52+
53+
#------------End Matplotlib code --------------------
54+
55+
layout = [[sg.Text('Plot Test - PySimpleGUI and Matplotlib', font = ('Calibri', 18, 'bold'))],
56+
[sg.Canvas(size = (figure_w, figure_h), key = '_canvas_')],
57+
[sg.OK(pad=((figure_w / 2, 0), 3), size=(6, 2))]]
58+
59+
window = sg.Window('Matplot in PySimpleGUI', force_toplevel = True).Layout(layout).Finalize()
60+
61+
fig_photo = draw_figure(window.FindElement('_canvas_').TKCanvas, fig)
62+
63+
button, value = window.Read()
64+
65+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#matplotlib, numpy, pyplot
2+
#Tony Crewe
3+
#Oct 2018 MacOS
4+
5+
import PySimpleGUI as sg
6+
import matplotlib
7+
# added this to work with MacOs
8+
matplotlib.use('TkAgg')
9+
import matplotlib.pyplot as plt
10+
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
11+
import matplotlib.backends.tkagg as tkagg
12+
import numpy as np
13+
import tkinter as tk
14+
15+
def draw_figure(canvas, figure, loc = (0,0)):
16+
17+
figure_canvas_agg = FigureCanvasAgg(figure)
18+
figure_canvas_agg.draw()
19+
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
20+
figure_w, figure_h = int(figure_w), int(figure_h)
21+
photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
22+
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
23+
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
24+
return photo
25+
26+
27+
#------------ Matplotlib code --------------------
28+
fig=plt.figure()
29+
ax = fig.add_subplot(111)
30+
x = np.linspace(-np.pi*2, np.pi*2, 100)
31+
y= np.sin(x)
32+
plt.plot(x,y)
33+
34+
ax.set_title('sin(x)')
35+
36+
#centre bottom and left axes to zero
37+
38+
ax.spines['left'].set_position('zero')
39+
ax.spines['right'].set_color('none')
40+
ax.spines['bottom'].set_position('zero')
41+
ax.spines['top'].set_color('none')
42+
43+
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
44+
45+
#------------End Matplotlib code --------------------
46+
47+
layout = [[sg.Text('Plot Test - PySimpleGUI and Matplotlib (axes centred)', font = ('Calibri', 18, 'bold'))],
48+
[sg.Canvas(size = (figure_w, figure_h), key = '_canvas_')],
49+
[sg.OK(pad=((figure_w / 2, 0), 3), size=(6, 2))]]
50+
51+
window = sg.Window('Matplot in PySimpleGUI', force_toplevel = True).Layout(layout).Finalize()
52+
53+
fig_photo = draw_figure(window.FindElement('_canvas_').TKCanvas, fig)
54+
55+
button, value = window.Read()
56+
57+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#matplotlib, numpy, pyplot
2+
#Tony Crewe
3+
#Oct 2018 MacOS
4+
5+
import PySimpleGUI as sg
6+
import matplotlib
7+
# added this to work with MacOs
8+
matplotlib.use('TkAgg')
9+
import matplotlib.pyplot as plt
10+
import matplotlib.ticker as ticker
11+
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
12+
import matplotlib.backends.tkagg as tkagg
13+
import numpy as np
14+
import tkinter as tk
15+
16+
17+
18+
"""
19+
Demonstrates one way of embedding Matplotlib figures into a PySimpleGUI window.
20+
21+
Basic steps are:
22+
* Create a Canvas Element
23+
* Layout form
24+
* Display form (NON BLOCKING)
25+
* Draw plots onto convas
26+
* Display form (BLOCKING)
27+
"""
28+
29+
def draw_figure(canvas, figure, loc = (0,0)):
30+
31+
figure_canvas_agg = FigureCanvasAgg(figure)
32+
figure_canvas_agg.draw()
33+
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
34+
figure_w, figure_h = int(figure_w), int(figure_h)
35+
photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
36+
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
37+
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
38+
return photo
39+
40+
41+
#------------ Matplotlib code --------------------
42+
fig=plt.figure()
43+
ax = fig.add_subplot(111)
44+
x = np.linspace(-np.pi*2, np.pi*2, 100)
45+
y= np.sin(x)
46+
plt.plot(x/np.pi,y)
47+
48+
ax.set_title('sin(x)')
49+
#centre bottom and left axes to zero
50+
51+
ax.spines['left'].set_position('zero')
52+
ax.spines['right'].set_color('none')
53+
ax.spines['bottom'].set_position('zero')
54+
ax.spines['top'].set_color('none')
55+
56+
#Format axes - nicer eh!
57+
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%g $\pi$'))
58+
59+
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
60+
61+
#------------End Matplotlib code --------------------
62+
63+
layout = [[sg.Text('Plot Test - PySimpleGUI and Matplotlib (axes pi format)', font = ('Calibri', 18, 'bold'))],
64+
[sg.Canvas(size = (figure_w, figure_h), key = '_canvas_')],
65+
[sg.OK(pad=((figure_w / 2, 0), 3), size=(6, 2))]]
66+
67+
window = sg.Window('Matplot in PySimpleGUI', force_toplevel = True).Layout(layout).Finalize()
68+
69+
fig_photo = draw_figure(window.FindElement('_canvas_').TKCanvas, fig)
70+
71+
button, value = window.Read()
72+
73+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#matplotlib, numpy, pyplot
2+
#Tony Crewe
3+
#Oct 2018 MacOs
4+
5+
import PySimpleGUI as sg
6+
import matplotlib
7+
# added this to work with MacOs
8+
matplotlib.use('TkAgg')
9+
import matplotlib.pyplot as plt
10+
import matplotlib.ticker as ticker
11+
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
12+
import matplotlib.backends.tkagg as tkagg
13+
import numpy as np
14+
import tkinter as tk
15+
16+
sg.SetOptions (background_color = 'LightBlue',
17+
element_background_color = 'LightBlue',
18+
text_element_background_color = 'LightBlue',
19+
font = ('Arial', 14, 'bold'),
20+
text_color = 'DarkBlue',
21+
input_text_color ='DarkBlue',
22+
button_color = ('DarkBlue', 'White'))
23+
24+
def draw_figure(canvas, figure, loc = (0,0)):
25+
26+
figure_canvas_agg = FigureCanvasAgg(figure)
27+
figure_canvas_agg.draw()
28+
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
29+
figure_w, figure_h = int(figure_w), int(figure_h)
30+
photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
31+
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
32+
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
33+
return photo
34+
35+
36+
#------------ Matplotlib code --------------------
37+
38+
def set_plot(amp, function):
39+
global figure_w, figure_h, fig
40+
fig=plt.figure()
41+
ax = fig.add_subplot(111)
42+
x = np.linspace(-np.pi*2, np.pi*2, 100)
43+
if function == 'sine':
44+
y= amp*np.sin(x)
45+
ax.set_title('sin(x)')
46+
else:
47+
y=amp*np.cos(x)
48+
ax.set_title('cos(x)')
49+
plt.plot(x/np.pi,y)
50+
51+
52+
#centre bottom and left axes to zero
53+
54+
ax.spines['left'].set_position('zero')
55+
ax.spines['right'].set_color('none')
56+
ax.spines['bottom'].set_position('zero')
57+
ax.spines['top'].set_color('none')
58+
59+
#Format axes - nicer eh!
60+
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%g $\pi$'))
61+
62+
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
63+
amp = 1
64+
function = 'sine'
65+
set_plot(amp, function)
66+
67+
#------------End Matplotlib code --------------------
68+
69+
#use Tabs - one for options, one for canvas to be displayed
70+
#set spinner for amplitude and combo for function type
71+
72+
tab1_layout = [[sg.Text('Select Amplitude and trig function type', font = ('Calibri', 18, 'bold'))],
73+
[sg.Spin([sz for sz in range (1,5)], initial_value =1, size = (2,1), key = '_spin_'),
74+
sg.Text('Amplitude', size = (10, 1), font = ('Calibri', 12, 'bold'))],
75+
[sg.InputCombo(['sine', 'cosine'], size = (8, 4), key = '_function_'),
76+
sg.Text('Function', size = (10, 1),font = ('Calibri', 12, 'bold'))],
77+
[sg.ReadButton('Redraw Plot')],
78+
[sg.Text('', size = (2, 25))]]
79+
80+
tab2_layout = [[sg.Text('Plot Test - PySimpleGUI and Matplotlib and options', font = ('Calibri', 18, 'bold'))],
81+
[sg.Canvas(size = (figure_w, figure_h), key = '_canvas_')],
82+
[sg.OK(pad=((figure_w / 2, 0), 3), size=(6, 2))]]
83+
84+
layout = [[sg.TabGroup([[sg.Tab('Select options', tab1_layout), sg.Tab('Display Plot', tab2_layout)]])]]
85+
window = sg.Window('Matplot, PySimpleGUI and options', force_toplevel = True).Layout(layout).Finalize()
86+
87+
fig_photo = draw_figure(window.FindElement('_canvas_').TKCanvas, fig)
88+
89+
while True:
90+
button, value = window.Read()
91+
if button == 'Redraw Plot':
92+
amp = int(value['_spin_'])
93+
function = value['_function_']
94+
set_plot(amp,function)
95+
fig_photo = draw_figure(window.FindElement('_canvas_').TKCanvas, fig)
96+
97+
if button is None:
98+
break
99+
100+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#matplotlib, numpy, pyplot
2+
#Tony Crewe
3+
#Oct 2018 MacOs
4+
5+
import PySimpleGUI as sg
6+
import matplotlib
7+
# added this to work with MacOs
8+
matplotlib.use('TkAgg')
9+
import matplotlib.pyplot as plt
10+
import matplotlib.ticker as ticker
11+
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
12+
import matplotlib.backends.tkagg as tkagg
13+
import numpy as np
14+
import tkinter as tk
15+
16+
sg.SetOptions (background_color = 'LightBlue',
17+
element_background_color = 'LightBlue',
18+
text_element_background_color = 'LightBlue',
19+
font = ('Arial', 14, 'bold'),
20+
text_color = 'DarkBlue',
21+
input_text_color ='DarkBlue',
22+
button_color = ('DarkBlue', 'White'))
23+
24+
def draw_figure(canvas, figure, loc = (0,0)):
25+
26+
figure_canvas_agg = FigureCanvasAgg(figure)
27+
figure_canvas_agg.draw()
28+
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
29+
figure_w, figure_h = int(figure_w), int(figure_h)
30+
photo = tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
31+
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
32+
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
33+
return photo
34+
35+
36+
#------------ Matplotlib code --------------------
37+
38+
def set_plot(amp, function):
39+
global figure_w, figure_h, fig
40+
fig=plt.figure()
41+
ax = fig.add_subplot(111)
42+
x = np.linspace(-np.pi*2, np.pi*2, 100)
43+
if function == 'sine':
44+
y= amp*np.sin(x)
45+
ax.set_title('sin(x)')
46+
else:
47+
y=amp*np.cos(x)
48+
ax.set_title('cos(x)')
49+
plt.plot(x/np.pi,y)
50+
51+
52+
#centre bottom and left axes to zero
53+
54+
ax.spines['left'].set_position('zero')
55+
ax.spines['right'].set_color('none')
56+
ax.spines['bottom'].set_position('zero')
57+
ax.spines['top'].set_color('none')
58+
59+
#Format axes - nicer eh!
60+
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%g $\pi$'))
61+
62+
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds
63+
amp = 1
64+
function = 'sine'
65+
set_plot(amp, function)
66+
67+
#------------End Matplotlib code --------------------
68+
69+
#using one window based on two columns instead of Tabs
70+
column1 = [[sg.Text('Select Amplitude and trig function type', font = ('Calibri', 18, 'bold'))],
71+
[sg.Spin([sz for sz in range (1,5)], initial_value =1, size = (2,1), key = '_spin_'),
72+
sg.Text('Amplitude', size = (10, 1), font = ('Calibri', 12, 'bold'))],
73+
[sg.InputCombo(['sine', 'cosine'], size = (8, 4), key = '_function_'),
74+
sg.Text('Function', size = (10, 1),font = ('Calibri', 12, 'bold'))],
75+
[sg.ReadButton('Redraw Plot')],
76+
[sg.Text('', size = (1, 27))]]
77+
78+
column2 = [[sg.Text('Plot Test - PySimpleGUI and Matplotlib and options', font = ('Calibri', 18, 'bold'))],
79+
[sg.Canvas(size = (figure_w, figure_h), key = '_canvas_')],
80+
[sg.OK(pad=((figure_w / 2, 0), 3), size=(6, 2))]]
81+
82+
layout = [[sg.Column(column1), sg.Column(column2)]]
83+
window = sg.Window('Matplot, PySimpleGUI and options', force_toplevel = True).Layout(layout).Finalize()
84+
85+
fig_photo = draw_figure(window.FindElement('_canvas_').TKCanvas, fig)
86+
87+
while True:
88+
button, value = window.Read()
89+
if button == 'Redraw Plot':
90+
amp = int(value['_spin_'])
91+
function = value['_function_']
92+
set_plot(amp,function)
93+
fig_photo = draw_figure(window.FindElement('_canvas_').TKCanvas, fig)
94+
95+
if button is None:
96+
break
97+
98+

0 commit comments

Comments
 (0)