参考文献
1.下载并解压
image.png
修改MyLabel.pro文件
image.png
把把这一段注释掉 , 这样才会同时生成dll和lib文件。
QT msvc版 release编译后 , 把Release目录拷贝到当前文件夹
编译结果
2.修改configure.py文件
- dll 和 moc_mylabel.obj 文件稍后会用到
#注释参照上一讲
from PyQt5.QtCore import PYQT_CONFIGURATION as pyqt_config
from distutils import sysconfig
import os, sipconfig, sys
config = sipconfig.Configuration()
config.platform = "win32-msvc2015"
qt_path = 'C:/Qt/Qt5.9.1/5.9.1/msvc2015_64'
print('QT_DIR: %s' % qt_path)
config.sip_bin ="C:/Users/Win_Lin/AppData/Local/Programs/Python/Python36/sip.exe"
config.default_sip_dir = "C:/Users/Win_Lin/AppData/Local/Programs/Python/Python36/Lib/site-packages/sip"
sip_files_dir=os.path.abspath(os.path.join(".","sip"))
sip_file = os.path.join(sip_files_dir, "PyMyLabel.sip")
inc_dir=os.path.abspath(os.path.join(".","src"))
sip_files_dir=os.path.abspath(os.path.join(".","sip"))
output_dir =os.path.abspath(os.path.join(".", "modules")) #新建文件
build_file="PyMyLabel.sbf"
build_path = os.path.join(output_dir, build_file)
dest_pkg_dir="PyMyLabel"
cmd=" ".join([
config.sip_bin,
pyqt_config['sip_flags'],
'-I', sip_files_dir,
'-I', config.default_sip_dir,
'-I', config.sip_inc_dir,
'-I', inc_dir,
'-I', config.sip_bin,
"-c", output_dir,
"-b", build_path,
"-w",
"-o",
sip_file,
])
os.makedirs("modules", exist_ok=True)
print(cmd)
os.system(cmd)
makefile=sipconfig.SIPModuleMakefile(
config,
build_file,
dir=output_dir,
install_dir=dest_pkg_dir
)
makefile.extra_defines+=['MYLABEL_LIBRARY','QT_CORE_LIB', 'QT_GUI_LIB', 'QT_WIDGETS_LIB']
makefile.extra_include_dirs = [
'../', '.',
os.path.join(qt_path, 'include'),
os.path.join(qt_path, 'include', 'QtCore'),
os.path.join(qt_path, 'include', 'QtGui'),
os.path.join(qt_path, 'include', 'QtWidgets'),
]
makefile.extra_lib_dirs = [
os.path.join('.', 'Release'),
os.path.join('.', 'Release','release'),
os.path.join('..', 'Release'),
os.path.join('..', '..', 'Release'),
os.path.join('..', 'Release', 'release'),
os.path.join('..', '..', 'Release', 'release'),
os.path.join(qt_path, 'lib'),
]
makefile.extra_libs = [
'MyLabel',
'Qt5Core',
'Qt5Gui',
'Qt5Widgets',
]
makefile.generate()
3. 构建
运行命令
python configure.py
进入modules文件夹 , 修改makefile 文件 , 把 moc_mylabel.obj 添加到 OFILES 中 (否则nmake会报LNK2001错误)
image.png
将src文件夹下的.h拷贝到modules下
image
运行VS2015 X64命令行工具 (参照上一讲 , 很重要!! 否则会出错) , 执行命令
nmake
nmake install
image.png
将 2. 中的dll复制到...\Python36\Lib\site-packages\文件夹下
5. python测试
#sip_label.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyMyLabel import MyLabel
class Widget(QWidget):
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
l=QVBoxLayout(self)
self._myLabel=MyLabel(self)
l.addWidget(self._myLabel)
if __name__=="__main__":
from sys import argv, exit
a=QApplication(argv)
w=Widget()
w.show()
w.raise_()
exit(a.exec_())
测试结果
image.png
如有错误与疑点,请留言指正。
欢迎同道交流指点
QQ群 与 微信公众号
QQ群 : 246269919 ( 已满 )
432987409 (入群先看 本群须知 )
微信公众号:WoHowLearn
( 课件与代码在此 )
简书 : WoHowLearn
GitHub :
-https://github.com/892768447
-https://github.com/625781186
厚脸皮求关注打赏