python - 属性错误 : 'QWheelEvent' object has no attribute 'delta'

标签 python pyqt pyqt5

我正在使用 pyqt5 制作一个简单的 GUI。它运行良好,但是当我打开它并尝试使用鼠标滚轮时,它崩溃并出现以下错误:

AttributeError: 'QWheelEvent' object has no attribute 'delta'.

这里是重现问题的代码:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

import sys

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import matplotlib.pyplot as plt
import numpy as np

class View(QGraphicsView):

    def __init__(self):
        super(View, self).__init__()

        self.setRenderHint(QPainter.Antialiasing)
        self.initScene(5)

    def initScene(self,h):     

        self.scene = QGraphicsScene()
        #self.setSceneRect(0, 100, 1400, 700)  #this controls where the scene begins relative to the window
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.canvas.setGeometry(0,0,900,700)
        #self.setSceneRect(0, 0, 2607, 700)


        self.figure.subplots_adjust(left=0,right=1,bottom=0,top=1,wspace=0, hspace=0)
        axes1 = self.figure.add_subplot(3, 1, 1)
        axes2 = self.figure.add_subplot(3, 1, 2)
        axes3 = self.figure.add_subplot(3, 1, 3)
        axes1.yaxis.set_ticks([5,6])
        axes1.set_yticklabels([5,6])
        #axes.yaxis.set_offset_position('right')
        axes1.yaxis.set_tick_params(color='red',labelcolor='red',direction='in',labelright = 'on',labelleft='off')
        axes1.plot(np.linspace(0,10,10), np.linspace(0,10,10))
        axes2.plot(np.linspace(0,10,10), np.linspace(0,10,10))
        axes3.plot(np.linspace(0,10,10), np.linspace(0,10,10))
        axes1.spines['bottom'].set_color('red')
        axes2.spines['top'].set_color('red')
        self.canvas.draw()
        self.setScene(self.scene)
        self.scene.addWidget(self.canvas)

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow,self).__init__()

        self.setGeometry(150, 150, 1424, 750) #the first two arguments control where the window will appear on the screen, the next

        self.view = View()
        self.view.setGeometry(0,0,1400,700)
        self.setCentralWidget(self.view)

app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

其他可能相关的细节:我将代码保存为 .py 文件并从“Anaconda Prompt”命令行运行它。

如果我没有将鼠标点击事件连接到任何东西,那么它就不会崩溃,所以我不明白为什么当我使用鼠标滚轮时它会崩溃(即使没有连接到任何东西)。

最佳答案

您使用的是 PyQt5,但随后为 PyQt4 导入了 matplotlib 后端,所以我想这就是错误的来源。

Qt4 在 QWheelEvent 类中有一个 delta 属性,但现在在 Qt5 中,它已被两个不同的属性 angleDelta 所取代pixelDelta 所以这就是你得到错误的原因。

要解决它,只需按如下方式替换您的导入(将 4 替换为 5):

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

引用资料

https://doc-snapshots.qt.io/qt5-dev/qwheelevent.html

关于python - 属性错误 : 'QWheelEvent' object has no attribute 'delta' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48379373/

相关文章:

python - Python 中的哈希集和哈希表

python - cython cdef类的多重继承

python - QCalendar 样式。禁用项目的属性

python - PyQT 从 .txt 文件读取和更新 TextEdit

python - 如何获取 QTreeWidget 可见区域中显示的所有项目?

python - openpyxl,如何读取单元格类型

python - 从 python Opencv Videocapture 接收 Gazebo gstreamer UDP 视频

python - QObject::killTimers 错误 QThread PyQt

python - “selectedFilters”不是有效的关键字参数

python - 通过鼠标滚轮平滑滚动 QTableWidget