这是VSCode的文件资源管理或者叫资源文件导航,这是一个树形结构的,我们使用 QTreeWidget来实现一下,最终的结果如下:
我先把代码贴出来,UI界面的编程,不可能把每一行代码都讲清楚,我只讲一些思路和需要注意的细节吧:
第一个文件(fileopen.py),UI设计的文件,是QT Designer设计好的ui文件直接转换为的Python文件:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'fileopen.ui'## Created by: PyQt5 UI code generator 5.13.0## WARNING! All changes made in this file will be lost! 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") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) self.menubar.setObjectName("menubar") self.menufile = QtWidgets.QMenu(self.menubar) self.menufile.setObjectName("menufile") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.actionfileopen = QtWidgets.QAction(MainWindow) self.actionfileopen.setObjectName("actionfileopen") self.menufile.addAction(self.actionfileopen) self.menubar.addAction(self.menufile.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.menufile.setTitle(_translate("MainWindow", "File")) self.actionfileopen.setText(_translate("MainWindow", "Open Folder..."))
第二个文件(file_tree.py), 逻辑文件,主要内容的实现,在这里:
import sysfrom PyQt5.QtWidgets import *from PyQt5 import Qt, QtGuifrom filename import file_name as fnfrom fileopen import Ui_MainWindowfrom PyQt5 import QtWidgetsimport threadingimport os class Tree(QMainWindow, Ui_MainWindow): def __init__(self, parent = None): super(Tree, self).__init__(parent) self.setupUi(self) self.setWindowTitle("File_Tree") self.actionfileopen.triggered.connect(self.Open_Folder) def Open_Folder(self): path = QFileDialog.getExistingDirectory(self, "选取文件夹", "./") self.tree = QTreeWidget() self.tree.setColumnCount(1) self.tree.setColumnWidth(0, 50) self.tree.setHeaderLabels(["EXPLORER"]) self.tree.setIconSize(Qt.QSize(25, 25)) self.tree.setSelectionMode(QAbstractItemView.ExtendedSelection) self.actionfileopen.triggered.connect(self.Open_Folder) dirs = fn(path) fileInfo = Qt.QFileInfo(path) fileIcon = Qt.QFileIconProvider() icon = QtGui.QIcon(fileIcon.icon(fileInfo)) root = QTreeWidgetItem(self.tree) root.setText(0,path.split('/')[-1]) root.setIcon(0,QtGui.QIcon(icon)) self.CreateTree(dirs, root, path) self.setCentralWidget(self.tree) QApplication.processEvents() def CreateTree(self, dirs, root, path): for i in dirs: path_new = path + '\\' + i if os.path.isdir(path_new): fileInfo = Qt.QFileInfo(path_new) fileIcon = Qt.QFileIconProvider() icon = QtGui.QIcon(fileIcon.icon(fileInfo)) child = QTreeWidgetItem(root) child.setText(0,i) child.setIcon(0,QtGui.QIcon(icon)) dirs_new = fn(path_new) self.CreateTree(dirs_new, child, path_new) else: fileInfo = Qt.QFileInfo(path_new) fileIcon = Qt.QFileIconProvider() icon = QtGui.QIcon(fileIcon.icon(fileInfo)) child = QTreeWidgetItem(root) child.setText(0,i) child.setIcon(0,QtGui.QIcon(icon)) if __name__ == "__main__": app = QApplication(sys.argv) win = Tree() win.show() sys.exit(app.exec_())
第三个文件(filename.py)我用于测试的文件,但是其中 file_name函数用于读取目录下的文件名,要使用到:
import os def file_name(path): return os.listdir(path) if __name__ == "__main__": path = 'C:\\Users\\...\\Desktop\\SeeTheData' dirs = file_name(path) for i in dirs: print(i)
菜单选项的事件函数如下:
def Open_Folder(self): path = QFileDialog.getExistingDirectory(self, "选取文件夹", "./") self.tree = QTreeWidget() self.tree.setColumnCount(1) self.tree.setColumnWidth(0, 50) self.tree.setHeaderLabels(["EXPLORER"]) self.tree.setIconSize(Qt.QSize(25, 25)) self.tree.setSelectionMode(QAbstractItemView.ExtendedSelection) self.actionfileopen.triggered.connect(self.Open_Folder) dirs = fn(path) fileInfo = Qt.QFileInfo(path) fileIcon = Qt.QFileIconProvider() icon = QtGui.QIcon(fileIcon.icon(fileInfo)) root = QTreeWidgetItem(self.tree) root.setText(0,path.split('/')[-1]) root.setIcon(0,QtGui.QIcon(icon)) self.CreateTree(dirs, root, path) self.setCentralWidget(self.tree) QApplication.processEvents()
生成文件选择对话框,并获得目录路径:
path = QFileDialog.getExistingDirectory(self, "选取文件夹", "./")
创建QTreeWidget组件,并设置相关参数:
self.tree = QTreeWidget()self.tree.setColumnCount(1)self.tree.setColumnWidth(0, 50)self.tree.setHeaderLabels(["EXPLORER"])self.tree.setIconSize(Qt.QSize(25, 25))self.tree.setSelectionMode(QAbstractItemView.ExtendedSelection)
获取文件在系统下的图标:
fileInfo = Qt.QFileInfo(path)fileIcon = Qt.QFileIconProvider()icon = QtGui.QIcon(fileIcon.icon(fileInfo))
创建根节点:
root = QTreeWidgetItem(self.tree)root.setText(0,path.split('/')[-1])root.setIcon(0,QtGui.QIcon(icon))
递归创建子节点的函数如下:
def CreateTree(self, dirs, root, path): for i in dirs: path_new = path + '\\' + i if os.path.isdir(path_new): fileInfo = Qt.QFileInfo(path_new) fileIcon = Qt.QFileIconProvider() icon = QtGui.QIcon(fileIcon.icon(fileInfo)) child = QTreeWidgetItem(root) child.setText(0,i) child.setIcon(0,QtGui.QIcon(icon)) dirs_new = fn(path_new) self.CreateTree(dirs_new, child, path_new) else: fileInfo = Qt.QFileInfo(path_new) fileIcon = Qt.QFileIconProvider() icon = QtGui.QIcon(fileIcon.icon(fileInfo)) child = QTreeWidgetItem(root) child.setText(0,i) child.setIcon(0,QtGui.QIcon(icon))
os.path.isdir用于判断路径是文件还是文件夹。
原网址: 访问
创建于: 2024-05-22 16:34:00
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论