PyQt5打开文件对话框QFileDialog实例代码
作者:HuangZhang_123 时间:2022-08-10 03:38:47
本文研究的主要是PyQt5打开文件对话框QFileDialog的代码示例,具体如下。
单个文件打开 QFileDialog.getOpenFileName()
多个文件打开 QFileDialog.getOpenFileNames()
文件夹选取 QFileDialog.getExistingDirectory()
文件保存 QFileDialog.getSaveFileName()
实例代码:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QFileDialog
class MyWindow(QtWidgets.QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.myButton = QtWidgets.QPushButton(self)
self.myButton.setObjectName("myButton")
self.myButton.setText("Test")
self.myButton.clicked.connect(self.msg)
def msg(self):
directory1 = QFileDialog.getExistingDirectory(self,
"选取文件夹",
"./") #起始路径
print(directory1)
fileName1, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
"./",
"All Files (*);;Text Files (*.txt)") #设置文件扩展名过滤,注意用双分号间隔
print(fileName1,filetype)
files, ok1 = QFileDialog.getOpenFileNames(self,
"多文件选择",
"./",
"All Files (*);;Text Files (*.txt)")
print(files,ok1)
fileName2, ok2 = QFileDialog.getSaveFileName(self,
"文件保存",
"./",
"All Files (*);;Text Files (*.txt)")
if __name__=="__main__":
import sys
app=QtWidgets.QApplication(sys.argv)
myshow=MyWindow()
myshow.show()
sys.exit(app.exec_())
来源:http://blog.csdn.net/huangzhang_123/article/details/78144692
标签:python,pyqt,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Linux下python与C++使用dlib实现人脸检测
2023-06-13 16:13:20
![](https://img.aspxhome.com/file/2023/9/59829_0s.jpg)
Python爬虫爬取属于自己的地铁线路图
2021-09-10 11:46:23
![](https://img.aspxhome.com/file/2023/3/103793_0s.jpg)
通过实例了解python__slots__使用方法
2023-03-16 13:24:50
关于Python中*args和**kwargs的深入理解
2021-04-07 17:45:06
![](https://img.aspxhome.com/file/2023/1/77861_0s.png)
Python tkinter三种布局实例详解
2022-11-28 11:23:44
![](https://img.aspxhome.com/file/2023/7/86417_0s.jpg)
Python装饰器中@property使用详解
2022-04-22 04:46:44
PyQt5 QSerialPort子线程操作的实现
2023-08-21 13:07:28
python3实现带多张图片、附件的邮件发送
2023-05-11 06:51:10
ASP 获取腾讯IP地址的代码
2011-02-26 11:19:00
matplotlib 对坐标的控制,加图例注释的操作
2021-06-12 16:28:46
![](https://img.aspxhome.com/file/2023/4/90404_0s.jpg)
document.createElement()用法及注意事项
2008-04-21 15:16:00
Python中django学习心得
2023-12-24 17:10:15
![](https://img.aspxhome.com/file/2023/2/89322_0s.png)
python中@property的作用和getter setter的解释
2022-08-14 21:12:39
django为Form生成的label标签添加class方式
2022-08-13 12:01:44
python输入一个水仙花数(三位数) 输出百位十位个位实例
2022-12-09 12:39:58
![](https://img.aspxhome.com/file/2023/6/110436_0s.jpg)
python学生信息管理系统实现代码
2021-07-01 03:41:18
![](https://img.aspxhome.com/file/2023/7/81817_0s.jpg)
PHP编程入门的基本语法知识点总结
2023-06-14 11:16:36
Python实现判断给定列表是否有重复元素的方法
2021-05-25 13:34:59
![](https://img.aspxhome.com/file/2023/1/93551_0s.png)
Python实现进程同步和通信的方法
2023-08-30 04:21:45
Python smallseg分词用法实例分析
2023-02-09 01:04:26