当前位置:   article > 正文

PyQt5从入门到精通系列-09-关于文件监听QFileSystemWatcher_pyqt从入门到精通pdf

pyqt从入门到精通pdf

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文件代码

  1. from PyQt5 import QtCore, QtGui, QtWidgets
  2. class Ui_MainWindow(object):
  3. def setupUi(self, MainWindow):
  4. MainWindow.setObjectName("MainWindow")
  5. MainWindow.resize(800, 600)
  6. self.centralwidget = QtWidgets.QWidget(MainWindow)
  7. self.centralwidget.setObjectName("centralwidget")
  8. self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
  9. self.gridLayout_2.setObjectName("gridLayout_2")
  10. self.gridLayout = QtWidgets.QGridLayout()
  11. self.gridLayout.setObjectName("gridLayout")
  12. self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
  13. self.lineEdit.setObjectName("lineEdit")
  14. self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
  15. self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
  16. self.lineEdit_2.setObjectName("lineEdit_2")
  17. self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
  18. self.pushButton = QtWidgets.QPushButton(self.centralwidget)
  19. font = QtGui.QFont()
  20. font.setFamily("Agency FB")
  21. font.setPointSize(14)
  22. font.setBold(True)
  23. font.setWeight(75)
  24. self.pushButton.setFont(font)
  25. self.pushButton.setObjectName("pushButton")
  26. self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
  27. self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
  28. font = QtGui.QFont()
  29. font.setFamily("Agency FB")
  30. font.setPointSize(14)
  31. font.setBold(True)
  32. font.setWeight(75)
  33. self.pushButton_2.setFont(font)
  34. self.pushButton_2.setObjectName("pushButton_2")
  35. self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1)
  36. self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
  37. self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
  38. self.textEdit.setObjectName("textEdit")
  39. self.gridLayout_2.addWidget(self.textEdit, 1, 0, 1, 1)
  40. MainWindow.setCentralWidget(self.centralwidget)
  41. self.menubar = QtWidgets.QMenuBar(MainWindow)
  42. self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
  43. self.menubar.setObjectName("menubar")
  44. MainWindow.setMenuBar(self.menubar)
  45. self.statusbar = QtWidgets.QStatusBar(MainWindow)
  46. self.statusbar.setObjectName("statusbar")
  47. MainWindow.setStatusBar(self.statusbar)
  48. self.retranslateUi(MainWindow)
  49. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  50. def retranslateUi(self, MainWindow):
  51. _translate = QtCore.QCoreApplication.translate
  52. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  53. self.pushButton.setText(_translate("MainWindow", "监听文件:"))
  54. self.pushButton_2.setText(_translate("MainWindow", "监听目录:"))
'
运行

 第二部分:主逻辑代码

  1. import os
  2. import sys
  3. import qdarkstyle
  4. from PyQt5.QtWidgets import QApplication, QMainWindow
  5. from PyQt5.QtCore import (QFileSystemWatcher,QDir,QTimer)
  6. from PyQt5.QtWidgets import QFileDialog
  7. from watchdog import Ui_MainWindow
  8. class QmyMainWindow(QMainWindow):
  9. def __init__(self, parent=None):
  10. super().__init__(parent)
  11. self.ui = Ui_MainWindow()
  12. self.ui.setupUi(self)
  13. self.file_name = ""
  14. self.select_path = ""
  15. self.timer = QTimer(self)
  16. self.timer.setInterval(500)
  17. self.timer.timeout.connect(self.handle_timer)
  18. self.timer.start(1000)
  19. self.fileWatcher = QFileSystemWatcher()
  20. self.ui.pushButton.clicked.connect(self.open_file)
  21. self.ui.pushButton_2.clicked.connect(self.open_dirs)
  22. self.fileWatcher.fileChanged.connect(self.do_fileChanged)
  23. self.fileWatcher.directoryChanged.connect(self.do_directoryChanged)
  24. self.processed_files = set()
  25. def do_directoryChanged(self,path):
  26. file_name = path.split('/')[-1]
  27. if file_name not in self.processed_files:
  28. self.processed_files.add(file_name)
  29. def handle_timer(self):
  30. for file_name in QDir(self.select_path).entryList():
  31. if file_name not in self.processed_files:
  32. self.ui.textEdit.append("New file added:"+ os.path.join(self.select_path,file_name))
  33. self.processed_files.add(file_name)
  34. def do_fileChanged(self):
  35. self.ui.textEdit.append(os.path.join(self.file_name)+"文件发生了改变")
  36. def open_file(self):
  37. curr_path = QDir.currentPath()
  38. self.file_name, ok = QFileDialog.getOpenFileName(self, "打开文件", curr_path, "所有文件(*.*)")
  39. self.ui.lineEdit.setText(self.file_name)
  40. self.ui.textEdit.append(self.file_name)
  41. self.fileWatcher.addPath(self.file_name)
  42. def open_dirs(self):
  43. curr_path = QDir.currentPath()
  44. self.select_path = QFileDialog.getExistingDirectory(self, "选择一个目录",
  45. curr_path, QFileDialog.ShowDirsOnly)
  46. self.ui.lineEdit_2.setText(self.select_path)
  47. self.ui.textEdit.append(self.select_path)
  48. self.fileWatcher.addPath(self.select_path)
  49. if __name__ == "__main__":
  50. app = QApplication(sys.argv)
  51. form = QmyMainWindow()
  52. form.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
  53. form.show()
  54. sys.exit(app.exec_())

 

本小节,重点介绍pyqt5中的QFileSystemWatcher,实现监听文件目录,当目录中新增文件或目录时显示新增文件或目录的完整路径,代码粗糙,仅提供参考。PyQt5中的文件监听使用不太方便,下期我们聊一聊实用的文件和目录实时监听框架的使用及实践案例。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/1006126
推荐阅读
  

闽ICP备14008679号