如果没有Traceback,PyQt程序会随机响应

时间:2015-03-17 16:32:04

标签: python pyqt pyqt5

我编写了一个程序,使用PyQt5批量更改许多图片的文件名。

当我运行它时,它会成功运行一些图片,并在没有Python Traceback的情况下随机崩溃,显示"已停止工作"在Windows 8.1中。即使我使用相同的测试用例,该程序有时会在20多张图片后崩溃,有时只有2张,有时会运行到最后。

我甚至都不知道程序崩溃了。我该如何解决这个问题?

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

# -*- 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_())

1 个答案:

答案 0 :(得分:0)

作为ekhumoro的评论,这个问题是由于反复创建fileNameDefine_Widget对象造成的:

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

相反,应该创建一次此对象,并在每次需要时 init ,例如:

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)