I can't get the matplotlib slider to respond inside a PyQt6 application. Here is a sample program. I know there is no binding on the slider (that's not the problem). The problem is the slider itself doesn't respond to mouse events (ie. clicking, dragging).
I'm using python 3.10.12, matplotlib 3.8.2, PyQt6 6.6.1
import numpy as np
from matplotlib.backends.backend_qtagg import FigureCanvas
from matplotlib.backends.backend_qtagg import \
NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.figure import Figure
from matplotlib.widgets import Slider
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
fig = Figure(figsize=(5, 3))
static_canvas = FigureCanvas(fig)
layout.addWidget(NavigationToolbar(static_canvas, self))
layout.addWidget(static_canvas)
self._static_ax = static_canvas.figure.subplots()
t = np.linspace(0, 10, 501)
self._static_ax.plot(t, np.tan(t), ".")
fig.subplots_adjust(left=0.25)
axamp = fig.add_axes([0.1, 0.25, 0.0225, 0.63])
amp_slider = Slider(
ax=axamp,
label="Amplitude",
valmin=0,
valmax=10,
valinit=6,
orientation="vertical"
)
amp_slider.on_changed(self.slider_callback)
def slider_callback(self):
print("slider is changing")
if __name__ == "__main__":
# Check whether there is already a running QApplication (e.g., if running
# from an IDE).
qapp = QtWidgets.QApplication.instance()
if not qapp:
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
app.activateWindow()
app.raise_()
qapp.exec()
axargument, which only tells where the slider would be put (as explained in the Slider docs) but won't do anything beyond avoiding destruction. The slider demo clearly shows that the update function has to be registered to the slider changes:amp_slider.on_changed(<function that actually updates the figure>).on_changedevent it will never get called because the slider itself doesn't move and will never generate theon_changedevent.