代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'F:\prog-tmp\PyQt5-code\MDI_code_compV0.33\FineText.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import os
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(476, 157)
Dialog.setSizeGripEnabled(True)
self.groupBox = QtWidgets.QGroupBox(Dialog)
self.groupBox.setGeometry(QtCore.QRect(20, 70, 181, 71))
self.groupBox.setObjectName("groupBox")
self.rdb_currentFile = QtWidgets.QRadioButton(self.groupBox)
self.rdb_currentFile.setGeometry(QtCore.QRect(20, 20, 111, 16))
self.rdb_currentFile.setObjectName("rdb_currentFile")
self.rdb_cbpDir = QtWidgets.QRadioButton(self.groupBox)
self.rdb_cbpDir.setGeometry(QtCore.QRect(20, 50, 101, 16))
self.rdb_cbpDir.setObjectName("rdb_cbpDir")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 30, 61, 16))
self.label.setObjectName("label")
self.lineEdit_destText = QtWidgets.QLineEdit(Dialog)
self.lineEdit_destText.setGeometry(QtCore.QRect(70, 30, 321, 20))
self.lineEdit_destText.setObjectName("lineEdit_destText")
self.ButtonFind = QtWidgets.QPushButton(Dialog)
self.ButtonFind.setGeometry(QtCore.QRect(400, 26, 61, 31))
self.ButtonFind.setObjectName("ButtonFind")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "查找"))
self.groupBox.setTitle(_translate("Dialog", "查找范围:"))
self.rdb_currentFile.setText(_translate("Dialog", "当前文件"))
self.rdb_cbpDir.setText(_translate("Dialog", "当前工程目录"))
self.label.setText(_translate("Dialog", "查找内容:"))
self.ButtonFind.setText(_translate("Dialog", "查找"))
class FindText(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(FindText, self).__init__(parent)
self.setupUi(self)
#1.主窗口作为父窗口
self.parent = parent
#2. 点击查找按钮的信号的槽函数
self.ButtonFind.clicked.connect(self.FineTextInFiles)
self.rdb_cbpDir.setChecked(True)
def FineTextInFiles(self):
self.parent.text_browser.clear()
if os.path.isdir(self.parent.ProjectDir) == False:
self.parent.text_browser.append("工程目录出错。")
return
text_file_list = []
#1. 仅仅查找当前的文件
if self.rdb_currentFile.isChecked():
text_file_list.append(self.parent.mdi.activeSubWindow().widget().filename)
#2. 扫描整个工程目录的c、h文件,并存放到 text_file_list
else:
walk_tree = os.walk(self.parent.ProjectDir)
#"walk" through the directory tree and save the readable files to a list
for root, subFolders, files in walk_tree:
for file in files:
full_with_path = os.path.join(root, file)
if os.path.exists(full_with_path) != None:
endChar = full_with_path[-1]
if 'h' == endChar or 'H' == endChar or 'c' == endChar or 'C' == endChar:
full_with_path = full_with_path.replace("\\", "/")
text_file_list.append(full_with_path)
#2.1 包含的系统文件也要
for file in self.parent.file_list:
if file not in text_file_list:
text_file_list.append(file)
#3. 开始查找
destCount=0
for file in text_file_list:
fin = open(file, "r", encoding='utf-8', errors="ignore")
input_file = fin.read()#.encode("gbk").decode("utf-8")
input_str = input_file.splitlines(False)
for i in range(len(input_str) ) :
if self.lineEdit_destText.text() in input_str[i]:
destCount += 1
self.parent.text_browser.append("%s:%d:%s"%(file, i+1, input_str[i]))
#4. 在信息输出窗口中显示结果
self.parent.text_browser.append("在 %d 个文件中找到 %d 处。"%(len(text_file_list), destCount))
#5. 发送一个 QCloseEvent,窗口自杀
e = QCloseEvent()
QApplication.postEvent(self, e)
'''
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
'''
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。