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

yipeiwu_com5年前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实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python追加元素到列表的方法

本文实例讲述了python追加元素到列表的方法。分享给大家供大家参考。具体实现方法如下: scores = ["1","2","3"] # add a score score = i...

Python数据分析之双色球基于线性回归算法预测下期中奖结果示例

Python数据分析之双色球基于线性回归算法预测下期中奖结果示例

本文实例讲述了Python数据分析之双色球基于线性回归算法预测下期中奖结果。分享给大家供大家参考,具体如下: 前面讲述了关于双色球的各种算法,这里将进行下期双色球号码的预测,想想有些小激...

python构造icmp echo请求和实现网络探测器功能代码分享

python发送icmp echo requesy请求复制代码 代码如下:import socketimport struct def checksum(source_string):&...

Python基于pygame实现单机版五子棋对战

Python基于pygame实现单机版五子棋对战

python实现的五子棋,能够自动判断输赢,没有是实现电脑对战功能 源码下载:pygame五子棋 # 1、引入pygame 和 pygame.locals import pygame...

python中lambda函数 list comprehension 和 zip函数使用指南

lambda 函数 Python 支持一种有趣的语法,它允许你快速定义单行的最小函数。这些叫做 lambda 的函数,是从 Lisp 借用来的,可以用在任何需要函数的地方。 def f...