python - ActiveX 对象的 PyQt5 包装器而不是 WX 包装器

标签 python qt pyqt5 qt5 activex

我正在尝试让 USB 摄像头向 PyQt5 应用程序显示实时视频源。我有 wx 包装器的工作代码,但我需要它在 PyQt5 中工作。经过多次搜索,我只是找不到正确的语法。

这是工作的 WX 代码:

import wx
from wx.lib.activexwrapper import MakeActiveXClass
from win32com.client import gencache

class mainFrm( wx.Frame ):
    def __init__( self, *args, **kwds ):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__( self, *args, **kwds )

        self.dcamModule = gencache.EnsureModule( '{6B9BD678-9710-44D9-9282-A088094E4216}', 0, 1, 0 )       
        dcamClass = MakeActiveXClass( self.dcamModule.ActiveGeni, eventObj = self )
        self.camera = dcamClass( self, -1 )

        self.camera.SetSize(( 752, 480 ))
        self.SetClientSize( ( 752, 480 ))

        self.camera.Acquire = True 
        self.camera.Display = True


if __name__ == '__main__':
    GUI = wx.PySimpleApp( 0 )
    frame_1 = mainFrm( None, -1, "" )
    GUI.SetTopWindow( frame_1 )
    frame_1.Show()
    GUI.MainLoop()

当我调试正在发生的事情时,这就是我在构建对象时得到的结果:

print(self.dcamModule)
<module 'win32com.gen_py.6B9BD678-9710-44D9-9282-A088094E4216x0x1x0' from '...\\AppData\\Local\\Temp\\3\\gen_py\\3.5\\6B9BD678-9710-44D9-9282-A088094E4216x0x1x0.py'>

print(dcamClass)
<class 'wx.lib.activexwrapper.AXControl_ActiveGeni'>

print(self.camera)
<win32com.gen_py.None.AXControl_ActiveGeni>

这是我尝试过的 PyQt5。不会给出错误,但也不会启动相机:

import sys
from PyQt5 import uic, QtWidgets
from PyQt5.QAxContainer import QAxWidget

qtCreatorFile = "ui\\camera_form.ui"
LandingPageUI, LandingPageBase = uic.loadUiType(qtCreatorFile)

class cameraForm(LandingPageBase, LandingPageUI):

    def __init__(self,  parent=None):
        QtWidgets.QMainWindow.__init__(self)
        LandingPageBase.__init__(self)
        self.setupUi(self)

        self.ocx = QAxWidget("'{6B9BD678-9710-44D9-9282-A088094E4216}', 0, 1, 0 ")
#Is there something else to do here?
        self.ocx.Acquire = True
        self.ocx.Display = True
        self.axWidget = self.ocx     #axWidget is the QaXWidget on the form


if __name__ == "__main__":
    app=QtWidgets.QApplication.instance() 
    if not app: 
         app = QtWidgets.QApplication(sys.argv)

    window = cameraForm()
    window.show()
    sys.exit(app.exec_())


当我尝试 PyQt 版本时,这是我在调试时得到的结果:

print(self.axWidget)
<PyQt5.QAxContainer.QAxWidget object at 0x036C4C60>

wx 的 MakeActiveXClass 步骤似乎正在做一些 PyQt 没有完成的事情,但我不知道它应该是什么。

以下是我迄今为止引用过的一些资源:

win32.Dispatch vs win32.gencache in Python. What are the pros and cons?

What can you do with COM/ActiveX in Python?

我也尝试过 QCamera,但它无法识别相机。

最佳答案

使self.axWidget = self.ocx不会导致self.ocx替换窗口中的self.axWidget,解决办法是通过使用setControl()设置控件来使用self.axWidget方法:

class cameraForm(LandingPageBase, LandingPageUI):
    def __init__(self, parent=None):
        super(cameraForm, self).__init__(parent)
        self.setupUi(self)

        self.axWidget.setControl("{6B9BD678-9710-44D9-9282-A088094E4216}")
        self.axWidget.setProperty("Acquire", True)
        self.axWidget.setProperty("Display", True) 

(代码未测试)

关于python - ActiveX 对象的 PyQt5 包装器而不是 WX 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60123343/

相关文章:

python - TKinter:如何动态更改帧宽度

qt - 带有 Qt 函数 cv::erode() 和 cv::dilate() 的 OpenCV 崩溃

python - 在pyqt中禁用窗口功能

python - Matplotlib 图与 PyQt5/PySide2 QSplitter Widget 之间的问题

python - 如何在主Python文件中使用多个.ui文件

python - 类型错误 : 'Node' Object is not iterable

python - 在用户输入中搜索关键字,并在字典中找到对应的关键字

python - 从十进制转换为二进制而不使用 bin() 不打印

qt - 我们如何连接带有不同参数的信号和插槽?

Python 3.5 + PyQt5 到独立 exe