1. 程式人生 > >PyQt5學習筆記----標準檔案開啟儲存框QFileDialog

PyQt5學習筆記----標準檔案開啟儲存框QFileDialog

  • 單個檔案開啟 QFileDialog.getOpenFileName()
    多個檔案開啟 QFileDialog.getOpenFileNames()

  • 資料夾選取     QFileDialog.getExistingDirectory()

  • 檔案儲存         QFileDialog.getSaveFileName()

  •  1 from PyQt5 import QtWidgets
     2 from PyQt5.QtWidgets import QFileDialog
     3  
     4 class
    MyWindow(QtWidgets.QWidget): 5 def __init__(self): 6 super(MyWindow,self).__init__() 7 self.myButton = QtWidgets.QPushButton(self) 8 self.myButton.setObjectName("myButton") 9 self.myButton.setText("Test") 10 self.myButton.clicked.connect(self.msg) 11 12
    def msg(self): 13 directory1 = QFileDialog.getExistingDirectory(self,"選取資料夾", "C:/") #起始路徑 14 print(directory1) 15 16 fileName1, filetype = QFileDialog.getOpenFileName(self, "選取檔案","C:/","All Files (*);;Text Files (*.txt)") #設定副檔名過濾,注意用雙分號間隔
    17 print(fileName1,filetype) 18 19 files, ok1 = QFileDialog.getOpenFileNames(self, "多檔案選擇", "C:/", "All Files (*);;Text Files (*.txt)") 20 print(files,ok1) 21 22 fileName2, ok2 = QFileDialog.getSaveFileName(self, "檔案儲存", "C:/","All Files (*);;Text Files (*.txt)") 23 24 if __name__=="__main__": 25 import sys 26 27 app=QtWidgets.QApplication(sys.argv) 28 myshow=MyWindow() 29 myshow.show() 30 sys.exit(app.exec_())

     轉載--------------------- 

    作者:翻滾吧挨踢男
    來源:CSDN
    原文:https://blog.csdn.net/a359680405/article/details/45166271
    版權宣告:本文為博主原創文章,轉載請附上博文連結!