python - 如何在 Python 的 Tkinter 的类中使用 slider.on_changed()
问题描述
我正在尝试使用水平滑块来更改我的情节的 xlim。但首先,我不知道如何使用 on_changed() 方法让滑块更新。我对类和对象如何相互交互没有深入的了解。
我使用这个滑块示例作为模板:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
delta_f = 5.0
s = a0 * np.sin(2 * np.pi * f0 * t)
l, = plt.plot(t, s, lw=2)
ax.margins(x=0)
axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
def update(val):
amp = samp.val
freq = sfreq.val
l.set_ydata(amp*np.sin(2*np.pi*freq*t))
fig.canvas.draw_idle()
sfreq.on_changed(update)
samp.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
sfreq.reset()
samp.reset()
button.on_clicked(reset)
rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)
def colorfunc(label):
l.set_color(label)
fig.canvas.draw_idle()
radio.on_clicked(colorfunc)
plt.show()
我在我的应用程序中难以实现的部分是:
sfreq.on_changed(update)
samp.on_changed(update)
如果您使用 plt.show() 打开绘图,它工作正常,但如果您像我在下面做的那样将它打包到画布中,它会停止工作。任何想法为什么?
这是我的应用程序的代码: 更新:我简化了代码以专注于问题。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import(FigureCanvasTkAgg, NavigationToolbar2Tk)
from tkinter import *
import tkinter as tk
global running
running = True
global graph_exists
graph_exists = False
class MyApp:
def __init__(self, parent):
self.myParent = parent ###remember my parent, the root
self.sfreq = 0
self.samp = 0
def run(self):
self.fig, self.ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
self.t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
delta_f = 5.0
s = a0 * np.sin(2 * np.pi * f0 * self.t)
self.l, = plt.plot(self.t, s, lw=2)
self.ax.margins(x=0)
axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
self.sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
self.samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
canvas = FigureCanvasTkAgg(self.fig, root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
graph_exists = True
def update(self, val):
amp = self.samp.val
freq = self.sfreq.val
self.l.set_ydata(amp*np.sin(2*np.pi*freq*self.t))
self.fig.canvas.draw_idle()
if graph_exists:
self.sfreq.on_changed(update)
self.samp.on_changed(update)
#Run the event loop
root = tk.Tk()
myapp = MyApp(root)
myapp.run()
root.mainloop()
解决方案
正如我评论中的链接所述,您需要在绘图之前将图形添加到画布,否则滑块不起作用。您也应该on_changed(...)
在创建滑块后调用。
def run(self):
self.fig, self.ax = plt.subplots()
canvas = FigureCanvasTkAgg(self.fig, root) # add the figure to canvas before plotting
plt.subplots_adjust(left=0.25, bottom=0.25)
self.t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
delta_f = 5.0
s = a0 * np.sin(2 * np.pi * f0 * self.t)
self.l, = plt.plot(self.t, s, lw=2)
self.ax.margins(x=0)
axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
self.sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
self.samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
### setup on_changed callbacks
self.sfreq.on_changed(self.update) # use 'self.update' instead of 'update'
self.samp.on_changed(self.update)
###
#canvas = FigureCanvasTkAgg(self.fig, root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
graph_exists = True
推荐阅读
- java - ConcurrentModificationException 偶尔
- google-sheets - 如何准确识别 Google 表格中的错误类型?
- c# - 有没有办法让十字准线固定在玩家的 Y 轴上(2d 上视图)并在 Y 轴上移动,与鼠标光标一样远?
- css - 更好地显示表格中的单选按钮 - 仅限 CSS 的解决方案
- python - 如何在pyqt5中设置快捷方式,以便可以从字符串变量中设置快捷方式?
- javascript - 在 iOS Safari 下使用 getUserMedia 流式传输麦克风输入
- scala - Sink.actorRefWithAck 可以批量发送消息吗?
- javascript - 从 ExcelJS 工作簿生成 Base64
- mysql - MySQL 查询中的 % 的 LEN()
- azure - 从 Azure 中的 API URL 获取 XML 数据