Как мне освободить дескриптор файла с Qt QMovie? - PullRequest
0 голосов
/ 18 мая 2019

Я написал простую программу просмотра изображений.Он может просматривать изображения очень хорошо, но при попытке shutil.move .gif-файла, воспроизводимого с помощью QMovie, происходит сбой.Проблема - это все еще открытый файловый дескриптор.Как закрыть дескриптор файла, продолжая играть 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'

При попытке шутила.mif .gif просто вылетает без сообщения об ошибке.

...