Skip to content

Commit 8f7356f

Browse files
Completely new set of materials for GUI class! Thanks Tony!!
1 parent ee0765c commit 8f7356f

File tree

53 files changed

+1909
-903
lines changed

Some content is hidden

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

53 files changed

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

0 commit comments

Comments
 (0)