当前位置: 首页 > news >正文

Pyside6 QFileDialog

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 = ... # 0x1DontResolveSymlinks      : QFileDialog.Option = ... # 0x2DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4DontUseNativeDialog      : QFileDialog.Option = ... # 0x8ReadOnly                 : QFileDialog.Option = ... # 0x10HideNameFilterDetails    : QFileDialog.Option = ... # 0x20DontUseCustomDirectoryIcons: 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 = ... # 0x1DontResolveSymlinks      : QFileDialog.Option = ... # 0x2DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4DontUseNativeDialog      : QFileDialog.Option = ... # 0x8ReadOnly                 : QFileDialog.Option = ... # 0x10HideNameFilterDetails    : QFileDialog.Option = ... # 0x20DontUseCustomDirectoryIcons: 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 = ... # 0x1DontResolveSymlinks      : QFileDialog.Option = ... # 0x2DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4DontUseNativeDialog      : QFileDialog.Option = ... # 0x8ReadOnly                 : QFileDialog.Option = ... # 0x10HideNameFilterDetails    : QFileDialog.Option = ... # 0x20DontUseCustomDirectoryIcons: 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 = ... # 0x1DontResolveSymlinks      : QFileDialog.Option = ... # 0x2DontConfirmOverwrite     : QFileDialog.Option = ... # 0x4DontUseNativeDialog      : QFileDialog.Option = ... # 0x8ReadOnly                 : QFileDialog.Option = ... # 0x10HideNameFilterDetails    : QFileDialog.Option = ... # 0x20DontUseCustomDirectoryIcons: 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())

相关文章:

Pyside6 QFileDialog

Pyside6 QFileDialog Pyside6 QFileDialog常用函数getOpenFileNamegetOpenFileNamesgetExistingDirectorygetSaveFileName 程序界面程序主程序 Pyside6 QFileDialog提供了一个允许用户选择文件或目录的对话框。关于QFileDialog的使用可以参考下面的文档 https://doc.qt.io/qtfo…...

Leetcode1793. Maximum Score of a Good Subarray

给定一个数组和一个下标 k k k 子数组 ( i , j ) (i,j) (i,j)分数定义为 min ⁡ ( n u m s [ i ] , n u m s [ i 1 ] , ⋯ , n u m s [ j ] ) ∗ ( j − i 1 ) \min\left(nums[i], nums[i 1],\cdots, nums[j]\right)*\left(j-i1\right) min(nums[i],nums[i1],⋯,nums[j])∗(…...

只需五步,在Linux安装chrome及chromedriver(CentOS)

一、安装Chrome 1&#xff09;先执行命令下载chrome&#xff1a; wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm2&#xff09;安装chrome yum localinstall google-chrome-stable_current_x86_64.rpm看到下图中的Complete出现则代表安装…...

第01章-Java语言概述

目录 1 常见DOS命令 常用指令 相对路径与绝对路径 2 转义字符 3 安装JDK与配置环境变量 JDK与JRE JDK的版本 JDK的下载 JDK的安装 配置path环境变量 4 Java程序的编写与执行 5 Java注释 6 Java API文档 7 Java核心机制&#xff1a;JVM 1 常见DOS命令 DOS&#xff08;…...

Spring | Spring Cache 缓存框架

Spring Cache 缓存框架&#xff1a; Spring Cache功能介绍Spring Cache的Maven依赖Spring Cache的常用注解EnableCaching注解CachePut注解Cacheable注解CacheEvict注解 Spring Cache功能介绍 Spring Cache是Spring的一个框架&#xff0c;实现了基于注解的缓存功能。只需简单加一…...

雷达开发的基本概念fft,cfar,以及Clutter, CFAR,AoA

CFAR Constant False-Alarm Rate的缩写。在雷达信号检测中&#xff0c;当外界干扰强度变化时&#xff0c;雷达能自动调整其灵敏度&#xff0c;使雷达的虚警概率保持不变。具有这种特性的接收机称为恒虚警接收机。雷达信号的检测总是在干扰背景下进行的&#xff0c;这些干扰包括…...

什么是大数据测试?有哪些类型?应该怎么测?

随着目前世界上各个国家使用大数据应用程序或应用大数据技术场景的数量呈指数增长&#xff0c;相应的&#xff0c;对于测试大数据应用时所需的知识与大数据测试工程师的需求也在同步增加。 针对大数据测试的相关技术已慢慢成为当下软件测试人员需要了解和掌握的一门通用技术。…...

03-垃圾收集策略与算法

垃圾收集策略与算法 程序计数器、虚拟机栈、本地方法栈随线程而生&#xff0c;也随线程而灭&#xff1b;栈帧随着方法的开始而入栈&#xff0c;随着方法的结束而出栈。这几个区域的内存分配和回收都具有确定性&#xff0c;在这几个区域内不需要过多考虑回收的问题&#xff0c;因…...

1.AUTOSAR的架构及方法论

在15、16年之前,AUTOSAR这个东西其实是被国内很多大的OEM或者供应商所排斥的。为什么?最主要的原因还是以前采用手写底层代码+应用层模型生成代码的方式进行开发。每个供应商或者OEM都有自己的软件规范或者技术壁垒,现在提个AUTOSAR想搞统一,用一个规范来收割汽车软件供应链…...

Kotlin中的List集合

在Kotlin中&#xff0c;List集合用于存储一组有序的元素。List集合分为可变集合&#xff08;MutableList&#xff09;和不可变集合&#xff08;List&#xff09;。本篇博客将分别介绍可变集合和不可变集合&#xff0c;并提供相关的API示例代码。 不可变集合&#xff08;List&a…...

微信小程序WeUI项目weui-miniprogram如何运行起来?

微信小程序WeUI项目weui-miniprogram如何运行起来&#xff1f; 解决方法: 1、下载 https://github.com/wechat-miniprogram/weui-miniprogram 2、在项目根目录weui-miniprogram-master执行以下命令安装依赖&#xff1a; npm install 3、继续执行编译命令&#xff1a; npm r…...

MapReduce编程:检索特定群体搜索记录和定义分片操作

文章目录 MapReduce 编程&#xff1a;检索特定群体搜索记录和定义分片操作一、实验目标二、实验要求及注意事项三、实验内容及步骤 附&#xff1a;系列文章 MapReduce 编程&#xff1a;检索特定群体搜索记录和定义分片操作 一、实验目标 熟悉MapReduce编程涉及的主要类和接口…...

pytorch 入门 (四)案例二:人脸表情识别-VGG16实现

实战教案二&#xff1a;人脸表情识别-VGG16实现 本文为&#x1f517;小白入门Pytorch内部限免文章 参考本文所写记录性文章&#xff0c;请在文章开头注明以下内容&#xff0c;复制粘贴即可 &#x1f368; 本文为&#x1f517;小白入门Pytorch中的学习记录博客&#x1f366; 参…...

数据结构--线性表回顾

目录 线性表 1.定义 2.线性表的基本操作 3.顺序表的定义 3.1顺序表的实现--静态分配 3.2顺序表的实现--动态分配 4顺序表的插入、删除 4.1插入操作的时间复杂度 4.2顺序表的删除操作-时间复杂度 5 顺序表的查找 5.1按位查找 5.2 动态分配的方式 5.3按位查找的时间…...

ChatGPT(1):ChatGPT初识

1 ChatGPT原理 ChatGPT 是基于 GPT-3.5 架构的一个大型语言模型&#xff0c;它的工作原理涵盖了深度学习和自然语言处理技术。以下是 ChatGPT 的工作原理的一些关键要点&#xff1a; 神经网络架构&#xff1a;ChatGPT 的核心是一个深度神经网络&#xff0c;采用了变种的 Tran…...

PostgreSQL 插件 CREATE EXTENSION 原理

PostgreSQL 提供了丰富的数据库内核编程接口&#xff0c;允许开发者在不修改任何 Postgres 核心代码的情况下以插件的形式将自己的代码融入内核&#xff0c;扩展数据库功能。本文探究了 PostgreSQL 插件的一般源码组成&#xff0c;梳理插件的源码内容和实现方式&#xff1b;并介…...

Android常见分区

一、Google官方标准分区 1. Boot分区 包含Linux内核和一个最小的root文件系统(装载到ramdisk中)&#xff0c;用于挂载系统和其他的分区并开始Runtime。正如名字所代表的意思&#xff08;注&#xff1a;boot的意思是启动&#xff09;&#xff0c;这个分区使Android设备可以启动…...

华为鸿蒙4谷歌GMS安装教学

目录 问题描述 参考视频 教学视频1 配套文档 教学视频2 资源包(配套视频1) 设备未经 play 保护机制认证 问题描述 很多国外的最新应用需要再Google商店才能下载比如ChatGPT 华为手机不支持 Google Play 服务的原因主要是由于谷歌服务框架&#xff08;GMS&#xff09;未…...

原型设计工具:Balsamiq Wireframes 4.7.4 Crack

原型设计工具:Balsamiq Wireframes是一种快速的低保真UI 线框图工具&#xff0c;可重现在记事本或白板上绘制草图但使用计算机的体验。 它确实迫使您专注于结构和内容&#xff0c;避免在此过程后期对颜色和细节进行冗长的讨论。 线框速度很快&#xff1a;您将产生更多想法&am…...

Nginx Proxy代理

代理原理 反向代理产生的背景&#xff1a; 在计算机世界里&#xff0c;由于单个服务器的处理客户端&#xff08;用户&#xff09;请求能力有一个极限&#xff0c;当用户的接入请求蜂拥而入时&#xff0c;会造成服务器忙不过来的局面&#xff0c;可以使用多个服务器来共同分担成…...

利用ngx_stream_return_module构建简易 TCP/UDP 响应网关

一、模块概述 ngx_stream_return_module 提供了一个极简的指令&#xff1a; return <value>;在收到客户端连接后&#xff0c;立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量&#xff08;如 $time_iso8601、$remote_addr 等&#xff09;&a…...

脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)

一、数据处理与分析实战 &#xff08;一&#xff09;实时滤波与参数调整 基础滤波操作 60Hz 工频滤波&#xff1a;勾选界面右侧 “60Hz” 复选框&#xff0c;可有效抑制电网干扰&#xff08;适用于北美地区&#xff0c;欧洲用户可调整为 50Hz&#xff09;。 平滑处理&…...

QMC5883L的驱动

简介 本篇文章的代码已经上传到了github上面&#xff0c;开源代码 作为一个电子罗盘模块&#xff0c;我们可以通过I2C从中获取偏航角yaw&#xff0c;相对于六轴陀螺仪的yaw&#xff0c;qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...

Cesium1.95中高性能加载1500个点

一、基本方式&#xff1a; 图标使用.png比.svg性能要好 <template><div id"cesiumContainer"></div><div class"toolbar"><button id"resetButton">重新生成点</button><span id"countDisplay&qu…...

基于uniapp+WebSocket实现聊天对话、消息监听、消息推送、聊天室等功能,多端兼容

基于 ​UniApp + WebSocket​实现多端兼容的实时通讯系统,涵盖WebSocket连接建立、消息收发机制、多端兼容性配置、消息实时监听等功能,适配​微信小程序、H5、Android、iOS等终端 目录 技术选型分析WebSocket协议优势UniApp跨平台特性WebSocket 基础实现连接管理消息收发连接…...

FastAPI 教程:从入门到实践

FastAPI 是一个现代、快速&#xff08;高性能&#xff09;的 Web 框架&#xff0c;用于构建 API&#xff0c;支持 Python 3.6。它基于标准 Python 类型提示&#xff0c;易于学习且功能强大。以下是一个完整的 FastAPI 入门教程&#xff0c;涵盖从环境搭建到创建并运行一个简单的…...

Cloudflare 从 Nginx 到 Pingora:性能、效率与安全的全面升级

在互联网的快速发展中&#xff0c;高性能、高效率和高安全性的网络服务成为了各大互联网基础设施提供商的核心追求。Cloudflare 作为全球领先的互联网安全和基础设施公司&#xff0c;近期做出了一个重大技术决策&#xff1a;弃用长期使用的 Nginx&#xff0c;转而采用其内部开发…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

【决胜公务员考试】求职OMG——见面课测验1

2025最新版&#xff01;&#xff01;&#xff01;6.8截至答题&#xff0c;大家注意呀&#xff01; 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:&#xff08; B &#xff09; A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...

[Java恶补day16] 238.除自身以外数组的乘积

给你一个整数数组 nums&#xff0c;返回 数组 answer &#xff0c;其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法&#xff0c;且在 O(n) 时间复杂度…...