其实使用pangu做文本格式标准化的业务代码在之前就实现了,主要能够将中文文本文档中的文字、标点符号等进行标准化。
但是为了方便起来我们这里使用了Qt5将其做成了一个可以操作的页面应用,这样不熟悉python的朋友就可以不用写代码直接双击运行使用就OK了。
为了使文本格式的美化过程不影响主线程的使用,特地采用QThread子线程来专门的运行文本文档美化的业务过程,接下来还是采用pip的方式将所有需要的非标准模块安装一下。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pangu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5
将我们使用到的pyqt5应用制作模块以及业务模块pangu导入到我们的代码块中。
# It imports all the classes, attributes, and methods of the PyQt5.QtCore module into the global symbol table.
from PyQt5.QtCore import *
# It imports all the classes, attributes, and methods of the PyQt5.QtWidgets module into the global symbol table.
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QTextBrowser, QLineEdit, QPushButton, \
QFormLayout, QFileDialog
# It imports all the classes, attributes, and methods of the PyQt5.QtGui module into the global symbol table.
from PyQt5.QtGui import QIcon, QFont, QTextCursor
# It imports the pangu module.
import pangu
# It imports the sys module.
import sys
# It imports the os module.
import os
为了减少python模块在打包时资源占用过多,打的exe应用程序的占用空间过大的情况,这次我们只导入了能够使用到的相关python类,这个小细节大家注意一下。
下面创建一个名称为PanGuUI的python类来实现对整个应用页面的开发,将页面的布局以及组件相关的部分写到这个类中。并且给页面组件绑定好相应的槽函数从而实现页面的'点击'等功能。
# It creates a class called PanGuUI that inherits from QWidget.
class PanGuUI(QWidget):
def __init__(self):
"""
A constructor. It is called when an object is created from a class and it allows the class to initialize the
attributes of a class.
"""
super(PanGuUI, self).__init__()
self.init_ui()
def init_ui(self):
"""
This function initializes the UI.
"""
self.setWindowTitle('文本文档美化器 公众号:Python 集中营')
self.setWindowIcon(QIcon('txt.ico'))
self.brower = QTextBrowser()
self.brower.setFont(QFont('宋体', 8))
self.brower.setReadOnly(True)
self.brower.setPlaceholderText('处理进程展示区域...')
self.brower.ensureCursorVisible()
self.txt_file_path = QLineEdit()
self.txt_file_path.setPlaceholderText('源文本文档路径')
self.txt_file_path.setReadOnly(True)
self.txt_file_path_btn = QPushButton()
self.txt_file_path_btn.setText('导入')
self.txt_file_path_btn.clicked.connect(self.txt_file_path_btn_click)
self.new_txt_file_path = QLineEdit()
self.new_txt_file_path.setPlaceholderText('新文本文档路径')
self.new_txt_file_path.setReadOnly(True)
self.new_txt_file_path_btn = QPushButton()
self.new_txt_file_path_btn.setText('路径')
self.new_txt_file_path_btn.clicked.connect(self.new_txt_file_path_btn_click)
self.start_btn = QPushButton()
self.start_btn.setText('开始导入')
self.start_btn.clicked.connect(self.start_btn_click)
hbox = QHBoxLayout()
hbox.addWidget(self.brower)
fbox = QFormLayout()
fbox.addRow(self.txt_file_path, self.txt_file_path_btn)
fbox.addRow(self.new_txt_file_path, self.new_txt_file_path_btn)
v_vbox = QVBoxLayout()
v_vbox.addWidget(self.start_btn)
vbox = QVBoxLayout()
vbox.addLayout(fbox)
vbox.addLayout(v_vbox)
hbox.addLayout(vbox)
self.thread_ = PanGuThread(self)
self.thread_.message.connect(self.show_message)
self.thread_.finished.connect(self.finshed)
self.setLayout(hbox)
def show_message(self, text):
"""
It shows a message
:param text: The text to b