功能强大的 QT
[Python] 纯文本查看 复制代码 # -*- coding : utf-8 -*-
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QObject, QThread, Signal, Slot
import time
import sys
# 指定网址爬虫
from zg163 import access_space
class WindowMain:
clearTask = []
def __init__(self):
self.ui = QUiLoader().load('main.ui') # 解析ui文件 生成窗口对象
self.ui.StartpushButton.clicked.connect(self.StartpushButtonHandler) # 信号与槽连接
self.taskZG163 = connectZG163()
self.taskZG163.progressBarSignal.connect(self.updateProgressBar) # 信号与槽连接
self.taskZG163.infoSignal.connect(self.updateInformationTextBrowser)
self.taskZG163.errorSignal.connect(self.updateErrorTextBrowser)
self.taskZG163.passSignal.connect(self.updatePassTextBrowser)
self.clearTask.append(self.taskZG163)
# 当前时间
self.taskUpdateStatusTime = updateStatusTime()
self.clearTask.append(self.taskUpdateStatusTime)
self.taskUpdateStatusTime.strTimeSignal.connect(self.statusbarUpdate) # 信号与槽连接
self.taskUpdateStatusTime.start()
def statusbarUpdate(self, strTime):
self.ui.statusbar.showMessage(strTime)
def StartpushButtonHandler(self):
''' 任务开始 '''
self.taskZG163.start()
def updateProgressBar(self, number):
self.ui.progressBar.setValue(number)
def updateInformationTextBrowser(self, text):
self.ui.informationTextBrowser.append(text)
def updateErrorTextBrowser(self, text):
self.ui.errorTextBrowser.append(text)
def updatePassTextBrowser(self, text):
self.ui.passTextBrowser.append(text)
def closeEvent(self, event):
print(event)
class updateStatusTime(QThread):
# 信号
strTimeSignal = Signal(str)
def __init__(self):
super().__init__()
def run(self):
while True:
self.strTimeSignal.emit(time.strftime("%Y-%m-%d %H:%M:%S"))
time.sleep(0.5)
class connectZG163(QThread):
# 信号
progressBarSignal = Signal(int)
infoSignal = Signal(str)
errorSignal = Signal(str)
passSignal = Signal(str)
def __init__(self):
super().__init__()
def run(self):
task = access_space() # 获取网址信息
for info in task.taskRun():
self.progressBarSignal.emit(info[0])
flag = info[2]
# ("1: information", "2: error", "3, pass")
if flag == 1:
self.infoSignal.emit(info[1])
elif flag == 2:
self.errorSignal.emit(info[1])
else:
self.passSignal.emit(info[1])
# 开始运行
app = QApplication(sys.argv)
winMain = WindowMain()
winMain.ui.show()
app.exec_()
# 清理线程
for task in winMain.clearTask:
task.terminate() |