Python 更新PyQt5中的QPixmap使应用程序崩溃

Python 更新PyQt5中的QPixmap使应用程序崩溃,python,pyqt,pyqt5,qpixmap,Python,Pyqt,Pyqt5,Qpixmap,在下面的代码中,当我使用函数get_core_data加载GridscanCanvas的pixmap时,它工作得非常好,没有崩溃,然后图像显示在我的应用程序中 但是,当我想更新函数get_square_data中的同一个pixmap时,它每次都会崩溃 这两次,原始图像都是np.array,它们之间的唯一区别是数组的大小。我已经对崩溃的代码进行了评论 我很迷茫,因为两个函数几乎相同。任何帮助都将不胜感激 from PyQt5.QtGui import * from PyQt5.QtWidgets

在下面的代码中,当我使用函数get_core_data加载GridscanCanvas的pixmap时,它工作得非常好,没有崩溃,然后图像显示在我的应用程序中

但是,当我想更新函数get_square_data中的同一个pixmap时,它每次都会崩溃

这两次,原始图像都是np.array,它们之间的唯一区别是数组的大小。我已经对崩溃的代码进行了评论

我很迷茫,因为两个函数几乎相同。任何帮助都将不胜感激

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

import os
import numpy as np
from mdoc_class import Gridscan_mdoc
from mrc_class import Gridscan_mrc
from em_ss_find_gridsquares import find_gridsquares



class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):

        super(MainWindow, self).__init__(*args, **kwargs)
        self.setGeometry(100,100,1000,1000)
        self.main_widget = QWidget(self)
        self.setCentralWidget(self.main_widget)
        self.gridscan_canvas = GridscanCanvas(self)
        self.init_menu()

        self.pixel_size = 92.7
        self.gridsquare_length = 85000
        self.spacing_length = 40000

        self.show()


    def init_menu(self):
        bar = self.menuBar()
        self.file = bar.addMenu('File')
        load_mdoc_action = QAction('Load mdoc', self)
        self.file.addAction(load_mdoc_action)
        self.file.triggered.connect(self.file_respond)
        find_squares_action = QAction('Find squares', self)
        self.file.addAction(find_squares_action)



    def file_respond(self, q):
        signal = q.text()
        if signal == 'Load mdoc':
            mdoc_file = QFileDialog.getOpenFileName(self, 'Open File', os.getenv('Home'),
                                                    "MDOC(*.mdoc);;AllFiles(*.*)")[0]
            if mdoc_file:
                try:
                    self.load_core_data = LoadCoreData(mdoc_file)
                except Exception as e:
                    print(e)
                    return
                try:
                    self.load_core_data.new_output.connect(self.get_core_data)
                except Exception as e:
                    print(e)
                    return
                try:
                    self.load_core_data.start()
                except Exception as e:
                    print(e)
                    return

        elif signal == 'Find squares':
            try:
                self.find_squares = FindGridsquares(self.mrc.gridscan, self.pixel_size, self.gridsquare_length,
                                                    self.spacing_length)
            except Exception as e:
                print(e)
                return
            try:
                self.find_squares.new_output.connect(self.get_square_data)
            except Exception as e:
                print(e)
                return
            try:
                self.find_squares.start()
            except Exception as e:
                print(e)
                return
        else:
            return


    def get_core_data(self, core_data):
        self.mrc = core_data['mrc']
        self.mdoc = core_data['mdoc']

        # This here works fine
        self.gridscan_canvas.pixmap = QPixmap(QImage(self.mrc.gridscan, self.mrc.gridscan.shape[1],
                                                     self.mrc.gridscan.shape[0], QImage.Format_Grayscale8))
        self.gridscan_canvas.update()

    def get_square_data(self, square_data):
        self.centres = square_data['centres']
        self.aligned_centres = square_data['aligned_centres']
        self.aligned_gridscan = square_data['aligned_gridscan']
        self.rot_angle = square_data['rot_angle']
        self.sqrd_centres = square_data['sqrd_centres']

        # This here crashes is where it crashes
        self.gridscan_canvas.pixmap = QPixmap(QImage(self.aligned_gridscan, self.aligned_gridscan.shape[1],
                                                     self.aligned_gridscan.shape[0], QImage.Format_Grayscale8))
        self.gridscan_canvas.update()


class GridscanCanvas(QWidget):
    DELTA  = 10
    def __init__(self,  *args, **kwargs):
        super(GridscanCanvas, self).__init__(*args, **kwargs)
        self.draggin_idx = -1
        self.points_old = np.array([[v * 5, v * 5] for v in range(75)], dtype=np.float)
        self.width = 700
        self.height = 700
        self.setFixedSize(self.width, self.height)
        self.pixmap = None

        self.points = None
        self.scaling_factor = [1, 1]

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        if self.pixmap is not None:
            self.pixmap = self.pixmap.scaled(self.width, self.height, transformMode=Qt.SmoothTransformation)
            qp.drawPixmap(0, 0, self.width, self.height, self.pixmap)
        else:
            self.scaling_factor = [1, 1]
        if self.points is not None:
            self.drawPoints(qp)
        qp.end()

    def drawPoints(self, qp):
        pen = QPen()
        pen.setWidth(5)
        pen.setColor(Qt.red)
        qp.setPen(pen)

        for x,y in self.points:
            qp.drawPoint(x,y)


class LoadCoreData(QThread):
    new_output = pyqtSignal(object)

    def __init__(self, mdoc_filepath):
        QThread.__init__(self)
        self.isRunning = True
        self.mdoc_filepath = mdoc_filepath

    def __del__(self):
        self.wait()

    def run(self):
        mdoc = Gridscan_mdoc(self.mdoc_filepath)
        mrc = Gridscan_mrc(mdoc)
        output = {'mdoc': mdoc, 'mrc': mrc}
        self.new_output.emit(output)


class FindGridsquares(QThread):
    new_output = pyqtSignal(object)

    def __init__(self, gridscan, pixel_size, gridsquare_length, spacing_length):
        QThread.__init__(self)
        self.isRunning = True
        self.gridscan = gridscan
        self.pixel_size = pixel_size
        self.gridsquare_length = gridsquare_length
        self.spacing_length = spacing_length

    def __del__(self):
        self.wait()

    def run(self):
        centres, aligned_centres, aligned_gridscan, rot_angle, sqrd_centres =find_gridsquares(self.gridscan, self.pixel_size, self.gridsquare_length, self.spacing_length)
        output = {'centres': centres, 'aligned_centres': aligned_centres, 'aligned_gridscan': aligned_gridscan, 'rot_angle': rot_angle, 'sqrd_centres': sqrd_centres}
        self.new_output.emit(output)



app = QApplication([])
window = MainWindow()
app.exec_()

什么是
em\u ss\u find\u gridsquares
?它属于哪个库?em\u ss\u find\u gridsquares是我自己的函数。它工作正常,并按预期返回文件。当我试图第二次更新pixmap时,唯一的问题出现了。两次图像的格式相同,唯一的例外是图像的大小(大小差异约为1%)mmm,该功能相关吗?如果没有,则从您显示的代码中删除它,请提供一个,如果我们无法重现您的问题,我们将无法帮助您是的,这是一个非常重要的功能。我将尝试以更简单的方式重新创建问题!如果它是重要的,那么你必须在你的问题中表现出来,你读过a的意思吗?