문제

I'm having some problems when trying to use slot/signals on a custom class.

The class looks like this:

import sys
from PyQt5 import QtCore
from PyQt5.QtGui import QGuiApplication, QPixmap

class Screenshot(QtCore.QObject):
    newScreenshotTaken = QtCore.pyqtSignal(QPixmap)
    timer = QtCore.QTimer()
    captureInterval = 5 * 60

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

    def startCapture(self):
        self.capture()

    def stopCapture(self):
        self.timer.stop()

    def on_userStartedCapture(self):
        self.startCapture()

    def on_userStoppedCapture(self):
        self.stopCapture()

    def capture(self):
        print("capture!")

The error happens at on_userStartedCapture(self):

  File "/Volumes/HD2/test/screenshot.py", line 23, in on_userStartedCapture
    self.startCapture()
AttributeError: 'NoneType' object has no attribute 'startCapture'

Emit is called from another class:

self.userStartedCapture.emit()

And the connect is done at main.py:

screenshot = Screenshot()
mainWindow = MainWindow()

mainWindow.userStartedCapture.connect(screenshot.on_userStartedCapture)

The strange thing is that self works on all slots/signals in my application. But I can't find out why this specific one is failing.

Any ideas of what might be happening?

도움이 되었습니까?

해결책

The error is raised because self is None at the time the signal is sent. Which is to say, the instance of Screenshot the signal is connected to has been deleted (or is in the process of being deleted).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top