PyQt5開啟檔案對話方塊QFileDialog執行個體代碼,pyqt5qfiledialog
本文研究的主要是PyQt5開啟檔案對話方塊QFileDialog的程式碼範例,具體如下。
單個檔案開啟 QFileDialog.getOpenFileName()
多個檔案開啟 QFileDialog.getOpenFileNames()
檔案夾選取 QFileDialog.getExistingDirectory()
檔案儲存 QFileDialog.getSaveFileName()
執行個體代碼:
from PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import QFileDialogclass 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執行個體代碼的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!