最近在学习pyqt,写了一个简单的验证正则的小工具,仅供学习交流使用
需要安装 PyQt5 :pip install PyQt==5.15.0
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '正则.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
import sys
import re
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.excel_label = QtWidgets.QLabel(self.centralwidget)
self.excel_label.setGeometry(QtCore.QRect(280, 20, 211, 20))
self.excel_label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.excel_label.setTextFormat(QtCore.Qt.AutoText)
self.excel_label.setObjectName("excel_label")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(190, 260, 351, 201))
self.textBrowser.setObjectName("textBrowser")
self.content_textEdit = QtWidgets.QLineEdit(self.centralwidget)
self.content_textEdit.setGeometry(QtCore.QRect(190, 100, 351, 78))
self.content_textEdit.setObjectName("content_textEdit")
self.start_pushButton = QtWidgets.QPushButton(self.centralwidget)
self.start_pushButton.setGeometry(QtCore.QRect(60, 340, 112, 32))
self.start_pushButton.setObjectName("start_pushButton")
self.re_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.re_lineEdit.setGeometry(QtCore.QRect(190, 220, 113, 21))
self.re_lineEdit.setObjectName("re_lineEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(80, 120, 58, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(80, 220, 81, 16))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.excel_label.setText(_translate("MainWindow", "python正则小工具"))
self.start_pushButton.setText(_translate("MainWindow", "开始匹配"))
self.label.setText(_translate("MainWindow", "文本"))
self.label_2.setText(_translate("MainWindow", "正则表达式"))
class MyMainForm(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyMainForm,self).__init__()
self.setupUi(self)
# 添加开始匹配的信号和槽,调用display函数
self.start_pushButton.clicked.connect(self.display)
def display(self):
text = self.content_textEdit.text()
re_ = self.re_lineEdit.text()
pattern = re.compile(fr"{re_}")
m = pattern.findall(text)
re_text = f"匹配结果:\n{m}"
self.textBrowser.setText(re_text)
if __name__ == "__main__":
# 固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
# 初始化
myWin = MyMainForm()
# 将窗口控件显示在屏幕上
myWin.show()
# 程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())
|