You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在PySide6中使用pyqtgraph出现RecursionError递归错误

PySide6集成pyqtgraph触发递归深度超限错误

之前在PyQt5环境下使用pyqtgraph完全正常,新项目切换到PySide6开发后,集成pyqtgraph时出现问题。

按照pyqtgraph文档要求,先导入Qt包装库再导入pyqtgraph以指定适配版本,编写测试代码如下:

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())

执行self.graph_widget = pg.PlotWidget()时触发错误:RecursionError: 调用Python对象时超过最大递归深度

实际项目为带导航标签页的应用,添加绘图标签页前代码运行正常。

补充信息:不导入tab_plot中的PlotTab类时,代码可正常运行。tab_plot.py代码如下:

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)

进一步排查发现,错误实际发生在self.plot1 = pg.PlotWidget()处,调用栈显示递归发生在pyqtgraph的PlotWidget类的__getattr__方法中,重复调用hasattr(self.plotItem, attr)直至超过最大递归深度:

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):

[该语句重复988次]
RecursionError: 调用Python对象时超过最大递归深度

内容的提问来源于stack exchange,提问作者Teddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 09:09:18