-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_embedding.py
61 lines (46 loc) · 1.75 KB
/
test_embedding.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
60
61
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import tkinter as tk
# matplotlib.figure.Figure.text is a method for adding text to a Figure
# object.
root = tk.Tk()
root.wm_title("Embedding in TK")
f = Figure()
f.set_dpi(96)
f.set_size_inches(13, 6)
# f.text could add text to the figure.
a = f.add_subplot(111)
frame1 = f.gca()
frame1.axes.get_xaxis().set_visible(False)
frame1.axes.get_yaxis().set_visible(False)
a.text(x=1, y=1, s='1,1', family='monospace', size=10)
a.text(x=0, y=0, s='0,0', family='monospace', size=10)
a.text(x=0, y=1, s='0,1', family='monospace', size=10)
a.text(x=1, y=0, s='1,0', family='monospace', size=10)
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def on_key_event(event):
print('you pressed %s' % event.key)
key_press_handler(event, canvas, toolbar)
canvas.mpl_connect('key_press_event', on_key_event)
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL
# tstate
button = tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=tk.BOTTOM)
tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager.