PyQt5——精简正则表达式测试小工具

标签: PyQt5  正则表达式  正则表达式测试小工具

PyQt5精简正则表达式测试小工具

希望读者可以关注一下本人的公众号“灵均兰草”,谢谢

https://pan.baidu.com/s/1e50u0YVswrqeB3PS_JqulA       网盘链接

首先我们打开QtDesigner创建一个Dialog窗口

如下图:

第1步:先调出三个布局,在Layouts中调出下面3个红框框(当然这布也可以省去,直接弄出下面的文档,及按钮),注意布局讲究顺序,把需要添加的按钮移动至布局中即可。

第2步:自由添加标签Label,双击即可修改标签名(方便调用)

添加两个文本编辑框Text Edit 以及一个行编辑框Line Edit

添加两个Spacers,以便隔开文本框按钮(美观)

第3步:打开信号槽按钮,如下图

添加信号槽函数,如下图点击拖动至某位置,就会弹出如下文本框,如想要点击的效果,就可以连接让clicked()连接槽函数,可以选择已经拥有的,也可以自己定义。这里自己用自己定义的函数,下面有个Edit点击,添加slot1()函数,接下来就可以自己定义啦!然后保存文件至pycharm下即可。

简单介绍一下下面的信号,还不懂就自己去问度娘啦!

QAbstractButton 提供的信号选择:

1、pressed(),当鼠标在button上并点击左键的时候 触发信号

2、released(),当鼠标左键被释放的时候触发信号

3、clicked(),当鼠标首次按下,然后释放,或者快捷键被释放的时候触发信号

4、toggled(),当button的标记状态发生改变的时候触发信号

第4步,如下图找到自己制作的ui文件直接生成PyUIC文件即可

第5步,找到槽函数,这里需要注意,改变变量使Dialog变为self  

self.retranslateUi(Dialog)
self.pushButton_2.clicked.connect(Dialog.slot2)
self.pushButtontextEdit.clicked.connect(Dialog.slot1)
self.pushButtontextEdit_2.clicked.connect(Dialog.slot3)
QtCore.QMetaObject.connectSlotsByName(Dialog)

即如下

self.pushButton_2.clicked.connect(self.slot2)

self.pushButtontextEdit.clicked.connect(self.slot1)

self.pushButtontextEdit_2.clicked.connect(self.slot3)

第6步,定义槽函数

让clear()函数连接清除textEdit文本框功能

def slot1(self):

    print('*slot1')

    self.textEdit.clear()

我们要使匹配结果输出在第三个文本框,就要使用以下输出方式,将lineEdit_2的文本内容输出赋给pattern。text()
将textEdit的文本内容输出赋给string。    用toPlainText()

利用正则表达式,re.findall()来传入表达式,文本内容,利用','.join()来分开字符串,再利用setText()来将得出的结果呈现在textEdit_2上

def slot2(self):

    print('* slot2  ')

    pattern = self.lineEdit_2.text()

    string = self.textEdit.toPlainText()

    textEdit_22 = re.findall(pattern, string)

    textEdit_222 = ','.join(textEdit_22)

    self.textEdit_2.setText(textEdit_222)

最后一步,添加主函数,将窗口运行显现出来

if __name__ == '__main__':

    app =QApplication(sys.argv)

    QQ  =QDialog()

    ui = Ui_Dialog()

    ui.setupUi(QQ)

    QQ.show()

    sys.exit(app.exec_())

全部代码如下:希望可以支持一下在下

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'hhh.ui'
# Created by: PyQt5 UI code generator 5.11.2
# WARNING! All changes made in this file will be lost!
# -*- coding: utf-8 -*-
import re
import sys
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(763, 519)
        self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 611, 161))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.textEdit = QtWidgets.QTextEdit(self.verticalLayoutWidget)
        self.textEdit.setObjectName("textEdit")
        self.verticalLayout.addWidget(self.textEdit)
        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Dialog)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(20, 190, 611, 41))
        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_3 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_2.addWidget(self.label_3)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.horizontalLayoutWidget_2)
        self.lineEdit_2.setEnabled(True)
        self.lineEdit_2.setMaxLength(32782)
        self.lineEdit_2.setFrame(True)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.horizontalLayout_2.addWidget(self.lineEdit_2)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem)
        self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_2.addWidget(self.pushButton_2)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem1)
        self.verticalLayoutWidget_2 = QtWidgets.QWidget(Dialog)
        self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(20, 250, 611, 251))
        self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        self.label_2.setObjectName("label_2")
        self.verticalLayout_2.addWidget(self.label_2)
        self.textEdit_2 = QtWidgets.QTextEdit(self.verticalLayoutWidget_2)
        self.textEdit_2.setObjectName("textEdit_2")
        self.verticalLayout_2.addWidget(self.textEdit_2)
        self.verticalSlider = QtWidgets.QSlider(Dialog)
        self.verticalSlider.setGeometry(QtCore.QRect(0, 500, 22, 160))
        self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
        self.verticalSlider.setObjectName("verticalSlider")
        self.pushButtontextEdit = QtWidgets.QPushButton(Dialog)
        self.pushButtontextEdit.setGeometry(QtCore.QRect(650, 100, 93, 28))
        self.pushButtontextEdit.setObjectName("pushButtontextEdit")
        self.pushButtontextEdit_2 = QtWidgets.QPushButton(Dialog)
        self.pushButtontextEdit_2.setGeometry(QtCore.QRect(650, 360, 93, 28))
        self.pushButtontextEdit_2.setObjectName("pushButtontextEdit_2")

        self.retranslateUi(Dialog)
        self.pushButton_2.clicked.connect(self.slot2)
        self.pushButtontextEdit.clicked.connect(self.slot1)
        self.pushButtontextEdit_2.clicked.connect(self.slot3)
        QtCore.QMetaObject.connectSlotsByName(Dialog)





    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "待匹配输入文本"))
        self.label_3.setText(_translate("Dialog", "正则表达式"))
        self.pushButton_2.setText(_translate("Dialog", "匹配测试"))
        self.label_2.setText(_translate("Dialog", "匹配结果:"))
        self.pushButtontextEdit.setText(_translate("Dialog", "clear"))
        self.pushButtontextEdit_2.setText(_translate("Dialog", "clear"))


    def slot2(self):
        print('* slot2  ')
        pattern = self.lineEdit_2.text()
        string = self.textEdit.toPlainText()
        textEdit_22 = re.findall(pattern, string)
        textEdit_222 = ','.join(textEdit_22)
        self.textEdit_2.setText(textEdit_222)

    def slot1(self):
        print('*slot1')
        self.textEdit.clear()

    def slot3(self):
        print('*slot3')
        self.textEdit_2.clear()


if __name__ == '__main__':
    app =QApplication(sys.argv)
    QQ  =QDialog()
    ui = Ui_Dialog()
    ui.setupUi(QQ)
    QQ.show()
    sys.exit(app.exec_())

 

https://pan.baidu.com/s/1e50u0YVswrqeB3PS_JqulA       网盘链接