part 1
初学pyqt,折腾了好久才把功能实现,功能是按开始按钮循环输出数字在textBrowser控件中,按结束按钮停止输出,再按开始按钮重新开始循环
part 2
技术点与其中遇到的坑
1、遇到的坑:一开始想实现按结束按钮停止循环,结果按了开始就一直结束不了,开始是在Ui_Dialog里写了个函数弄了个标志位想要结束,结果那个标志位根本作用不了开始循环的标志位,所以想到用线程来实现,就是界面这个主线程开始一个循环的子线程,按停止按钮结束循环的子线程。百度了好多,用threading和QThread都可以实现,重点是要用信号槽机制才能将标识位的值改变。
2、将thread中的输出显示在界面主线程中的textBrowser中
Ui_Dialog相关代码
self.myThread1.strsin.connect(self.updateText)
def updateText(self,text):
self.textBrowser.append(text)
myThread相关代码
strsin = pyqtSignal(str)
self.strsin.emit(str(i))
3、通过信号槽机制使线程停止
Ui_Dialog相关代码
sin = pyqtSignal()
self.myThread1 = myThread()
self.sin.connect(self.myThread1.stop)
myThread相关代码
def stop(self):
self.flag = 0
4、点击按钮传参
实现将按钮的text传入函数
self.startButton.clicked.connect(lambda:self.startOrstop(self.startButton.text()))
part 3
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'myWindow.ui'
#
# Created by: PyQt5 UI code generator 5.13.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QThread,pyqtSignal,QObject
from PyQt5.QtWidgets import QApplication,QMainWindow
import time,sys
class Ui_Dialog(QObject):
sin = pyqtSignal()
def __init__(self):
super(Ui_Dialog,self).__init__()
self.myThread1 = myThread()
self.sin.connect(self.myThread1.stop)
self.myThread1.strsin.connect(self.updateText)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(593, 415)
self.startButton = QtWidgets.QPushButton(Dialog)
self.startButton.setGeometry(QtCore.QRect(410, 80, 75, 23))
self.startButton.setObjectName("pushButton")
self.stopButton = QtWidgets.QPushButton(Dialog)
self.stopButton.setGeometry(QtCore.QRect(410, 140, 75, 23))
self.stopButton.setObjectName("pushButton_2")
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(50, 120, 256, 192))
self.textBrowser.setObjectName("textBrowser")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.update_tag = 1
self.startButton.clicked.connect(lambda:self.startOrstop(self.startButton.text()))
self.stopButton.clicked.connect(lambda:self.startOrstop(self.stopButton.text()))
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.startButton.setText(_translate("Dialog", "start"))
self.stopButton.setText(_translate("Dialog", "stop"))
def startOrstop(self,text):
try:
print(text)
myThread1 = myThread()
self.sin.connect(myThread1.stop)
if text =='start':
self.update_tag = 0
self.textBrowser.clear()
self.myThread1.flag = 1
self.myThread1.start()
elif text == 'stop':
self.update_tag = 1
self.sin.emit()
except Exception as e:
print(e)
def updateText(self,text):
self.textBrowser.append(text)
class myThread(QThread):
strsin = pyqtSignal(str)
def __init__(self,parent=None):
super(myThread,self).__init__(parent)
self.flag = 1
def run(self):
try:
for i in range(100):
if self.flag == 1:
self.strsin.emit(str(i))
time.sleep(0.1)
QtWidgets.QApplication.processEvents()
elif self.flag == 0:
self.strsin.emit("stop thread")
break
self.strsin.emit("please restart")
except Exception as e:
print(e)
def stop(self):
self.flag = 0
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui_dialog = Ui_Dialog()
ui_dialog.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
part 4
功能展示
用pyqt实现的小工具,可以实现zip和rar压缩文件的密码爆破,
欢迎来的我github查看:https://github.com/lynngeo/zi...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。