快速开发字符检索器

最近发现市面上的搜索器都只能检索文件名或文件夹名,做不到遍历每个文件中的字符。而自己以前写的search.py每次都依托于python环境才能跑。于是打算用pyqt开发个exe来用。

一、设计

首先用qtdesigner设计页面:
在这里插入图片描述

二、写代码

import os
import pyperclip
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(843, 429)
        self.inputLE1 = QtWidgets.QLineEdit(Form)
        self.inputLE1.setGeometry(QtCore.QRect(110, 20, 501, 51))
        self.inputLE1.setObjectName("inputLE1")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(10, 20, 111, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.copy1 = QtWidgets.QPushButton(Form)
        self.copy1.setGeometry(QtCore.QRect(620, 20, 91, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.copy1.setFont(font)
        self.copy1.setObjectName("copy1")
        self.paste1 = QtWidgets.QPushButton(Form)
        self.paste1.setGeometry(QtCore.QRect(720, 20, 91, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.paste1.setFont(font)
        self.paste1.setObjectName("paste1")
        self.paste2 = QtWidgets.QPushButton(Form)
        self.paste2.setGeometry(QtCore.QRect(720, 90, 91, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.paste2.setFont(font)
        self.paste2.setObjectName("paste2")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(10, 90, 91, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.copy2 = QtWidgets.QPushButton(Form)
        self.copy2.setGeometry(QtCore.QRect(620, 90, 91, 41))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.copy2.setFont(font)
        self.copy2.setObjectName("copy2")
        self.inputLE2 = QtWidgets.QLineEdit(Form)
        self.inputLE2.setGeometry(QtCore.QRect(110, 90, 501, 51))
        self.inputLE2.setObjectName("inputLE2")
        self.copy3 = QtWidgets.QPushButton(Form)
        self.copy3.setGeometry(QtCore.QRect(620, 310, 91, 81))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.copy3.setFont(font)
        self.copy3.setObjectName("copy3")
        self.paste3 = QtWidgets.QPushButton(Form)
        self.paste3.setGeometry(QtCore.QRect(720, 310, 91, 81))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.paste3.setFont(font)
        self.paste3.setObjectName("paste3")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(10, 240, 101, 61))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.goBTN = QtWidgets.QPushButton(Form)
        self.goBTN.setGeometry(QtCore.QRect(640, 180, 151, 91))
        font = QtGui.QFont()
        font.setPointSize(25)
        font.setBold(True)
        font.setWeight(75)
        self.goBTN.setFont(font)
        self.goBTN.setObjectName("goBTN")
        self.showTE = QtWidgets.QTextEdit(Form)
        self.showTE.setGeometry(QtCore.QRect(110, 180, 501, 211))
        self.showTE.setObjectName("showTE")
        self.showLEN = QtWidgets.QLabel(Form)
        self.showLEN.setGeometry(QtCore.QRect(110, 150, 691, 21))
        self.showLEN.setObjectName("showLEN")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

        self.copy1.clicked.connect(lambda: pyperclip.copy(self.inputLE1.text()))
        self.copy2.clicked.connect(lambda: pyperclip.copy(self.inputLE2.text()))
        self.copy3.clicked.connect(lambda: pyperclip.copy(self.showTE.toPlainText()))
        self.paste1.clicked.connect(lambda: self.inputLE1.setText(pyperclip.paste()))
        self.paste2.clicked.connect(lambda: self.inputLE2.setText(pyperclip.paste()))
        self.paste3.clicked.connect(lambda: self.showTE.setPlainText(pyperclip.paste()))

        self.goBTN.clicked.connect(self.go)

    def showResult(self):
        self.result = list(set(self.result))
        result = ""
        for l in self.result:
            result = result + l + "\n"
        return result

    def go(self):
        self.result = []
        i1 = self.inputLE1.text()
        i2 = self.inputLE2.text().strip()
        self.showLEN.setText("开始搜索")
        if not os.path.isdir(i1):
            self.showTE.setPlainText("非法路径")
        elif not i2:
            self.showTE.setPlainText("空白有啥好搜的")
        else:
            self.search(i1, i2)
            self.showLEN.setText("搜索完毕,共搜索到 {} 个结果".format(len(self.result)))

    def search(self, path, string):
        for f in os.listdir(path):
            f = os.path.join(path, f)
            if os.path.isfile(f):
                try:
                    with open(f, "r", encoding="utf-8")as fp:
                        content = fp.read()
                    if content.find(string) != -1:
                        self.result.append(f)
                        self.showTE.setPlainText(self.showResult())
                    if content.find(string.encode('unicode_escape').decode()) != -1:
                        self.result.append(f)
                        self.showTE.setPlainText(self.showResult())
                except Exception as e:
                    print(f,e)
            elif os.path.isdir(f):
                self.search(f, string)
            else:
                pass

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle("文本搜索器")
        self.label.setText(_translate("Form", "查找路径:"))
        self.copy1.setText(_translate("Form", "复制"))
        self.paste1.setText(_translate("Form", "粘贴"))
        self.paste2.setText(_translate("Form", "粘贴"))
        self.label_2.setText(_translate("Form", "查找文本:"))
        self.copy2.setText(_translate("Form", "复制"))
        self.copy3.setText(_translate("Form", "复制"))
        self.paste3.setText(_translate("Form", "粘贴"))
        self.label_3.setText(_translate("Form", "返回结果:"))
        self.goBTN.setText(_translate("Form", "查找"))
        self.showLEN.setText(_translate("Form", "请输入要检索的路径和文本内容,然后点击\"查找\""))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

三、打包

pyinstaller.exe -Fw main.py

四、效果

嚯、打包完30多M,pyqt打包起来是真的大:
在这里插入图片描述

点我下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值