PyQt5打开文件对话框QFileDialog实例代码

yipeiwu_com6年前Python基础

本文研究的主要是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_()) 

总结

以上就是本文关于PyQt5打开文件对话框QFileDialog实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

pyramid配置session的方法教程

1. 使用默认的session, 在ini文件中:复制代码 代码如下:from pyramid.session import UnencryptedCookieSessionFactor...

pandas DataFrame 行列索引及值的获取的方法

pandas DataFrame是二维的,所以,它既有列索引,又有行索引 上一篇里只介绍了列索引: import pandas as pd df = pd.DataFrame({'...

Python 实现文件打包、上传与校验的方法

不多说,我们直接上源码: # -*- coding:UTF-8 -*- ''' 实现文件打包、上传与校验 Created on 2018年1月12日 @author: liuyazh...

python利用pandas将excel文件转换为txt文件的方法

python将数据换为txt的方法有很多,可以用xlrd库实现。本人比较懒,不想按太多用的少的插件,利用已有库pandas将excel文件转换为txt文件。 直接上代码: ''' f...

在Python3 numpy中mean和average的区别详解

mean和average都是计算均值的函数,在不指定权重的时候average和mean是一样的。指定权重后,average可以计算一维的加权平均值。 具体如下: import num...