HOME/Articles/

pyqt example configure (snippet)

Article Outline

Python pyqt (gui) example 'configure'

Modules used in program:

  • import sipconfig
  • import PyQt5
  • import shutil
  • import os

configure

Python pyqt example: configure

#!/usr/bin/env python
# -*- encoding:utf-8 -*-

import os
import shutil

import PyQt5
from PyQt5.QtCore import PYQT_CONFIGURATION
import sipconfig


# 模块名
moduleName = 'QtNinePatch'

# The name of the SIP build file generated by SIP and used by the build
# system.
build_file = '%s.sbf' % moduleName

# Get the SIP configuration information.
config = sipconfig.Configuration()
# pyqt5.5 + python34 + msvc 2010
config.platform = "win32-msvc2010"
qt_path = 'D:/soft/Qt/Qt5.5.1/5.5/msvc2010'
print('QT_DIR: %s' % qt_path)

pyqtpath = os.path.dirname(PyQt5.__file__)
print('PyQt5 path: ', pyqtpath)

config.sip_bin = os.path.join(pyqtpath, 'sip.exe')
config.default_sip_dir = os.path.join(pyqtpath, 'sip')

sip_cmd = ' '.join([
    config.sip_bin,
    '-c', "build",
    '-b', "build/" + build_file,
    '-I', config.default_sip_dir + '/PyQt5',
    PYQT_CONFIGURATION.get('sip_flags', ''),
    '%s.sip' % moduleName,
])

os.makedirs('build', exist_ok=True)
print(sip_cmd)
os.system(sip_cmd)


# Create the Makefile.
makefile = sipconfig.SIPModuleMakefile(
    config, build_file, dir='build'
)

# Add the library we are wrapping.  The name doesn't include any platform
# specific prefixes or extensions (e.g. the 'lib' prefix on UNIX, or the
# '.dll' extension on Windows).

# 添加头文件路径
makefile.extra_include_dirs = [
    '../', '.',
    os.path.join(qt_path, 'include'),
    os.path.join(qt_path, 'include', 'QtCore'),
    os.path.join(qt_path, 'include', 'QtGui')
]

# 添加用C++编译的库文件路径
makefile.extra_lib_dirs = [
    os.path.abspath('../build'),
    os.path.join(qt_path, 'lib'),
]
print(makefile.extra_lib_dirs)

makefile.extra_libs = [
    moduleName,
    'Qt5Core',
    'Qt5Gui'
]
print(makefile.extra_libs)

# Generate the Makefile itself.
makefile.generate()

shutil.copy('../build/%s.dll' % moduleName, '%s.dll' % moduleName)

os.chdir('build')
os.system('nmake')

os.chdir('../')
shutil.copy('build/%s.pyd' % moduleName, '%s.pyd' % moduleName)
print('ok')