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

本文研究的主要是PyQt5打开文件对话框QFileDialog的代码示例,具体如下。

单个文件打开 QFileDialog.getOpenFileName()
多个文件打开 QFileDialog.getOpenFileNames()
文件夹选取 QFileDialog.getExistingDirectory()
文件保存 QFileDialog.getSaveFileName()

实例代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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

搜索