如何使用Qt QMovie释放文件句柄?

2024-09-30 06:26:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个简单的图像查看器。它可以很好地查看图像,但是当它尝试shutil.移动用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'

当你试图shutil.移动一个.gif文件在没有错误信息的情况下崩溃了。你知道吗


Tags: 文件imageselfmoviegiflabelreadershutil

热门问题