PyQt 程序在没有 Traceback 的情况下随机不响应

PyQt program randomly not responding without a Traceback

我用PyQt5写了一个批量修改多张图片文件名的程序

当我 运行 它时,它 运行 成功地浏览了一些图片,并且在没有 Python Traceback 的情况下随机崩溃,在 windows 中显示 "has stopped working" 8.1.即使我使用相同的测试用例,程序有时会在 20+ 张图片后崩溃,有时只有 2 张,有时 运行s 到最后。

我什至不知道程序在哪一行崩溃了。我该如何解决这个问题?

这是程序,我最小化了仍然有效的代码,并像以前一样随机崩溃。

# -*- coding: utf-8 -*-

import sys
import os
import re
from PyQt5 import Qt

if __name__ == '__main__':
    application_Variable = Qt.QApplication(sys.argv)
    from MainWindow import Ui_MainWindow_Widget
    from FileName import Ui_FileName_Widget

class filename_class: # just a class to get path, filename, append
    def __init__(self, fullpath):

        self.re_path_temp = re.match(r".+/", fullpath)
        self.path = self.re_path_temp.group(0)
        self.name = fullpath[len(self.path):]
        self.append = self.name[len(self.name[:self.name.rfind('.')])-len(self.name)+1:]


class fileNameDefine_Widget(Qt.QWidget): # the rename widget
    def __init__(self, filename):
        super(fileNameDefine_Widget, self).__init__()
        self.fileWidget = Ui_FileName_Widget()
        self.fileWidget.setupUi(self)

        self.filename = filename
        self.file_append = filename_class(self.filename).append # get the append

        self.fileWidget.InputFileName_LineEdit.textChanged.connect(self.input_file_name_Change)

    def input_file_name_Change(self):
        self.export_name = self.fileWidget.InputFileName_LineEdit.text() + "." + self.file_append
        self.fileWidget.ExportFileName_LineEdit.setText(self.export_name)
        self.fileWidget.InputFileName_LineEdit.setEnabled(True)


class MainWindow_Class(Qt.QWidget): # the main widget

    def __init__(self):

        super(MainWindow_Class, self).__init__()

        self.main = Ui_MainWindow_Widget()  # init
        self.main.setupUi(self)
        self.root_directory = r"D:\TLCTest"
        self.file_list = Qt.QFileDialog.getOpenFileNames(caption="Select file", directory=self.root_directory)[0]

        self.count = 0 # count which file are being processed

        self.show()
        self.initiate_change_filename()

    def initiate_change_filename(self):
        file = self.file_list[self.count]

        # show the picture
        self.pixmap = Qt.QPixmap()
        self.pixmap.load(file)
        self.graphicsPixmapItem = Qt.QGraphicsPixmapItem(self.pixmap)
        self.graphicsScene = Qt.QGraphicsScene()
        self.graphicsScene.addItem(self.graphicsPixmapItem)
        self.main.graphicsView.setScene(self.graphicsScene)

        # start the rename widget
        self.fileName_Widget = fileNameDefine_Widget(file)
        self.fileName_Widget.fileWidget.InputFileName_LineEdit.returnPressed.connect(self.submit) # press enter to submit

        self.fileName_Widget.show()

    def submit(self):

        filename = self.fileName_Widget.filename
        path = filename_class(filename).path
        final_name = self.fileName_Widget.fileWidget.ExportFileName_LineEdit.text()

        os.rename(filename, path + final_name)
        self.count += 1

        if self.count == len(self.file_list):
            exit()
        else:
            self.fileName_Widget.close()
            self.initiate_change_filename()

if __name__ == '__main__':
    my_Qt_Program = MainWindow_Class()

    my_Qt_Program.show()
    sys.exit(application_Variable.exec_())

正如 ekhumoro 的评论,这个问题是由重复创建一个 fileNameDefine_Widget 对象引起的,例如:

def initiate_change_filename(self):
    self.fileName_Widget = fileNameDefine_Widget(file)

相反,应该创建一次该对象,并且在每次需要时初始化它,例如:

class TLC_processor_Class(Qt.QWidget):

    def init(self):
        # Blabla
        self.fileName_Widget = fileNameDefine_Widget(file)

    def initiate_change_filename(self):
        self.fileName_Widget.__init__(file)