I have a very simple demo to practice pyinstaller by using pyqt5 and pyexiv2, when I run ·python demo.py·, it is ok。
to brief the problem, I need to introduce pyexiv2, it is a picture process package using exiv2.dll and a pyd file. when I pacakge my demo.py by pyinstaller to exe binary, it went strange. some picture can run fluently, but some went nothing.
I mean it didn't get any wrong message, and it didn't run correctly, it just like skip one line of the pyexiv2 code and returned.
here is my code:
import sys
import checkHandler
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QVBoxLayout, QWidget,QDesktopWidget
class DragDropTextEdit(QTextEdit):
def __init__(self, parent=None):
super(DragDropTextEdit, self).__init__(parent)
self.setAcceptDrops(True) #定义的 DragDropTextEdit 类的构造函数中调用的方法,它的作用是启用该文本编辑框接受拖拽操作。
#当用户拖拽文件或其他可拖拽的内容进入文本编辑框时,这个方法会被触发
def dragEnterEvent(self, event):
if event.mimeData().hasUrls(): #它检查拖拽事件中的 MIME 数据是否包含文件路径
event.accept() #如果包含文件路径,它调用 event.accept() 来接受拖拽事件,允许文件拖拽进入文本编辑框。
else: #否则,如果不包含文件路径,它调用 event.ignore() 来忽略拖拽事件,表示不允许拖拽操作。
event.ignore()
def dropEvent(self, event): #当用户释放鼠标按钮时,这个方法会被触发,用于处理拖拽事件。在这个方法中,你可以获取拖拽事件中的文件路径。
for url in event.mimeData().urls(): #使用 event.mimeData().urls() 来获取所有拖拽事件中的 URL 列表,每个 URL 表示一个文件或文件夹。
if url.isLocalFile() and url.toLocalFile(): #使用 url.toLocalFile() 来获取本地文件路径,如果是本地文件的话。
self.append("图片"+url.toLocalFile()+"的检测结果是:"+checkHandler.check_metadata(url.toLocalFile())) #接下来,你可以将这些文件路径添加到文本编辑框中,或者进行任何你希望执行的处理。
class MainApp(QMainWindow): #创建实例化类
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('图片检测器') #设置主窗口的标题为 "文件拖拽窗口"。
# 获取屏幕的宽度和高度
screen = QDesktopWidget().screenGeometry()
screenWidth = screen.width()
screenHeight = screen.height()
# 计算窗口居中的坐标
x = (screenWidth - self.width()) // 2
y = (screenHeight - self.height()) // 2
#设置窗口大小已经出现在屏幕的什么位置
self.setGeometry(x, y, 600, 400) #设置主窗口的初始位置和大小。 (x, y)是设置窗口出现的位置。窗口的宽度为 600 像素,高度为 400 像素。
#初始化窗口排版模式
central = QWidget(self) #创建一个名为 central 的 QWidget(窗口中央部件),用于将其他小部件添加到主窗口的中央区域。
self.setCentralWidget(central) #将 central 部件设置为主窗口的中央部分。这意味着所有其他小部件将放置在 central 部件中,以确保它们在窗口中间显示。
display = QVBoxLayout(central) #创建一个垂直布局管理器 display,它将用于管理 central 部件中的小部件的位置和大小。垂直布局意味着小部件将按垂直方向排列。
#窗口
self.textEdit = DragDropTextEdit() #####这里来实例化上面子类继承的内容DragDropTextEdit 的实例,并将其赋值给 self.textEdit 属性。这个文本编辑框支持文件拖拽功能。
display.addWidget(self.textEdit) #将 self.textEdit 添加到垂直布局管理器 display 中
#按钮
self.submit_Button = QPushButton('提交文件', self) #创建提交按钮的名称
self.submit_Button.clicked.connect(self.processFiles) #给提交按钮绑定事件函数processFiles
display.addWidget(self.submit_Button) #展示出提交按钮
def processFiles(self):
file_paths = self.textEdit.toPlainText().split('\n')
# 这里添加处理文件的代码
return checkHandler.check_metadata(file_paths)
def main():
app = QApplication(sys.argv)
ex = MainApp()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
import pyexiv2
# 1. 元数据检查
def check_metadata(image_path):
metadata = pyexiv2.Image(image_path).read_xmp()
try:
# 检查是否有软件修改记录
if 'Xmp.xmp.CreatorTool' in metadata.keys():
software= metadata['Xmp.xmp.CreatorTool']
return "图片可能被"+str(software)+"编辑过"
else:
return "未检测到被修改"
except KeyError:
return "元数据中没有找到编辑历史记录"
pyinstaller --clean --noconfirm -d all pyqt5.py --add-binary="C:\\Users\\husyh\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\pyexiv2\\lib\\exiv2.dll:./pyexiv2/lib" --add-binary="C:\\Users\\husyh\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\pyexiv2\\lib\\py3.12-win\exiv2api.pyd:./pyexiv2/lib/py3.12-win/"