PySide6 GUI 编程(27):QFileDialog的简单使用

2024-08-21 10:00:11 浏览数 (1)

QFileDialog 的常见用法

用法示例

代码语言:python代码运行次数:0复制
    def choose_single_local_file(self):
        filters_str, default_filter = get_filters()
        ret = QFileDialog.getOpenFileName(self, 'Open File', './', filters_str, default_filter)
        print('选择单个本地文件', ret)

    def choose_multiple_local_files(self):
        filters_str, default_filter = get_filters()
        ret = QFileDialog.getOpenFileNames(self, 'Open Files', './', filters_str, default_filter)
        print('选择多个本地文件', ret)

    def choose_single_save_file(self):
        filters_str, default_filter = get_save_format()
        ret = QFileDialog.getSaveFileName(self, 'Save File', './', filters_str, default_filter)
        print('选择单个保存文件', ret)

    def choose_single_folder(self):
        ret = QFileDialog.getExistingDirectory(self, 'Save Folder', './',
                                               options = QFileDialog.Option.ReadOnly | QFileDialog.Option.ShowDirsOnly)
        print('选择保存文件夹', ret)

完整代码

代码语言:python代码运行次数:0复制
from __future__ import annotations

import sys
from datetime import datetime
from typing import Tuple

from PySide6.QtWidgets import QApplication, QFileDialog, QMainWindow, QPushButton, QVBoxLayout, QWidget


def get_time_str() -> str:
    return datetime.now().isoformat(sep = ' ')


def get_filters() -> Tuple[str, str]:
    my_filters = [
        'All Files (*)',
        'Text Files (*.txt)',
        'Python Files (*.py)',
        'Image Files (*.png *.jpg *.bmp)',
        'Video Files (*.mp4 *.avi)',
        'Audio Files (*.mp3 *.wav)',
        'PDF Files (*.pdf)',
        'Json Files (*.json)',
        'Excel Files (*.xls *.xlsx)',
        'Markdown Files (*.md)',
        'Word Files (*.doc *.docx)',
        'PowerPoint Files (*.ppt *.pptx)',
        'HTML Files (*.html)',
        'CSS Files (*.css)',
        'JavaScript Files (*.js)',
    ]
    # 将 my_filters 转换为字符串列表,并以;;分隔
    my_filters_str = ';;'.join(my_filters)
    return my_filters_str, my_filters[2]


def get_save_format() -> Tuple[str, str]:
    my_filters = [
        'Text Files (*.txt)',
        'Python Files (*.py)',
        'Image Files PNG (*.png)',
        'Image Files JPG (*.jpg)',
        'Image Files BMP (*.bmp)',
        'Video Files MP4(*.mp4)',
        'Video Files AVI(*.avi)',
        'Audio Files MP3(*.mp3)',
        'Audio Files WAV(*.wav)',
        'PDF Files (*.pdf)',
        'Json Files (*.json)',
        'Excel Files (*.xls)',
        'Excel Files (*.xlsx)',
        'Markdown Files (*.md)',
        'Word Files (*.doc)',
        'Word Files (*.docx)',
        'PowerPoint Files (*.ppt)',
        'PowerPoint Files (*.pptx)',
        'HTML Files (*.html)',
        'CSS Files (*.css)',
        'JavaScript Files (*.js)',
    ]
    # 将 my_filters 转换为字符串列表,并以;;分隔
    my_filters_str = ';;'.join(my_filters)
    return my_filters_str, my_filters[0]


class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Hello, PySide6!')
        self.setToolTip('A PySide6 GUI Application Demo')

        self.open_single_file_button = QPushButton('选择单个文件')
        self.open_single_file_button.clicked.connect(self.choose_single_local_file)

        self.open_multiple_files_button = QPushButton('选择多个文件')
        self.open_multiple_files_button.clicked.connect(self.choose_multiple_local_files)

        self.save_single_file_button = QPushButton('保存单个文件')
        self.save_single_file_button.clicked.connect(self.choose_single_save_file)

        self.save_folder_button = QPushButton('选择文件夹')
        self.save_folder_button.clicked.connect(self.choose_single_folder)

        v_layout = QVBoxLayout()
        v_layout.addWidget(self.open_single_file_button)
        v_layout.addWidget(self.open_multiple_files_button)
        v_layout.addWidget(self.save_single_file_button)
        v_layout.addWidget(self.save_folder_button)

        container = QWidget()
        container.setLayout(v_layout)

        self.setCentralWidget(container)

    def choose_single_local_file(self):
        filters_str, default_filter = get_filters()
        ret = QFileDialog.getOpenFileName(self, 'Open File', './', filters_str, default_filter)
        print('选择单个本地文件', ret)

    def choose_multiple_local_files(self):
        filters_str, default_filter = get_filters()
        ret = QFileDialog.getOpenFileNames(self, 'Open Files', './', filters_str, default_filter)
        print('选择多个本地文件', ret)

    def choose_single_save_file(self):
        filters_str, default_filter = get_save_format()
        ret = QFileDialog.getSaveFileName(self, 'Save File', './', filters_str, default_filter)
        print('选择单个保存文件', ret)

    def choose_single_folder(self):
        ret = QFileDialog.getExistingDirectory(self, 'Save Folder', './',
                                               options = QFileDialog.Option.ReadOnly | QFileDialog.Option.ShowDirsOnly)
        print('选择保存文件夹', ret)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyMainWindow()
    window.show()
    app.exec()

运行效果

静态效果

静态界面效果静态界面效果

选择本地的单个文件

选择本地文件选择本地文件
自定义文件格式过滤条件自定义文件格式过滤条件

选择多个本地文件

选择多个本地文件选择多个本地文件

保存文件

保存文件保存文件
自定义保存文件的格式选项自定义保存文件的格式选项

选择文件夹

选择本地文件夹选择本地文件夹

0 人点赞