python调用dll、so库
因为挺简单的,直接上代码。
界面使用pyqt画的
1.引入库以及获取库方法
代码如下(示例):
/*可以看注释帮助理解*/
import sys
from ctypes import * //python引用C++库的模块
import threading//线程
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog//界面类
from mainWindow import Ui_Dialog//ui源码
#python回调函数
def _callback(para):
return 1
CALLBACK =CFUNCTYPE(c_int,c_int)//回调函数
callBackFunc = CALLBACK(_callback)
//写一个类加载dll中方法
class testdll:
def __init__(self):
self.cdll = cdll.LoadLibrary("./xxx.dll")
return
def ownFuncB(self):
self.cdll.FuncC()
return
def ownFuncC(self,a,b,c,d,e,f):
self.cdll.FuncD(a,b,c,d,e,f)
return
def ownFuncD(self,a,b,c,d,e,f):
self.cdll.FuncE(a,b,c,d,e,f)
return
def ownFuncE(self,callback):
self.cdll.FuncF(callback)//此处为回调
self.cdll.FuncG()
self.cdll.FuncH()
return
def ownFuncF(self, callback):
self.cdll.FuncI(callback)
return
dllTest = testdll()//类对象
def start(self):
dllTest.ownFunc();
def end(self):
dllTest.ownFunc();
mainWindowUi = Ui_Dialog()
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = QDialog()
mainWindowUi.setupUi(dialog)
mainWindowUi.pushButton.clicked.connect(start)
mainWindowUi.pushButton_2.clicked.connect(end)
dialog.show()
sys.exit(app.exec_())
UI源码,建议用qt设计师直接拖
代码如下(示例):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(660, 420)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(140, 370, 120, 36))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(400, 370, 120, 36))
self.pushButton_2.setObjectName("pushButton_2")
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(30, 10, 600, 350))
self.textBrowser.setObjectName("textBrowser")
self.retranslateUi(Dialog)
#self.pushButton.clicked.connect(Dialog.startSign)
#self.pushButton_2.clicked.connect(Dialog.endSign)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "功能"))
self.pushButton.setText(_translate("Dialog", "操作1"))
self.pushButton_2.setText(_translate("Dialog", "操作2"))
总结
1、加载库
cdll = cdll.LoadLibrary("./XXX.dll")//dll路径,替换为自己的路径,so库也是同样的写法
cdll.FuncA()//dll中的方法。类似于win下GetProcAddress(),linux下Qt的 QLibrary.resolve();
2、回调
def _callback(para):
return 1
CALLBACK =CFUNCTYPE(c_int,c_int)//回调函数
callBackFunc = CALLBACK(_callback)
3、线程
class myThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
testthread()
def testthread():
//Func();
4、展示图片
mainWindowUi.textBrowser.document().setHtml("""<img src="sign.png"/>""")
mainWindowUi.textBrowser.show()
总体不难,可以试试。
版权声明:本文为weixin_38908576原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。