python - Python2 PyQt5 不响应循环
问题描述
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
这是程序外观的图像
我尝试了线程,但它不是我想让我的程序我的程序是关于从 pyqt5 中的 Textarea 进行电子邮件过滤的方式,所以我输入了这么多邮件,大约 5000 封邮件到 10000 封邮件来过滤它们,所以线程无法使用所以我该怎么办?
解决方案
您必须使用线程并通过线程更新 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
推荐阅读
- php - 如何通过更改通过引用传递的参数来对调用具有副作用的函数的方法进行单元测试?
- php - 使用日期助手指定月份或 Laravel/Eloquent 问题?
- node.js - CouchDB 整数大小
- javascript - 单击最近的输入时一次仅显示一个跨度
- c# - 我如何也可以使用 httpclient 创建一个功能,即使在应用程序关闭完成后也能保持用户登录?
- r - 数据表 R - 聚合
- admin - 为新开发人员推荐的博客主机是什么?
- c++ - 安全 SSL 连接错误:未知协议 | 错误的主机名查找
- css - Internet Explorer 上的 Angular 6 -- Angular Material/Flex 布局不会居中?
- javascript - 如果字符串中包含特定单词,则删除该单词