Python2 PyQt5没有响应循环

问题描述 投票:0回答:1
import sys
import os
from lxml import html
import re
import time
from functions import * # its other python file
from PyQt5 import QtWidgets
from PyQt5.QtCore import *


def check(mail, live_count=0, die_count=0, couldnt_count=0, timeout=60):

    liveco, dieco, couldntco = checkMail(mail, live_count, die_count, couldnt_count, timeout)

    return liveco, dieco, couldntco

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window,self).__init__()
        self.mainWindow()

    def mainWindow(self):
        global textarea
        global gate
        global label

        self.setWindowTitle("EXE CHECKER")
        self.setGeometry(100, 100, 600, 400)
        self.setMaximumSize(QSize(600, 400))

        gate = QtWidgets.QComboBox()
        gate.addItem("MAIL")

        textarea = QtWidgets.QTextEdit(self)
        textarea.setPlaceholderText("Enter Mails to Check")

        button = QtWidgets.QPushButton("Start", self)
        button.clicked.connect(self.OnClick)

        label = QtWidgets.QLabel(self)
        label.setAlignment(Qt.AlignCenter)
        label.setText("Result")

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(gate)
        layout.addWidget(textarea)
        layout.addWidget(button)
        layout.addWidget(label)
        self.setLayout(layout)
        self.show()

    def OnClick(self):
        listmail = textarea.toPlainText()
        listmail = listmail.split("\n")
        gate2 = str(gate.currentText())

        now = 1
        live_count = 0
        die_count = 0
        couldnt_count = 0
        length = len(listmail)

        for mail in listmail:
            txt = "%s/%s | Live: %s - Die: %s - Couldn't Check: %s" % (now,length,live_count,die_count,couldnt_count)
            label.setText(txt)

            try:
                live_count, die_count, couldnt_count = check(mail, live_count, die_count, couldnt_count, 60)
            except:
                try:
                    live_count, die_count, couldnt_count = check(mail, live_count, die_count, couldnt_count, 120)
                except:
                    continue

            txt = "%s/%s | Live: %s - Die: %s - Couldn't Check: %s" % (now,length,live_count,die_count,couldnt_count)
            label.setText(txt)
            now += 1
        print("\nChecking has been finished...")

try:
    pycCleaner()
    app = QtWidgets.QApplication(sys.argv)
    app_win = Window()
    sys.exit(app.exec_())
except:
    pass

这是程序外观的图像

我试过线程,但它不是我想让我的程序我的程序是关于从pytat5中的Textarea电子邮件过滤所以我输入这么多邮件约5000邮件到10000邮件过滤它们所以线程不工作所以我该怎么办?

python python-2.7 pyqt pyqt5 python-multithreading
1个回答
0
投票

你必须使用一个线程并通过一个线程更新GUI,如下所示:

import sys
import os
from lxml import html
import re
import time
from functions import * # its other python file
from PyQt5 import QtCore, QtWidgets


def check(mail, live_count=0, die_count=0, couldnt_count=0, timeout=60):
    liveco, dieco, couldntco = checkMail(mail, live_count, die_count, couldnt_count, timeout)

    return liveco, dieco, couldntco

class MailThread(QtCore.QThread):
    textChanged = QtCore.pyqtSignal(str)

    def run(self):
        live_count, die_count, couldnt_count, length = self.args

        for i, mail in enumerate(self.listmail):
            now = i+1
            txt = "%s/%s | Live: %s - Die: %s - Couldn't Check: %s" % (now,length,live_count,die_count,couldnt_count)
            self.textChanged.emit(txt)

            try:
                live_count, die_count, couldnt_count = check(mail, live_count, die_count, couldnt_count, 60)
            except:
                try:
                    live_count, die_count, couldnt_count = check(mail, live_count, die_count, couldnt_count, 120)
                except:
                    continue
            txt = "%s/%s | Live: %s - Die: %s - Couldn't Check: %s" % (now,length,live_count,die_count,couldnt_count)
            self.textChanged.emit(txt)
        print("\nChecking has been finished...")
        self.quit()
        self.wait()


class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window,self).__init__()
        self.mainWindow()

    def mainWindow(self):
        self.setWindowTitle("EXE CHECKER")
        self.setGeometry(100, 100, 600, 400)
        self.setMaximumSize(QtCore.QSize(600, 400))

        self.gate = QtWidgets.QComboBox()
        self.gate.addItem("MAIL")

        self.textarea = QtWidgets.QTextEdit()
        self.textarea.setPlaceholderText("Enter Mails to Check")

        button = QtWidgets.QPushButton("Start")
        button.clicked.connect(self.OnClick)

        self.label = QtWidgets.QLabel()
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setText("Result")

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.gate)
        layout.addWidget(self.textarea)
        layout.addWidget(button)
        layout.addWidget(self.label)

        self.show()

    def OnClick(self):
        listmail = self.textarea.toPlainText().split("\n")
        gate2 = str(self.gate.currentText())

        live_count = 0
        die_count = 0
        couldnt_count = 0
        length = len(listmail)

        thread = MailThread(self)
        thread.args = live_count, die_count, couldnt_count, length
        thread.listmail = listmail
        thread.textChanged.connect(self.label.setText)
        thread.start()


if __name__ == '__main__':
    try:
        pycCleaner()

        app = QtWidgets.QApplication(sys.argv)
        app_win = Window()
        sys.exit(app.exec_())
    except:
        pass
© www.soinside.com 2019 - 2024. All rights reserved.