无法连接在 class qt 之外导入的函数

Can't connect function imported outside of class qt

我有一个大型函数(600 多行)我不想从我的主代码中删除以提高可读性,但是,我无法在我的主代码中引用该函数 window class。如果我将 import 语句移动到 class 内部,它会无缝运行,但我计划在其他地方使用它,所以不想多次导入它。有没有一种简单的方法可以从 window?

中引用导入的函数
import sys
import cv2
from PySide import QtCore
from PySide import QtGui
import mainWindowUI
from videoFunctions import videoFeed

class MainWindow(QtGui.QMainWindow, mainWindowUI.Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.setup_camera()

    def setup_camera(self):
        self.capture = cv2.VideoCapture(0)      
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.videoFeed)
        self.timer.start(30)

app = QtGui.QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()

回溯:

File "<stdin>", line 1, in <module>
File "C:\WinPython\python-2.7.10.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 790, in runfile
  execfile(filename, namespace)
File "C:\WinPython\python-2.7.10.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 77, in execfile
  exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Scanner.py", line 42, in <module>
form = MainWindow()
File "C:/Scanner.py", line 17, in __init__
self.setup_camera()
File "C:/Scanner.py", line 37, in setup_camera
self.timer.timeout.connect(self.videoFeed)
AttributeError: 'MainWindow' object has no attribute 'videoFeed'

一种可能是将这些方法放入一个单独的 class 中,您将其用作主 class:

中的混入
class VideoFeedMixin(object):
    def videoFeed(self):
        ...

然后:

from videoFunctions import VideoFeedMixin

class MainWindow(VideoFeedMixin, QtGui.QMainWindow, mainWindowUI.Ui_MainWindow):
    ...

现在 self.timer.timeout.connect(self.videoFeed) 将像以前一样工作。