赞
踩
PyQt5从入门到实践系列-01-配置安装
PyQt5从入门到实践系列-02-布局(数据展示GUI小项目)
PyQt5从入门到实践系列-03-QTableWidget(选取表单数据绘图)
PyQt5从入门到实践系列-04-数据可视化(PyQtChart)
PyQt5从入门到实践系列-05-数据可视化(GUI中Matplotlib)
PyQt5从入门到实践系列-06-QtreeWidget树形控件
PyQt5从入门到精通系列-07-信号与槽详解
PyQt5从入门到精通系列-08-文件
上期,我们已经介绍了基于PyQt5常用的文件操作,本期,我们介绍QFileSystemWatcher文件目录监听类。QFileSystemWatcher把一些目录或文件添加到监听列表后,当目录或文件发生修改、删除等变化时会发射信号,从而实现对目录和文件的监听。基本函数及操作如下:
附上完整小项目案例代码
第一部分:ui文件代码
-
- 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.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
- self.gridLayout_2.setObjectName("gridLayout_2")
- self.gridLayout = QtWidgets.QGridLayout()
- self.gridLayout.setObjectName("gridLayout")
- self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
- self.lineEdit.setObjectName("lineEdit")
- self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
- self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
- self.lineEdit_2.setObjectName("lineEdit_2")
- self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
- self.pushButton = QtWidgets.QPushButton(self.centralwidget)
- font = QtGui.QFont()
- font.setFamily("Agency FB")
- font.setPointSize(14)
- font.setBold(True)
- font.setWeight(75)
- self.pushButton.setFont(font)
- self.pushButton.setObjectName("pushButton")
- self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
- self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
- font = QtGui.QFont()
- font.setFamily("Agency FB")
- font.setPointSize(14)
- font.setBold(True)
- font.setWeight(75)
- self.pushButton_2.setFont(font)
- self.pushButton_2.setObjectName("pushButton_2")
- self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1)
- self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
- self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
- self.textEdit.setObjectName("textEdit")
- self.gridLayout_2.addWidget(self.textEdit, 1, 0, 1, 1)
- MainWindow.setCentralWidget(self.centralwidget)
- self.menubar = QtWidgets.QMenuBar(MainWindow)
- self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
- self.menubar.setObjectName("menubar")
- MainWindow.setMenuBar(self.menubar)
- 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.pushButton.setText(_translate("MainWindow", "监听文件:"))
- self.pushButton_2.setText(_translate("MainWindow", "监听目录:"))
-
'运行
第二部分:主逻辑代码
- import os
- import sys
-
- import qdarkstyle
- from PyQt5.QtWidgets import QApplication, QMainWindow
- from PyQt5.QtCore import (QFileSystemWatcher,QDir,QTimer)
- from PyQt5.QtWidgets import QFileDialog
- from watchdog import Ui_MainWindow
-
-
- class QmyMainWindow(QMainWindow):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.ui = Ui_MainWindow()
- self.ui.setupUi(self)
- self.file_name = ""
- self.select_path = ""
-
- self.timer = QTimer(self)
- self.timer.setInterval(500)
- self.timer.timeout.connect(self.handle_timer)
- self.timer.start(1000)
-
- self.fileWatcher = QFileSystemWatcher()
- self.ui.pushButton.clicked.connect(self.open_file)
- self.ui.pushButton_2.clicked.connect(self.open_dirs)
-
- self.fileWatcher.fileChanged.connect(self.do_fileChanged)
- self.fileWatcher.directoryChanged.connect(self.do_directoryChanged)
- self.processed_files = set()
-
- def do_directoryChanged(self,path):
- file_name = path.split('/')[-1]
- if file_name not in self.processed_files:
- self.processed_files.add(file_name)
-
- def handle_timer(self):
- for file_name in QDir(self.select_path).entryList():
- if file_name not in self.processed_files:
- self.ui.textEdit.append("New file added:"+ os.path.join(self.select_path,file_name))
- self.processed_files.add(file_name)
-
- def do_fileChanged(self):
- self.ui.textEdit.append(os.path.join(self.file_name)+"文件发生了改变")
-
- def open_file(self):
- curr_path = QDir.currentPath()
- self.file_name, ok = QFileDialog.getOpenFileName(self, "打开文件", curr_path, "所有文件(*.*)")
- self.ui.lineEdit.setText(self.file_name)
- self.ui.textEdit.append(self.file_name)
- self.fileWatcher.addPath(self.file_name)
-
- def open_dirs(self):
- curr_path = QDir.currentPath()
- self.select_path = QFileDialog.getExistingDirectory(self, "选择一个目录",
- curr_path, QFileDialog.ShowDirsOnly)
- self.ui.lineEdit_2.setText(self.select_path)
- self.ui.textEdit.append(self.select_path)
- self.fileWatcher.addPath(self.select_path)
-
-
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- form = QmyMainWindow()
- form.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
- form.show()
- sys.exit(app.exec_())
本小节,重点介绍pyqt5中的QFileSystemWatcher,实现监听文件目录,当目录中新增文件或目录时显示新增文件或目录的完整路径,代码粗糙,仅提供参考。PyQt5中的文件监听使用不太方便,下期我们聊一聊实用的文件和目录实时监听框架的使用及实践案例。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。