本文研究的主要是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_()) |
總結
以上就是本文關于PyQt5打開文件對話框QFileDialog實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/huangzhang_123/article/details/78144692