簡體   English   中英

pyqt5 gui 在 while 循環中凍結

[英]pyqt5 gui freezing in while loop

我正在編寫 python gui 應用程序,它處理圖像並從串行端口發送圖像顏色並顯示結果,但不幸的是我的 gui 凍結了。 我嘗試使用 QApllication.processEvents,它可以工作,但我的程序速度很慢,速度對我來說非常重要,每一秒一次迭代都應該完成。 這是我的代碼:

        while self.knot <= knotter_width:

            color_to_send = []
            for i in range(1, number_of_knotter + 1):
                color_to_send.append(self.baft["knotters"][i][self.lay][self.knot])

            # send color numbers

            # receive color feedback

            self.knot = self.knot + 1

            if self.knot > knotter_width:
                break

            self.config_write()
            self.knotters_status() # gui update function
            QCoreApplication.processEvents()

和knotters_status function:

    def knotters_status(self):
        try:
            for i in range(number_of_knotter):
                self.knotters_status_dict[i].deleteLater()
        except:
            pass

        try:
            self.baft
        except:
            self.status.setAlignment(Qt.Alignment(132))
            status_gui_font_error = QFont("Arial", 43)
            status_gui_font_error.setBold(True)
            self.status.setFont(status_gui_font_error)
            self.status.setText("فایل بافت لود نشده است!")
            return

        self.knotters_status_dict = {}


        # size calculation:
        width_geo_start_point = 50
        width_step = int((self.status_width - 50)/number_of_knotter)
        self.each_knotters_sector_unit_height = int(self.status_height / 7)
        knotters_status_font_size = int(self.each_knotters_sector_unit_height / 1.5)
        knotters_status_font = QFont("Arial", knotters_status_font_size)
        knotters_status_font.setBold(True)


        for i in range(number_of_knotter):
            self.knotters_status_dict[i] = QLabel(self.status)
            self.knotters_status_dict[i].setGeometry((width_geo_start_point + width_step * (number_of_knotter - i - 1)),
                                                      0,
                                                      width_step,
                                                      self.status_height)
            self.knotters_status_dict[i].setStyleSheet("border: 1px solid black;"
                                                       "background-color: whitesmoke")
            self.knotters_status_dict[i].setAlignment(Qt.Alignment(36))
            self.knotters_status_dict[i].setFont(knotters_status_font)
            color_number = self.baft["knotters"][i + 1][self.lay][self.knot] + 1
            self.knotters_status_dict[i].setText("بافنده {}\nلای {}\nگره {}\nرنگ {}".format(i,
                                                                                           self.lay,
                                                                                           self.knot,
                                                                                           color_number))

            colors_width = width_step // 4
            y = self.each_knotters_sector_unit_height * 4 + self.each_knotters_sector_unit_height // 3
            colors = {}
            if self.lay != 1 and self.lay != self.baft["height"]:
                if self.knot != 1 and self.knot != knotter_width:
                    for j in range(2):
                        colors[j] = {}
                        x = colors_width // 4
                        for k in range(1,-2,-1):
                            target_lay = self.lay - j
                            target_knot = self.knot + k
                            colors[j][k] = QLabel(self.knotters_status_dict[i])
                            colors[j][k].setGeometry(x,
                                                     y,
                                                     colors_width,
                                                     self.each_knotters_sector_unit_height)
                            color_rgb = self.baft["color table"][self.baft["knotters"][i+1][target_lay][target_knot]+1]
                            colors[j][k].setStyleSheet("border: 1px solid black;"
                                                       "border-radius: 10px;"
                                                       "background-color: rgb({}, {}, {});".format \
                                                       (color_rgb[0],
                                                        color_rgb[1],
                                                        color_rgb[2]))
                            colors[j][k].show()
                            x = x + colors_width + colors_width // 4
                         y = y + int(self.each_knotters_sector_unit_height * 4 / 3)
                elif self.knot == 1:
                pass
                elif self.knot == knotter_width:
                    pass
            elif self.lay == 1:
                pass
            elif self.lay == self.baft["height"]:
                pass

            self.knotters_status_dict[i].show()

經過我的研究,我發現了這個但也沒有工作:

class Worker(QObject):

progress = pyqtSignal(int)
gui_update = pyqtSignal()
finish = pyqtSignal(bool)
ex = pyqtSignal()

@pyqtSlot(int, int, dict)
def run(self, knot, lay, baft):

    # send finish order

    # recieve finish feedback

    while knot <= knotter_width:
        color_to_send = []
        for i in range(1, number_of_knotter + 1):
            color_to_send.append(baft["knotters"][i][lay][knot])

        # send color numbers

        # receive color feedback

        if knot < knotter_width:
            knot = knot + 1
        else:
            break


        self.progress.emit(knot)
        self.gui_update.emit()

    self.finish.emit(True)

    # send finish order

    #reveive finish feedback

    self.ex.emit()

這設置了線程:

    self.thrd = QThread()
    self.worker = Worker()
    self.worker.moveToThread(self.thrd)
    self.thrd.started.connect(lambda: self.worker.run(self.knot, self.lay, self.baft))
    self.worker.progress.connect(self.progress)
    self.worker.gui_update.connect(self.knotters_status)
    self.worker.finish.connect(self.finished)
    self.worker.ex.connect(self.thrd.quit)
    self.worker.ex.connect(self.worker.deleteLater)
    self.thrd.finished.connect(self.thrd.deleteLater)

    self.thrd.start()

After somedays of research i found this solution: At first you have to write class and inherit from QThread class and then reimplement run method with signals and slots then after that you have create object from that class and just after that you should call processEvents()方法參考您的 QApplication object。 在這種情況下,它不會導致速度變慢,並且非常適合 mopre 細節,您可以參考我的另一個問題和答案,您可以單擊此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM