|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Created on 2025/04/08 |
| 6 | +@author: Irony |
| 7 | +@site: https://pyqt.site | https://github.com/PyQt5 |
| 8 | +@email: 892768447@qq.com |
| 9 | +@file: FileManager.py |
| 10 | +@description: |
| 11 | +""" |
| 12 | + |
| 13 | +try: |
| 14 | + from PyQt5.QtWidgets import ( |
| 15 | + QApplication, |
| 16 | + QColumnView, |
| 17 | + QFileSystemModel, |
| 18 | + QGridLayout, |
| 19 | + QMessageBox, |
| 20 | + QPushButton, |
| 21 | + QSizePolicy, |
| 22 | + QSpacerItem, |
| 23 | + QWidget, |
| 24 | + ) |
| 25 | +except Exception: |
| 26 | + from PySide2.QtWidgets import ( |
| 27 | + QApplication, |
| 28 | + QColumnView, |
| 29 | + QFileSystemModel, |
| 30 | + QGridLayout, |
| 31 | + QMessageBox, |
| 32 | + QPushButton, |
| 33 | + QSizePolicy, |
| 34 | + QSpacerItem, |
| 35 | + QWidget, |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +class FileManager(QWidget): # type: ignore |
| 40 | + def __init__(self, *args, **kwargs): |
| 41 | + super(FileManager, self).__init__(*args, **kwargs) |
| 42 | + self.resize(800, 600) |
| 43 | + self._view = QColumnView(self) |
| 44 | + self._btn = QPushButton("确定", self) |
| 45 | + layout = QGridLayout(self) |
| 46 | + layout.addWidget(self._view, 0, 0, 1, 2) |
| 47 | + layout.addItem( |
| 48 | + QSpacerItem( |
| 49 | + 40, |
| 50 | + 20, |
| 51 | + QSizePolicy.Expanding, |
| 52 | + QSizePolicy.Minimum, |
| 53 | + ), |
| 54 | + 1, |
| 55 | + 0, |
| 56 | + ) |
| 57 | + layout.addWidget(self._btn, 1, 1, 1, 1) |
| 58 | + layout.setRowStretch(1, 0) |
| 59 | + self._model = QFileSystemModel(self) |
| 60 | + self._model.setRootPath("") # 设置根路径 |
| 61 | + self._view.setModel(self._model) # type: ignore |
| 62 | + self._btn.clicked.connect(self.onAccept) # type: ignore |
| 63 | + |
| 64 | + def onAccept(self): |
| 65 | + path = self._model.filePath(self._view.currentIndex()) |
| 66 | + print("path", path) |
| 67 | + if path: |
| 68 | + QMessageBox.information(self, "提示", "路径:%s" % path) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + import cgitb |
| 73 | + import sys |
| 74 | + |
| 75 | + cgitb.enable(format="text") |
| 76 | + app = QApplication(sys.argv) |
| 77 | + w = FileManager() |
| 78 | + w.show() |
| 79 | + sys.exit(app.exec_()) |
0 commit comments