• Pyside6 QFileDialog



    Pyside6 QFileDialog提供了一个允许用户选择文件或目录的对话框。关于QFileDialog的使用可以参考下面的文档

    https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QFileDialog.html#qfiledialog

    Pyside6 QFileDialog

    常用函数

    函数作用
    getOpenFileName打开单个已存在的文件
    getOpenFileNames打开多个已存在的文件
    getSaveFileName打开需要保存的文件
    getExistingDirectory打开已存在的文件夹

    getOpenFileName

    getOpenFileName是打开一个已经存在的文件,如果文件存在就返回该文件的文件路径,如果不存在就返回空。

    static PySide6.QtWidgets.QFileDialog.getOpenFileName([parent=None[, caption=""[, dir=""[, filter=""[, selectedFilter=""[, options=QFileDialog.Options()]]]]]])
    
    # parent:父组件
    # caption:对话框的标题
    # dir:默认路径 比如在windows下默认选择C盘则应该为 'C:\\'
    # filter:话框的后缀名过滤器 比如筛选txt和bin文件 图像文件 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)'
    # selectedFilter:默认选择的过滤器
    # options:参数
    '''
    options参数可选
     ShowDirsOnly             : QFileDialog.Option = ... # 0x1
     DontResolveSymlinks      : QFileDialog.Option = ... # 0x2
     DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4
     DontUseNativeDialog      : QFileDialog.Option = ... # 0x8
     ReadOnly                 : QFileDialog.Option = ... # 0x10
     HideNameFilterDetails    : QFileDialog.Option = ... # 0x20
     DontUseCustomDirectoryIcons: QFileDialog.Option = ... # 0x40
     可用|运算符进行组合
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    file =  QFileDialog.getOpenFileName(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件
    
    • 1

    在这里插入图片描述

    getOpenFileNames

    getOpenFileNames是打开多个已经存在的文件,如果文件存在就返回该文件的文件路径,如果不存在就返回空。

    static PySide6.QtWidgets.QFileDialog.getOpenFileNames([parent=None[, caption=""[, dir=""[, filter=""[, selectedFilter=""[, options=QFileDialog.Options()]]]]]])
    
    # parent:父组件
    # caption:对话框的标题
    # dir:默认路径 比如在windows下默认选择C盘则应该为 'C:\\'
    # 话框的后缀名过滤器 比如筛选txt和bin文件 图像文件 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)'
    # selectedFilter:默认选择的过滤器
    # options:参数
    '''
    options参数可选
     ShowDirsOnly             : QFileDialog.Option = ... # 0x1
     DontResolveSymlinks      : QFileDialog.Option = ... # 0x2
     DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4
     DontUseNativeDialog      : QFileDialog.Option = ... # 0x8
     ReadOnly                 : QFileDialog.Option = ... # 0x10
     HideNameFilterDetails    : QFileDialog.Option = ... # 0x20
     DontUseCustomDirectoryIcons: QFileDialog.Option = ... # 0x40
     可用|运算符进行组合
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    file =  QFileDialog.getOpenFileNames(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件
    
    • 1

    在这里插入图片描述

    getExistingDirectory

    getExistingDirectory是打开单个已存在的文件夹,如果文件夹存在就返回该文件夹的的路径,如果不存在就返回空。

    static PySide6.QtWidgets.QFileDialog.getExistingDirectory([parent=None[, caption=""[, dir=""[, options=QFileDialog.Option.ShowDirsOnly]]]])
    
    # parent:父组件
    # caption:对话框的标题
    # dir:默认路径 比如在windows下默认选择C盘则应该为 'C:\\'
    # options:参数
    '''
    options参数可选
     ShowDirsOnly             : QFileDialog.Option = ... # 0x1
     DontResolveSymlinks      : QFileDialog.Option = ... # 0x2
     DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4
     DontUseNativeDialog      : QFileDialog.Option = ... # 0x8
     ReadOnly                 : QFileDialog.Option = ... # 0x10
     HideNameFilterDetails    : QFileDialog.Option = ... # 0x20
     DontUseCustomDirectoryIcons: QFileDialog.Option = ... # 0x40
     可用|运算符进行组合
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    file =  QFileDialog.getExistingDirectory(self, "选择文件夹","",QFileDialog.ShowDirsOnly| QFileDialog.DontResolveSymlinks)
    
    • 1

    在这里插入图片描述

    getSaveFileName

    getSaveFileName是获取需要保存文件的文件名,此函数不会帮你创建文件,该函数允许返回不存在的文件路径,调用成功后该函数会返回文件的路径,如果取消则返回空。

    static PySide6.QtWidgets.QFileDialog.getSaveFileName([parent=None[, caption=""[, dir=""[, filter=""[, selectedFilter=""[, options=QFileDialog.Options()]]]]]])
    
    # parent:父组件
    # caption:对话框的标题
    # dir:默认路径 比如在windows下默认选择C盘则应该为 'C:\\'
    # 话框的后缀名过滤器 比如筛选txt和bin文件 图像文件 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)'
    # selectedFilter:默认选择的过滤器
    # options:参数
    '''
    options参数可选
     ShowDirsOnly             : QFileDialog.Option = ... # 0x1
     DontResolveSymlinks      : QFileDialog.Option = ... # 0x2
     DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4
     DontUseNativeDialog      : QFileDialog.Option = ... # 0x8
     ReadOnly                 : QFileDialog.Option = ... # 0x10
     HideNameFilterDetails    : QFileDialog.Option = ... # 0x20
     DontUseCustomDirectoryIcons: QFileDialog.Option = ... # 0x40
     可用|运算符进行组合
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    file  = QFileDialog.getSaveFileName(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件
    
    • 1

    在这里插入图片描述

    程序

    界面程序

    
    <ui version="4.0">
     <class>MainWindowclass>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0x>
        <y>0y>
        <width>444width>
        <height>331height>
       rect>
      property>
      <property name="windowTitle">
       <string>MainWindowstring>
      property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item>
         <widget class="QTabWidget" name="tabWidget">
          <property name="currentIndex">
           <number>0number>
          property>
          <widget class="QWidget" name="tab">
           <attribute name="title">
            <string>getOpenFileNamestring>
           attribute>
           <layout class="QVBoxLayout" name="verticalLayout_3">
            <item>
             <layout class="QVBoxLayout" name="verticalLayout">
              <property name="leftMargin">
               <number>120number>
              property>
              <item>
               <widget class="QPushButton" name="pushButton_2">
                <property name="maximumSize">
                 <size>
                  <width>150width>
                  <height>16777215height>
                 size>
                property>
                <property name="text">
                 <string>选择单个文件string>
                property>
               widget>
              item>
             layout>
            item>
            <item>
             <layout class="QHBoxLayout" name="horizontalLayout">
              <item>
               <widget class="QLineEdit" name="lineEdit">
                <property name="font">
                 <font>
                  <pointsize>10pointsize>
                 font>
                property>
               widget>
              item>
             layout>
            item>
           layout>
          widget>
          <widget class="QWidget" name="tab_2">
           <attribute name="title">
            <string>getOpenFileNamesstring>
           attribute>
           <layout class="QVBoxLayout" name="verticalLayout_6">
            <item>
             <layout class="QVBoxLayout" name="verticalLayout_4">
              <property name="leftMargin">
               <number>120number>
              property>
              <item>
               <widget class="QPushButton" name="pushButton_3">
                <property name="maximumSize">
                 <size>
                  <width>150width>
                  <height>16777215height>
                 size>
                property>
                <property name="text">
                 <string>选择多个文件string>
                property>
               widget>
              item>
             layout>
            item>
            <item>
             <layout class="QVBoxLayout" name="verticalLayout_5">
              <item>
               <widget class="QTextEdit" name="textEdit"/>
              item>
             layout>
            item>
           layout>
          widget>
          <widget class="QWidget" name="tab_3">
           <attribute name="title">
            <string>getExistingDirectorystring>
           attribute>
           <layout class="QVBoxLayout" name="verticalLayout_8">
            <item>
             <layout class="QVBoxLayout" name="verticalLayout_7">
              <property name="leftMargin">
               <number>120number>
              property>
              <item>
               <widget class="QPushButton" name="pushButton_5">
                <property name="maximumSize">
                 <size>
                  <width>150width>
                  <height>16777215height>
                 size>
                property>
                <property name="text">
                 <string>选择文件夹string>
                property>
               widget>
              item>
             layout>
            item>
            <item>
             <layout class="QHBoxLayout" name="horizontalLayout_2">
              <item>
               <widget class="QLineEdit" name="lineEdit_2">
                <property name="font">
                 <font>
                  <pointsize>10pointsize>
                 font>
                property>
               widget>
              item>
             layout>
            item>
           layout>
          widget>
          <widget class="QWidget" name="tab_4">
           <attribute name="title">
            <string>getSaveFileNamestring>
           attribute>
           <layout class="QVBoxLayout" name="verticalLayout_9">
            <item>
             <layout class="QHBoxLayout" name="horizontalLayout_3">
              <item>
               <widget class="QPushButton" name="pushButton">
                <property name="maximumSize">
                 <size>
                  <width>150width>
                  <height>16777215height>
                 size>
                property>
                <property name="text">
                 <string>选择要保存的文件string>
                property>
               widget>
              item>
             layout>
            item>
            <item>
             <layout class="QHBoxLayout" name="horizontalLayout_4">
              <item>
               <widget class="QLineEdit" name="lineEdit_3"/>
              item>
             layout>
            item>
           layout>
          widget>
         widget>
        item>
       layout>
      widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0x>
         <y>0y>
         <width>444width>
         <height>22height>
        rect>
       property>
      widget>
      <widget class="QStatusBar" name="statusbar"/>
     widget>
     <resources/>
     <connections/>
    ui>
    
    
    • 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
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187

    主程序

    # Import Qt libraries
    from PySide6.QtWidgets import *
    from PySide6.QtCore import QFile,Qt,QTimer
    # Import UI developed in Qt Creator
    from FileDialog_ui import Ui_MainWindow  # 导入界面
    # Import PseudoSensor
    # Import system tools and datetime
    import sys
    import statistics
    import time
    from datetime import datetime
    from PySide6 import QtGui, QtWidgets
    from PySide6.QtGui import QIcon, QPixmap, QMovie, QPainter, QBrush, QPen,QColor,QPalette,QFont,QImage,QPixmap
    import random
    
    # Create and start the Qt application
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            
            # 设置界面为用户设计的界面
            self.ui = Ui_MainWindow() 
            self.ui.setupUi(self) 
    
            self.ui.pushButton_2.clicked.connect(self.getOpenFileName)
            self.ui.pushButton_3.clicked.connect(self.getOpenFileNames)
            self.ui.pushButton_5.clicked.connect(self.getExistingDirectory)
            self.ui.pushButton.clicked.connect(self.getSaveFileName)
    
        def getOpenFileName(self):
            file  = QFileDialog.getOpenFileName(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件
            self.ui.lineEdit.setText(file[0])
            print(file)
    
        def getOpenFileNames(self):
            file  = QFileDialog.getOpenFileNames(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件
            self.ui.textEdit.setText(str(file[0]))
            print(file)
    
        def getSaveFileName(self):
            file  = QFileDialog.getSaveFileName(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件
            self.ui.lineEdit_3.setText(file[0])
            print(file)
    
        def getExistingDirectory(self):
            file = QFileDialog.getExistingDirectory(self, "选择文件夹","",QFileDialog.ShowDirsOnly| QFileDialog.DontResolveSymlinks)
            self.ui.lineEdit_2.setText(file)
            print(file)
    
        def closeAndExit(self):
            sys.exit()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv) # 初始化QApplication
    
        # 初始化界面并显示界面
        window = MainWindow() 
        window.show() 
        window.setFixedSize(window.width(), window.height())
        sys.exit(app.exec())
    
    • 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
  • 相关阅读:
    Spring Boot Spring Cloud 微服务 分布式项目 实现接口幂等性的 4 种方案
    Linux》yum与vim
    [附源码]java毕业设计教务系统
    BigDecimal使用方法
    [架构之路-231]:计算机硬件与体系结构 - 性能评估汇总,性能优化加速比
    数据结构——栈
    php yield使用
    MySQL数据库的数据类型和基于MySQL数据类型的综合实例项目
    2022Web前端从入门到精通资料
    巨细靡遗流程控制,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang流程结构详解EP09
  • 原文地址:https://blog.csdn.net/hwx1546/article/details/133960385