I am having trouble implementing pyqtgraph with PySide6. I have used pyqtgraph with PyQt5 without a hitch, but I'm making a new application in PySide6 specifically for a new project.
I checked the pyqtgraph documentation and it says to import your Qt wrapper before pyqtgraph so it knows which to work with.
import sys
from PySide6.QtWidgets import QMainWindow, QApplication, QTabWidget
from gui_scripts.tab_plot import PlotTab
import PySide6
import pyqtgraph as pg
from __feature__ import snake_case, true_property
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.graph_widget = pg.PlotWidget()
self.set_central_widget(self.graph_widget)
hour = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
temperature = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
self.graph_widget.plot(hour, temperature)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
I get a "RecursionError: maximum recursion depth exceeded while calling a Python object" triggered where self.graph_widget = pg.PlotWidget()
I used this example https://www.pythonguis.com/tutorials/pyside-plotting-pyqtgraph/ to generate the same error because my actual implementation is across a few scripts. The application has a navigation tab that switches between a few interfaces, one of which will be a plot, and the code worked just fine until I added the tab for the plot.
Edit: The code is fine when I don't import the class from tab_plot. That script is
from PySide6.QtWidgets import QWidget, QGridLayout
import sys
import pyqtgraph as pg
from __feature__ import snake_case, true_property
class PlotTab(QWidget):
def __init__(self, parent):
QWidget.__init__(self)
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
self.parent = parent
self.layout = QGridLayout(self)
self.plot1 = pg.PlotWidget()
self.plot1.setLabel('left', 'Temperature (K)')
self.plot1.setLabel('bottom', 'Voltage (V)')
self.layout.add_widget(self.plot1, 0, 0)
Edit2: this is the error happening at: self.plot1 = pg.PlotWidget() File "..\Python\Python310\lib\site-packages\pyqtgraph\widgets\PlotWidget.py", line 51, in init GraphicsView.init(self, parent, background=background)
File "..\Python\Python310\lib\site-packages\pyqtgraph\widgets\GraphicsView.py", line 73, in init self.useOpenGL(useOpenGL)
File "..\Python\Python310\lib\site-packages\pyqtgraph\widgets\GraphicsView.py", line 168, in useOpenGL self.setViewport(v)
File "..\Python\Python310\lib\site-packages\pyqtgraph\widgets\PlotWidget.py", line 78, in getattr if hasattr(self.plotItem, attr):
File ..\Python\Python310\lib\site-packages\pyqtgraph\widgets\PlotWidget.py", line 78, in getattr if hasattr(self.plotItem, attr):
File "..\Python\Python310\lib\site-packages\pyqtgraph\widgets\PlotWidget.py", line 78, in getattr if hasattr(self.plotItem, attr): [Previous line repeated 988 more times] RecursionError: maximum recursion depth exceeded while calling a Python object
Process finished with exit code 1
CodePudding user response:
Okay, I figured it out. pyqtgraph is not compatible with the snake_case feature from PySide6. I thought removing it didn't have an impact, but it was still importing a script that was using it.
CodePudding user response:
I think you need super(MainWindow, self).__init__()
after def __init__(self):
It works fine for me like this
#!/usr/bin/env python3
from PySide6.QtWidgets import QApplication, QMainWindow
import pyqtgraph as pg
import sys
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.graphWidget = pg.PlotWidget()
self.setCentralWidget(self.graphWidget)
hour = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
temperature = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
# plot data: x, y values
self.graphWidget.plot(hour, temperature)
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()