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