Install the PyQt5 environment
Install PyQt5 in PyCharm
pip install PyQt5 -i https://pypi.douban.com/simple
Install the Qt toolkit in PyCharm
pip install PyQt5-tools -i https://pypi.douban.com/simple
QT official website: https://doc.qt.io/
Cursor pointer
QT provides a very convenient method to set the shape of the mouse. There is QCursor cursor() in the root class QWidget of the QT interface.
self.Button.setCursor(Qt.WaitCursor)
self.Button.setCursor(Qt.PointingHandCursor)
self.Button.setCursor(Qt.ArrowCursor)
PointingHandCursor 变为手型
CrossCursor 变为十字型
ArrowCursor 变为箭头型
UpArrowCursor 变为向上箭头型
IBeamCursor 变为文本输入型
WaitCursor 变为等待型
BusyCursor 变为繁忙型
ForbiddenCursor 变为禁止型
WhatsThisCursor 变为问号型
SizeVerCursor 变为垂直拖拽型
SizeHorCursor 变为水平拖拽性
SizeBDiagCursor 变为对角线调整大小型
SizeAllCursor 变为移动对象型
SplitHCursor 变为水平拆分型
SplitVCursor 变为垂直拆分型
OpenHandCursor 变为打开型
ClosedHandCursor 变为关闭型
BlankCursor 变为空白型
Read display image
imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png")
jpg = QtGui.QPixmap(imgName).scaled(240, 240)
self.logoImage.setPixmap(jpg)
save picture
fileName, tmp = QFileDialog.getSaveFileName(self, "保存数学派图标", "", "*.jpg;;*.png")
im = Image.open(self.imageResource)
im.save(fileName)
Rewrite the drag event
def mouseMoveEvent(self, e: QMouseEvent):
self._endPos = e.pos() - self._startPos
self.move(self.pos() + self._endPos)
def mousePressEvent(self, e: QMouseEvent):
if e.button() == Qt.LeftButton:
self._isTracking = True
self._startPos = QPoint(e.x(), e.y())
def mouseReleaseEvent(self, e: QMouseEvent):
if e.button() == Qt.LeftButton:
self._isTracking = False
self._startPos = None
self._endPos = None
common error
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes
in position 2-3: truncated \UXXXXXXXX escape
pyinstaller package
Generate the spec file corresponding to the package for the main program file of the code through the command to generate the spec file
pyi-makespec app.py
pyinstaller is a commonly used Python packaging tool that can package Python code into independent executable files. The following are commonly used pyinstaller packaging parameters, which can be set according to actual needs.
parameter | effect |
---|---|
-F or --onefile | Package the generated files into a single executable file for easy distribution and use. |
-w or --windowed | Hide the command line window of the generated executable file to make it more beautiful. |
-n or --name | Specifies the name of the resulting executable. |
-i or --icon | Specifies the icon for the generated executable. |
–hidden-import | Specifies the hidden modules that need to be imported. |
–add-data | Specify the data files that need to be packaged. |
–upx | Compress executables with UPX, reducing file size. |
–clean | Clear cache and temporary files before packaging. |
Generate exe file according to spec
pyinstaller app.spec
Configure static resource files in datas
a = Analysis(
['app.py'],
pathex=[],
binaries=[],
datas=[('icon.png', '.')],
hiddenimports=[],
hookspath=[],
hooksconfig={
},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
After the packaging is completed, there is only one executable file under dist at this time. When running this executable file, an error occurs, and the static resource file cannot be found.
Reason: When running an executable file, the executable file will be compressed first, and the compressed location is under /tmp, and then executed, so the packaged data files are under the decompressed path, while the program is under the running path Search, that is, under the directory of the executable file, so the data file cannot be found.
Solution: configure the runtime tmp directory
import os
import sys
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)