WIN7 + Python 3.6.8 + VS Code 1.14.1 + Qt Designer 5.13
Ui_main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'e:\PY\zh\main.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainForm(object):
def setupUi(self, MainForm):
MainForm.setObjectName("MainForm")
MainForm.resize(400, 320)
MainForm.setFixedSize(400, 320)
MainForm.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainForm.sizePolicy().hasHeightForWidth())
MainForm.setSizePolicy(sizePolicy)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("e:\\PY\\zh\\icon-windowed.ico"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainForm.setWindowIcon(icon)
self.groupBox = QtWidgets.QGroupBox(MainForm)
self.groupBox.setGeometry(QtCore.QRect(10, 10, 380, 111))
self.groupBox.setObjectName("groupBox")
self.txt_ml = QtWidgets.QLineEdit(self.groupBox)
self.txt_ml.setEnabled(True)
self.txt_ml.setReadOnly(True)
self.txt_ml.setGeometry(QtCore.QRect(12, 20, 281, 21))
self.txt_ml.setObjectName("txt_ml")
self.btn_ml = QtWidgets.QPushButton(self.groupBox)
self.btn_ml.setGeometry(QtCore.QRect(300, 20, 70, 21))
self.btn_ml.setObjectName("btn_ml")
self.label_2 = QtWidgets.QLabel(self.groupBox)
self.label_2.setGeometry(QtCore.QRect(220, 50, 71, 21))
self.label_2.setObjectName("label_2")
self.txt_qz = QtWidgets.QLineEdit(self.groupBox)
self.txt_qz.setGeometry(QtCore.QRect(50, 50, 151, 21))
self.txt_qz.setObjectName("txt_qz")
self.rtn_bt = QtWidgets.QRadioButton(self.groupBox)
self.rtn_bt.setGeometry(QtCore.QRect(300, 80, 70, 16))
self.rtn_bt.setObjectName("rtn_bt")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(10, 50, 35, 21))
self.label.setObjectName("label")
self.rtn_xt = QtWidgets.QRadioButton(self.groupBox)
self.rtn_xt.setGeometry(QtCore.QRect(220, 80, 70, 16))
self.rtn_xt.setObjectName("rtn_xt")
self.txt_xh = QtWidgets.QLineEdit(self.groupBox)
self.txt_xh.setGeometry(QtCore.QRect(300, 50, 70, 21))
self.txt_xh.setObjectName("txt_xh")
self.txt_qh = QtWidgets.QLineEdit(self.groupBox)
self.txt_qh.setGeometry(QtCore.QRect(50, 80, 50, 21))
self.txt_qh.setObjectName("txt_qh")
self.label_3 = QtWidgets.QLabel(self.groupBox)
self.label_3.setGeometry(QtCore.QRect(10, 80, 35, 21))
self.label_3.setObjectName("label_3")
self.txt_zh = QtWidgets.QLineEdit(self.groupBox)
self.txt_zh.setGeometry(QtCore.QRect(150, 80, 50, 21))
self.txt_zh.setObjectName("txt_zh")
self.label_4 = QtWidgets.QLabel(self.groupBox)
self.label_4.setGeometry(QtCore.QRect(110, 80, 35, 21))
self.label_4.setObjectName("label_4")
self.btn_ks = QtWidgets.QPushButton(MainForm)
self.btn_ks.setGeometry(QtCore.QRect(160, 129, 75, 21))
self.btn_ks.setObjectName("btn_ks")
self.groupBox_2 = QtWidgets.QGroupBox(MainForm)
self.groupBox_2.setGeometry(QtCore.QRect(10, 150, 380, 171))
self.groupBox_2.setObjectName("groupBox_2")
self.pla_out = QtWidgets.QPlainTextEdit(self.groupBox_2)
self.pla_out.setGeometry(QtCore.QRect(10, 20, 360, 141))
self.pla_out.setReadOnly(True)
self.pla_out.setObjectName("pla_out")
self.retranslateUi(MainForm)
QtCore.QMetaObject.connectSlotsByName(MainForm)
MainForm.setTabOrder(self.txt_ml, self.btn_ml)
MainForm.setTabOrder(self.btn_ml, self.txt_qz)
MainForm.setTabOrder(self.txt_qz, self.txt_xh)
MainForm.setTabOrder(self.txt_xh, self.txt_qh)
MainForm.setTabOrder(self.txt_qh, self.txt_zh)
MainForm.setTabOrder(self.txt_zh, self.rtn_xt)
MainForm.setTabOrder(self.rtn_xt, self.rtn_bt)
MainForm.setTabOrder(self.rtn_bt, self.btn_ks)
def retranslateUi(self, MainForm):
_translate = QtCore.QCoreApplication.translate
MainForm.setWindowTitle(_translate("MainForm", "综合归档"))
self.groupBox.setTitle(_translate("MainForm", "归档目录:"))
self.btn_ml.setText(_translate("MainForm", "浏览"))
self.label_2.setText(_translate("MainForm", "顺序号位数:"))
self.rtn_bt.setText(_translate("MainForm", "前缀不同"))
self.label.setText(_translate("MainForm", "前缀:"))
self.rtn_xt.setText(_translate("MainForm", "前缀相同"))
self.label_3.setText(_translate("MainForm", "起号:"))
self.label_4.setText(_translate("MainForm", "止号:"))
self.btn_ks.setText(_translate("MainForm", "开始检查"))
self.groupBox_2.setTitle(_translate("MainForm", "信息:"))
main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog, QApplication
# from PyQt5.QtGui import *
# from PyQt5.QtCore import *
from Ui_main import Ui_MainForm
class MainWindow(QtWidgets.QMainWindow, Ui_MainForm):
# 创建类,分离UI与功能
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
# 添加按钮事件
self.btn_ml.clicked.connect(self.clk_btn_ml)
self.btn_ks.clicked.connect(self.clk_btn_ks)
def clk_btn_ml(self):
# 浏览按钮事件,打开选择文件夹窗口,并将选择的文件夹赋值给文本框
dirpath = QFileDialog.getExistingDirectory(None, "选择文件夹", "/")
self.txt_ml.setText(dirpath.replace("/", "\\"))
def listDir(self, rootDir):
# 获取当前目录rootDir下的目录名,保存在列表lstDir中,返回lstDir
lstDir = []
for pathname in os.listdir(rootDir):
if os.path.isdir(os.path.join(rootDir, pathname)):
lstDir.append(pathname)
return lstDir
def clk_btn_ks(self):
# 开始检查按钮事件
# 清空信息内容
self.pla_out.setPlainText("")
# 缺档计数器
qd = 0
try:
# 设置异常处理,检查录入信息并给出提示
ml = str(self.txt_ml.text()) \
if self.txt_ml.text() != "" \
else self.pla_out.appendPlainText("请选择路径!")
qz = str(self.txt_qz.text()) \
if self.txt_qz.text() != "" \
else self.pla_out.appendPlainText("请输入文档前缀!")
xh = int(self.txt_xh.text()) \
if self.txt_xh.text() != "" \
else self.pla_out.appendPlainText("请输入顺序号位数!")
qh = int(self.txt_qh.text()) \
if self.txt_qh.text() != "" \
else self.pla_out.appendPlainText("请输入起号!")
zh = int(self.txt_zh.text()) \
if self.txt_zh.text() != "" \
else self.pla_out.appendPlainText("请输入止号!")
if self.rtn_xt.isChecked():
# 前缀相同
# docname:文档名称,前缀+顺序号
docname = qz + "{:0>" + str(xh) + "d}"
while qh <= zh:
if docname.format(qh) not in self.listDir(ml):
self.pla_out.appendPlainText(
"{}".format(docname.format(qh)))
qd += 1
qh += 1
# 每次循环刷新页面
QApplication.processEvents()
# 统计检查结果
self.pla_out.setPlainText("") & self.pla_out.appendPlainText(
"请检查输入项并重新检查!") \
if qd == zh else \
self.pla_out.appendPlainText(
"---------- 共计:{} ----------".format(qd))
elif self.rtn_bt.isChecked():
# 前缀不同
# docname:文档名称,只取顺序号
docname = "{:0>" + str(xh) + "d}"
# 历遍目录列表listDir,将列表中的字符截取后x位赋值给新列表new_list
new_list = []
for list_id in self.listDir(ml):
new_list.append(list_id[-xh:])
while qh <= zh:
if docname.format(qh) not in new_list:
self.pla_out.appendPlainText(
"{}".format(docname.format(qh)))
qd += 1
qh += 1
# 每次循环刷新页面
QApplication.processEvents()
# 统计检查结果
self.pla_out.setPlainText("") & self.pla_out.appendPlainText(
"请检查输入项并重新检查!") \
if qd == zh else \
self.pla_out.appendPlainText(
"---------- 共计:{} ----------".format(qd))
# 异常提示
except:
self.pla_out.appendPlainText("顺序号位数、起止号必须为数字!")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())
测试运行:
错误提示:
gonery 发布了4 篇原创文章 · 获赞 0 · 访问量 5949 私信 关注