PyQt5 open file dialog QFileDialog example code

The main research in this paper is the code example of the PyQt5 open file dialog box QFileDialog, as follows.
Single file open QFileDialog.getOpenFileName() 
Multiple files open QFileDialog.getOpenFileNames() 
Folder selection QFileDialog.getExistingDirectory() 
File save QFileDialog.getSaveFileName()
Example code:
  1. fromPyQt5importQtWidgets
  2. fromPyQt5.QtWidgetsimportQFileDialog
  3.  
  4. classMyWindow(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,
  14.                   "Select Folder" ,
  15.                   "./" ) #Start path                
  16.     print(directory1)
  17.  
  18.     fileName1, filetype =QFileDialog.getOpenFileName(self,
  19.                   "Select file" ,
  20.                   "./",
  21.                   "All Files (*);;Text Files (*.txt)" ) #Set the file extension filter, pay attention to the double semicolon interval 
  22.     print(fileName1,filetype)
  23.  
  24.     files, ok1 =QFileDialog.getOpenFileNames(self,
  25.                   "Multiple file selection" ,
  26.                   "./",
  27.                   "All Files (*);;Text Files (*.txt)")
  28.     print(files,ok1)
  29.  
  30.     fileName2, ok2 =QFileDialog.getSaveFileName(self,
  31.                   "File Save" ,
  32.                   "./",
  33.                   "All Files (*);;Text Files (*.txt)")
  34.  
  35. if __name__=="__main__": 
  36.   import sys 
  37.  
  38.   app=QtWidgets.QApplication(sys.argv) 
  39.   myshow = MyWindow ()
  40.   myshow.show()
  41.   sys.exit(app.exec_()) 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889343&siteId=291194637