-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathDrawRandom.py
More file actions
53 lines (46 loc) · 1.77 KB
/
DrawRandom.py
File metadata and controls
53 lines (46 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import PySimpleGUI as sg
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# Update function
def data_gen():
fig1 = plt.figure(dpi=125)
x = np.round(10*np.random.random(20),3)
y = np.round(10*np.random.random(20),3)
p1 = plt.scatter(x,y,edgecolor='k')
plt.ylabel('Y-Values')
plt.xlabel('X-Values')
plt.title('Scatter plot')
figure_x, figure_y, figure_w, figure_h = fig1.bbox.bounds
return (x,y,fig1,figure_x, figure_y, figure_w, figure_h)
def draw_figure(canvas, figure, loc=(0, 0)):
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
return figure_canvas_agg
def delete_fig_agg(fig_agg):
fig_agg.get_tk_widget().forget()
plt.close('all')
# Define the window's contents i.e. layout
layout = [
[sg.Button('Generate a random scatter plot',enable_events=True, key='-GENERATE-', font='Helvetica 16')],
[sg.Canvas(size=(350,350), key='-CANVAS-', pad=(20,20))],
[sg.Button('Exit')],
]
# Create the window
window = sg.Window('Generate random integer', layout, size=(700,700))
# Event loop
fig_agg = None
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
if event == '-GENERATE-':
if fig_agg is not None:
delete_fig_agg(fig_agg)
_,_,fig1,figure_x, figure_y, figure_w, figure_h = data_gen()
canvas_elem = window['-CANVAS-'].TKCanvas
canvas_elem.Size=(int(figure_w),int(figure_h))
fig_agg = draw_figure(canvas_elem, fig1)
# Close the window i.e. release resource
window.close()