Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何避免线程冻结循环_Python 3.x_Pyqt5 - Fatal编程技术网

Python 3.x 如何避免线程冻结循环

Python 3.x 如何避免线程冻结循环,python-3.x,pyqt5,Python 3.x,Pyqt5,我正在尝试创建GUI Api。首先,我在控制台中仅使用打印信息构建python脚本 因此,我想将应用程序重建为具有接口的应用程序。我决定使用PyQt5 像这样: 至(第一眼): 我在运行时遇到了循环问题。应用程序在运行时冻结 我准备了一个简短的脚本来模拟这个问题。主程序看起来不一样 import sys from PyQt5.QtWidgets import * from PyQt5 import QtWidgets from termcolor import colored import

我正在尝试创建GUI Api。首先,我在控制台中仅使用打印信息构建python脚本

因此,我想将应用程序重建为具有接口的应用程序。我决定使用PyQt5

像这样:

至(第一眼):

我在运行时遇到了循环问题。应用程序在运行时冻结

我准备了一个简短的脚本来模拟这个问题。主程序看起来不一样

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from termcolor import colored
import time

class App(QMainWindow):
   def __init__(self):
       super().__init__()
       self.title = 'API NORD'
       self.left = 0
       self.top = 0
       self.width = 300
       self.height = 200
       self.setWindowTitle(self.title)
       self.resize(800, 600)
       self.center()
       self.table_widget = MyTableWidget(self)
       self.setCentralWidget(self.table_widget)

       self.show()
    def center(self):
       # geometry of the main window
       qr = self.frameGeometry()

       # center point of screen
       cp = QDesktopWidget().availableGeometry().center()

       # move rectangle's center point to screen's center point
       qr.moveCenter(cp)

    # top left of rectangle becomes top left of window centering it
       self.move(qr.topLeft())


class MyTableWidget(QWidget):

   def __init__(self, parent):
       super(QWidget, self).__init__(parent)

       self.layout = QVBoxLayout(self)
       self.pushButton1 = QPushButton("Run")
       self.layout.addWidget(self.pushButton1)
       self.pushButton1.clicked.connect(self.button2_clicked)
       self.textedit = QtWidgets.QTextEdit(readOnly=True)
       self.layout.addWidget(self.textedit)
       self.textedit.setText("STATUS")



   def onClicked(self):
       radioButton = self.sender()
       if radioButton.isChecked():
           x=0
        # print("Shop is %s" % (radioButton.shop))
           self.Sklep=radioButton.shop
           self.l1.setText(self.Sklep)
       return


   def checkBulkStatus(self):
       Status = "Start"
       x=0
       self.textedit.setText("Start")
       while x < 5:

           print("Aktualny Status:", colored(Status,"yellow"))
           Status="Running"
           self.textedit.append(Status)
           if Status=="FAILED":
               print("Error")
               break
           time.sleep(2.5)
           x+=1

       print("Aktualny Status: ", colored("COMPLETED", "green"))
       self.textedit.setText("COMPLETED")


   def button2_clicked(self):
       self.checkBulkStatus()

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = App()
   sys.exit(app.exec_())

如何运行不同的线程以并行运行。可能吗?

您可以在线程中运行与UI无关的函数:

x = threading.Thread(target=self.checkBulkStatus)
x.start()
别忘了在某个时候加入

x.join()

作为可能的选项之一,使用
QTimer

QTimer
类提供重复和单次触发计时器
更多


您能在我的简短示例中演示如何实现这一点吗?
x.join()
import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtCore                                       # + QtCore
#from termcolor import colored
#import time


class MyTableWidget(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.pushButton1 = QPushButton("Run")
        self.layout.addWidget(self.pushButton1)
        self.pushButton1.clicked.connect(self.button2_clicked)
        self.textedit = QtWidgets.QTextEdit(readOnly=True)
        self.layout.addWidget(self.textedit)
        self.textedit.setText("STATUS")
        
        self.timer = QtCore.QTimer(self)                                  # +++
        self.timer.timeout.connect(self.checkBulkStatus)                  # +++
        
    def checkBulkStatus(self):
        _status = f"Running: x={self.x}"
        self.textedit.append(_status)
        if _status == "FAILED":
            self.textedit.append(_status + "FAILED")
            self.timer.stop()
            
        self.x += 1
        if self.x >= 5:
            self.timer.stop()
            self.textedit.append("COMPLETED")

    def button2_clicked(self):
        self._status = "Start"
        self.x = 0
        self.textedit.setText("Start")
        self.timer.start(2500)
        

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'API NORD'
        self.left = 0
        self.top = 0
        self.width = 300
        self.height = 200
        self.setWindowTitle(self.title)
        self.resize(800, 600)
        self.center()
        
        self.table_widget = MyTableWidget(self)
        self.setCentralWidget(self.table_widget)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        # move rectangle's center point to screen's center point
        qr.moveCenter(cp)
        # top left of rectangle becomes top left of window centering it
        self.move(qr.topLeft())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())