Python基础 | QFileDialog打开文件对话框获取路径地址和文件(夹)列表

# coding    :utf-8
# @Time     :2022-01-14 23:09
# @Author   :Kevin
# @Software :PyCharm
# @File     :openfileDialog.py
import os
import sys
from PyQt5.QtWidgets import QMessageBox, QFileDialog, QApplication, QMainWindow
from PyQt5.QtWidgets import QPushButton, QLineEdit, QVBoxLayout, QWidget

class myWin(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('打开文件对话框测试')
        self.resize(400,300)
        self.btn = QPushButton('打开文件', self)
        self.lineEidt = QLineEdit(self)
        # 添加垂直布局和容器控件widget并设置为中心控件
        self.vLayout = QVBoxLayout(self)
        self.vLayout.addWidget(self.lineEidt)
        self.vLayout.addWidget(self.btn)
        self.widget = QWidget(self)
        self.widget.setLayout(self.vLayout)
        self.setCentralWidget(self.widget)

        self.btn.clicked.connect(self.msg)

    def msg(self):
        try:
            self.dir_path = QFileDialog.getExistingDirectory(None, '选择路径', os.getcwd())
            self.lineEidt.setText(self.dir_path)
        except Exception as e:
            print(e)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = myWin()
    ui.show()
    sys.exit(app.exec_())

Python基础 | QFileDialog打开文件对话框获取路径地址和文件(夹)列表_第1张图片

Python基础 | QFileDialog打开文件对话框获取路径地址和文件(夹)列表_第2张图片

将指定路径获取所在目录下的所有文件名和文件夹名

# coding    :utf-8
# @Time     :2022-01-14 23:09
# @Author   :Kevin
# @Software :PyCharm
# @File     :openfileDialog.py
import os
import sys
from PyQt5.QtWidgets import QMessageBox, QFileDialog, QApplication, QMainWindow
from PyQt5.QtWidgets import QPushButton, QLineEdit,QTextEdit, QVBoxLayout, QWidget

class myWin(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('打开文件对话框测试')
        self.resize(400,300)
        self.btn = QPushButton('打开文件', self)
        self.btn2 = QPushButton('获取文件列表', self)
        self.lineEidt = QLineEdit(self)
        self.textEidt = QTextEdit(self)
        self.textEidt.setFixedHeight(200)

        # 添加垂直布局和容器控件widget并设置为中心控件
        self.vLayout = QVBoxLayout(self)
        self.vLayout.addWidget(self.lineEidt)
        self.vLayout.addWidget(self.textEidt)
        self.vLayout.addWidget(self.btn)
        self.vLayout.addWidget(self.btn2)
        self.widget = QWidget(self)
        self.widget.setLayout(self.vLayout)
        self.setCentralWidget(self.widget)

        self.btn.clicked.connect(self.msg)
        self.btn2.clicked.connect(self.getFiles)

    def msg(self):
        try:
            self.dir_path = QFileDialog.getExistingDirectory(None, '选择路径', os.getcwd())
            self.lineEidt.setText(self.dir_path)
        except Exception as e:
            print(e)

    def getFiles(self):
        # 列出文件夹下的所有目录与文件
        self.list = os.listdir(self.lineEidt.text())
        print(self.list)
        s1 = '\r\n' # 用换行符对列表字符串进行连接
        text=s1.join(self.list)
        self.textEidt.setPlainText(text)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = myWin()
    ui.show()
    sys.exit(app.exec_())

Python基础 | QFileDialog打开文件对话框获取路径地址和文件(夹)列表_第3张图片

 

你可能感兴趣的:(Python基础,python,ui)