-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvc_tk.py
59 lines (45 loc) · 1.24 KB
/
mvc_tk.py
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
54
55
56
57
58
59
#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, cos, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
root = Tk.Tk()
root.wm_title("Embedding in TK")
f = Figure()
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
c = cos(2*pi*t)
# A tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
# Plot functions
def plot_sin():
a.clear()
a.plot(t, s)
a.set_title('Sin')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')
canvas.show()
def plot_cos():
a.clear()
a.plot(t, c)
a.set_title('Cos')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')
canvas.show()
# Buttons
quit_button = Tk.Button(master=root, text='Quit', command=sys.exit)
quit_button.pack(side=Tk.LEFT)
sin_button = Tk.Button(master=root, text='sin', command=plot_sin)
sin_button.pack(side=Tk.LEFT)
cos_button = Tk.Button(master=root, text='cos', command=plot_cos)
cos_button.pack(side=Tk.LEFT)
Tk.mainloop()