我编写了一个简单的图像查看器。它可以很好地查看图像,但是当它尝试关闭并移动QMovie播放的.gif文件时,它就会崩溃。问题是仍然打开的文件句柄。如何在仍在播放gif的同时关闭文件句柄?
这是显示代码。
# -*- coding: utf-8 -*-
import shutil
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
currentFile = "B:\\A.gif" # <=== GIF
newFile = "B:\\B.gif"
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1600, 900)
MainWindow.setMinimumSize(QtCore.QSize(1600, 900))
MainWindow.setMaximumSize(QtCore.QSize(1600, 900))
MainWindow.setWindowTitle("Demo")
MainWindow.setWindowOpacity(1.0)
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background: rgb(125, 125, 125);\n"
"font: 10pt \"Verdana\";\n"
"color: rgb(223, 223, 223)")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.moveButton = QtWidgets.QPushButton(self.centralwidget)
self.moveButton.setGeometry(QtCore.QRect(810, 820, 110, 30))
self.moveButton.setObjectName("moveButton")
self.moveButton.clicked.connect(self.moveFile)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(110, 0, 1380, 820))
self.label.setStyleSheet("background-color: rgb(85, 85, 85);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.loadImage()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.moveButton.setText(_translate("MainWindow", "Move File"))
def loadImage(self):
print(currentFile)
image_reader = QtGui.QImageReader()
image_reader.setDecideFormatFromContent(True)
image_reader.setFileName(currentFile)
if image_reader.format() == 'gif':
movie = QtGui.QMovie(currentFile)
movie.setCacheMode(QtGui.QMovie.CacheAll)
movie.setScaledSize(self.label.size())
self.label.setMovie(movie)
movie.start()
else:
image = image_reader.read()
myPixmap = QtGui.QPixmap.fromImage(image)
myScaledPixmap = myPixmap.scaled(self.label.size(), QtCore.Qt.KeepAspectRatio)
self.label.setPixmap(myScaledPixmap)
def moveFile(self):
shutil.move(currentFile, newFile)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
完整的错误消息:
B:\demo\A.gif
Traceback (most recent call last):
File "C:\Users\Zottelchen\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 557, in move
os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'B:\\demo\\A.gif' -> 'B:\\demo\\B.gif'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "mini.py", line 63, in moveFile
shutil.move(currentFile, newFile)
File "C:\Users\Zottelchen\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 572, in move
os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'B:\\demo\\A.gif'
尝试关闭.gif时,它只是崩溃而没有错误消息。