|
21 | 21 |
|
22 | 22 | fig = Figure(figsize=(5, 4), dpi=100)
|
23 | 23 | t = np.arange(0, 3, .01)
|
24 |
| -fig.add_subplot().plot(t, 2 * np.sin(2 * np.pi * t)) |
| 24 | +ax = fig.add_subplot() |
| 25 | +line, = ax.plot(t, 2 * np.sin(2 * np.pi * t)) |
| 26 | +ax.set_xlabel("time [s]") |
| 27 | +ax.set_ylabel("f(t)") |
25 | 28 |
|
26 | 29 | canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
|
27 | 30 | canvas.draw()
|
|
30 | 33 | toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
|
31 | 34 | toolbar.update()
|
32 | 35 |
|
33 |
| - |
34 | 36 | canvas.mpl_connect(
|
35 | 37 | "key_press_event", lambda event: print(f"you pressed {event.key}"))
|
36 | 38 | canvas.mpl_connect("key_press_event", key_press_handler)
|
37 | 39 |
|
38 |
| -button = tkinter.Button(master=root, text="Quit", command=root.quit) |
| 40 | +button_quit = tkinter.Button(master=root, text="Quit", command=root.quit) |
| 41 | + |
| 42 | + |
| 43 | +def update_frequency(new_val): |
| 44 | + # retrieve frequency |
| 45 | + f = float(new_val) |
| 46 | + |
| 47 | + # update data |
| 48 | + y = 2 * np.sin(2 * np.pi * f * t) |
| 49 | + line.set_data(t, y) |
| 50 | + |
| 51 | + # required to update canvas and attached toolbar! |
| 52 | + canvas.draw() |
| 53 | + |
| 54 | + |
| 55 | +slider_update = tkinter.Scale(root, from_=1, to=5, orient=tkinter.HORIZONTAL, |
| 56 | + command=update_frequency, label="Frequency [Hz]") |
39 | 57 |
|
40 | 58 | # Packing order is important. Widgets are processed sequentially and if there
|
41 | 59 | # is no space left, because the window is too small, they are not displayed.
|
42 | 60 | # The canvas is rather flexible in its size, so we pack it last which makes
|
43 | 61 | # sure the UI controls are displayed as long as possible.
|
44 |
| -button.pack(side=tkinter.BOTTOM) |
| 62 | +button_quit.pack(side=tkinter.BOTTOM) |
| 63 | +slider_update.pack(side=tkinter.BOTTOM) |
45 | 64 | toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
|
46 | 65 | canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
|
47 | 66 |
|
|
0 commit comments