Pyside6 QFileDialog

news2024/10/7 6:25:54

Pyside6 QFileDialog

  • Pyside6 QFileDialog
    • 常用函数
      • getOpenFileName
      • getOpenFileNames
      • getExistingDirectory
      • getSaveFileName
    • 程序
      • 界面程序
      • 主程序

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
 可用|运算符进行组合
'''
file =  QFileDialog.getOpenFileName(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件

在这里插入图片描述

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
 可用|运算符进行组合
'''
file =  QFileDialog.getOpenFileNames(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件

在这里插入图片描述

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
 可用|运算符进行组合
'''
file =  QFileDialog.getExistingDirectory(self, "选择文件夹","",QFileDialog.ShowDirsOnly| QFileDialog.DontResolveSymlinks)

在这里插入图片描述

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
 可用|运算符进行组合
'''
file  = QFileDialog.getSaveFileName(parent =  None, caption =  '请选择选择文件',  dir = '',filter = 'file(*.txt *.bin) ;;image(*.jpg *.bmp *.png *.gif)') # 选择后缀为.txt .bin的文件 和.jpg .bmp .png .gif的图像文件

在这里插入图片描述

程序

界面程序

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>444</width>
    <height>331</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout_2">
    <item>
     <widget class="QTabWidget" name="tabWidget">
      <property name="currentIndex">
       <number>0</number>
      </property>
      <widget class="QWidget" name="tab">
       <attribute name="title">
        <string>getOpenFileName</string>
       </attribute>
       <layout class="QVBoxLayout" name="verticalLayout_3">
        <item>
         <layout class="QVBoxLayout" name="verticalLayout">
          <property name="leftMargin">
           <number>120</number>
          </property>
          <item>
           <widget class="QPushButton" name="pushButton_2">
            <property name="maximumSize">
             <size>
              <width>150</width>
              <height>16777215</height>
             </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>10</pointsize>
             </font>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
      <widget class="QWidget" name="tab_2">
       <attribute name="title">
        <string>getOpenFileNames</string>
       </attribute>
       <layout class="QVBoxLayout" name="verticalLayout_6">
        <item>
         <layout class="QVBoxLayout" name="verticalLayout_4">
          <property name="leftMargin">
           <number>120</number>
          </property>
          <item>
           <widget class="QPushButton" name="pushButton_3">
            <property name="maximumSize">
             <size>
              <width>150</width>
              <height>16777215</height>
             </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>getExistingDirectory</string>
       </attribute>
       <layout class="QVBoxLayout" name="verticalLayout_8">
        <item>
         <layout class="QVBoxLayout" name="verticalLayout_7">
          <property name="leftMargin">
           <number>120</number>
          </property>
          <item>
           <widget class="QPushButton" name="pushButton_5">
            <property name="maximumSize">
             <size>
              <width>150</width>
              <height>16777215</height>
             </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>10</pointsize>
             </font>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
      <widget class="QWidget" name="tab_4">
       <attribute name="title">
        <string>getSaveFileName</string>
       </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>150</width>
              <height>16777215</height>
             </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>0</x>
     <y>0</y>
     <width>444</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

主程序

# 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())

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1118079.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Easyx趣味编程7,鼠标消息读取及音频播放

hello大家好&#xff0c;这里是dark flame master&#xff0c;今天给大家带来Easyx图形库最后一节功能实现的介绍&#xff0c;前边介绍了绘制各种图形及键盘交互&#xff0c;文字&#xff0c;图片等操作&#xff0c;今天就可以使写出的程序更加生动且容易操控。一起学习吧&…

CUDA编程入门系列(十)并行规约

一、什么是规约&#xff1f; 规约&#xff0c;用白话来说&#xff0c;就是将多个值变成一个值的操作&#xff0c;如向量求和&#xff0c;向量内积操作。 以向量求和为例&#xff0c;如果使用串行规约的话&#xff0c;那么就是依靠for循环来进行操作 for(int i 0; i < nums.…

泛微 E-Office download.php 任意文件读取漏洞

一、漏洞描述 泛微E-Office是一款企业级的全流程办公自动化软件&#xff0c;它包括协同办公、文档管理、知识管理、工作流管理等多个模块&#xff0c;涵盖了企业日常工作中的各个环节。泛微E-Office能够帮助企业实现全流程数字化、自动化&#xff0c;提高工作效率和管理质量&a…

#define 宏定义看这一篇文章就够了

前言&#xff1a;在c/c学习的过程中&#xff0c;宏定义&#xff08;#define&#xff09;是作为初学者学习到的为数不多的预处理指令&#xff0c;在学习的时候我们被告知他可以帮助我们更高效的写程序&#xff0c;可以增加程序的可读性&#xff0c;但宏定义&#xff08;#define&…

电脑出现xinput1_3.dll的错误提示怎么办?有什么办法可以解决

电脑如果缺失了xinput1_3.dll还是一件比较复杂的事情&#xff0c;那么电脑出现xinput1_3.dll的错误提示怎么办&#xff0c;又有什么办法可以解决xinput1_3.dll&#xff1f;今天我们就来聊聊xinput1_3.dll丢失的解决办法&#xff0c;来看看都有哪些办法可以解决吧。 一.常见的问…

[swift刷题模板] 树状数组(BIT/FenwickTree)

[TOC]([swift刷题模板] 树状数组(BIT/FenwickTree) ) 一、 算法&数据结构 1. 描述 [python刷题模板] 树状数组 二、 模板代码 1. 单点赋值(增加)&#xff0c;区间求和(PURQ) 例题: 307. 区域和检索 - 数组可修改 class BIT {var c: [Int]var n: Int init(_ n: Int){c…

13 Multi-Head Self-Attention(从空间角度解释为什么做多头)

博客配套视频链接: https://space.bilibili.com/383551518?spm_id_from=333.1007.0.0 b 站直接看 配套 github 链接:https://github.com/nickchen121/Pre-training-language-model 配套博客链接:https://www.cnblogs.com/nickchen121/p/15105048.html 上节课回顾 0:40 At…

java内部类学习总结/lambda表达式总结

使用内部类的情况 此时如果不使用内部类,下图会出现new 了两次Outer的情况 如果只想new 一次Outer 代码会很繁琐(如下),所以内部类可以很方便的访问外部类的一些私有属性,外部类也可方便访问内部类的私有属性 内部类说明 如果内部类没有追加 static 之前要想获取内部类的对象必…

Origami Studio for Mac:塑造未来,掌握原型设计之巅

在当今高度竞争的设计领域&#xff0c;原型设计的重要性不言而喻。它不仅是沟通想法&#xff0c;也是测试和改进设计的关键环节。而现在&#xff0c;一款强大的原型设计工具——Origami Studio for Mac&#xff0c;正在席卷设计界&#xff0c;以其独特的功能和卓越的性能&#…

【Javascript】基础数据类型

目录 基础数据类型 1.number 字面量声明 数字对象方式声明 整数判断 指定返回小数位数 NaN-表示非数字值 浮点精度 解决误差 String 字面量声明 数字对象声明 连接运算符 获取长度 大小写转换 转换成大写 转换成小写 ​编辑 移除空白 获取单字符 ​编辑 截…

循环队列----数据结构

缘由 在队列的顺序存储中&#xff0c;采用第二种出队的方式&#xff0c;将头指针 1 &#xff0c;可以避免元素的移动&#xff0c;但是这样也出现了一个问题 "假溢出" &#xff0c;如图&#xff1a; 当出现这种情况时&#xff1a;头指针和尾指针都指向了不可访问的地…

Qt窗体设计的布局

本文介绍Qt窗体的布局。 Qt窗体的布局分为手动布局和自动布局&#xff0c;手动布局即靠手工排布各控件的位置。而自动布局则是根据选择的布局类型自动按此类型排布各控件的位置&#xff0c;使用起来比较方便&#xff0c;本文主要介绍Qt的自动布局。 1.垂直布局 垂直布局就是…

[AUTOSAR][网络管理] 什么是BusOff? 如何实现它?

文章目录 一、BusOff检测机制(1)简介(2)目录介绍(3)软件实现逻辑代码运行流程如下:二、测试方法(1)物理测试三、示例代码(1) busoff_recovery.c(2) busoff_recovery.h一、BusOff检测机制 (1)简介 CAN控制器可以判断出错误的类型是总线上暂时的数据错误(如外部干扰等…

分布式锁 - 理论篇

一、为什么需要分布式锁 二、分布式锁实现 1.分布式锁演进 - 基本原理 我们可以同时去一个地方“占坑”&#xff0c;如果占到&#xff0c;就执行逻辑。否则就必须等待&#xff0c;直到释放锁。“占坑”可以去redis&#xff0c;可以去数据库&#xff0c;可以去任何大家都能访…

树状数组学习笔记

树状数组 树状数组的用途&#xff0c;主要是可以以 O ( log ⁡ n ) O(\log n) O(logn) 的时间复杂度维护前缀和。 对于树状数组的使用&#xff0c;我们开一个数组 c&#xff0c;c[x] 表示 [ x − lowbit ( x ) 1 , x ] [x-\text{lowbit}(x)1,x] [x−lowbit(x)1,x] 的区间和…

图像超分辨率超分辨率NeRF论文阅读

文章目录 前置知识图像超分辨率《High-resolution image reconstruction with latent diffusion models from human brain activity》【CVPR23】《Dynamic High-Pass Filtering and Multi-Spectral Attention for Image Super-Resolution》【ICCV21】《DiffBIR: Towards Blind …

视频的二维码怎么做?教你一个实用技巧操作

现在扫码看视频越来越常见&#xff0c;很多人都会用这种方式来展示内容&#xff0c;比如展示环境、产品说明、自拍录像等等&#xff0c;让别人能够更快的获取内容。举个例子&#xff0c;当我们从淘宝或者其他购物平台买东西时&#xff0c;现在咨询客服询问使用说明时&#xff0…

HTML超链接标签

目录 1&#xff1a;页面间的链接 1.1 文字跳转&#xff1a; 1.2 图片跳转 2&#xff1a;锚链接 1&#xff1a;页面间的链接 1.1 文字跳转&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><t…

webpack(五)热更新

webpack-dev-serve 开启本地服务器 现状&#xff1a;打包后将index.html文件通过open in liveServe打开&#xff0c;我们继续修改js、html文件内容&#xff0c;页面没有变化。 解决方法&#xff1a; 方法一&#xff1a; 在打包命令后面加上--watch,重新打包后我们的终端不会终…

类的继承简介

一、声明格式&#xff1a; class 子类名&#xff1a;继承方式(public private protected) 父类名{子类成员表} 二、继承过程&#xff1a; 吸取父类成员——>改造父类成员——>添加新成员 三、作用&#xff1a; 子类会继承父类中的方法(不包括构造和析构函数)与属性 …