QFileDialog for selecting Photos library

Environment

MacOS
Python 3.6
PyQt5

Code for the UI

Following code prepares UI components:

  1. a button to open a system dialog for selecting a file
  2. a label to display the absolute path of the file
def setupUi(self, MainWindow):
        ......
        self.lblPath = QtWidgets.QLabel(self.centralwidget)
        self.lblPath.setGeometry(QtCore.QRect(350, 280, 200, 16))
        self.lblPath.setObjectName("lblPath")

        self.btnSelectLibrary = QtWidgets.QPushButton(self.centralwidget)
        self.btnSelectLibrary.setGeometry(QtCore.QRect(320, 320, 131, 32))
        self.btnSelectLibrary.setObjectName("btnSelectLibrary")
        ......
Code for the button event
def retranslateUi(self, MainWindow):
        ......
        self.btnSelectLibrary.clicked.connect(self.selectLibraryPath)
        ......
Code for the dialog
    def selectLibraryPath(self):
        print("yes")
        dir, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Select the library", "/Users/", "*.photoslibrary")
        print(dir)
        if dir:
            self.lblPath.setText(dir)

Method getOpenFilename returns an array as following

(path, filter)

e.g,

('/Volumes/Mac Photos/MacPhotos.libraries/Photos.photoslibrary', '*.photoslibrary')

The first item of the array is what we expected.

你可能感兴趣的:(QFileDialog for selecting Photos library)