PyQt5 Retrieve Folder Directory and Set It in lineEdit

7.3k views Asked by At

Need some advice on how to retrieve the directory path for a selected folder and set it on the LineEdit.

I have the following simple GUI enter image description here

If I clicked the toolButton (the button inside the red-circle), then a dialog window would pop up. Then we could navigate to select the desired folder. I wish that I could pass the path (in string) to the selected folder to the lineEdit box next to the button, once the user clicks Select Folder. However, I could not figure out how to do that. So far here is my code:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_TestQFileDialog(object):
    def _open_file_dialog(self): # a function to open the dialog window
        result = str(QtWidgets.QFileDialog.getExistingDirectory())
        print(result)
        return result

    def setupUi(self, TestQFileDialog):
        TestQFileDialog.setObjectName("TestQFileDialog")
        TestQFileDialog.resize(240, 320)

        self.toolButtonOpenDialog = QtWidgets.QToolButton(TestQFileDialog)
        self.toolButtonOpenDialog.setGeometry(QtCore.QRect(210, 10, 25, 19))
        self.toolButtonOpenDialog.setObjectName("toolButtonOpenDialog")
        directory = self.toolButtonOpenDialog.clicked.connect(self._open_file_dialog)

        self.lineEdit = QtWidgets.QLineEdit(TestQFileDialog)
        self.lineEdit.setEnabled(False)
        self.lineEdit.setGeometry(QtCore.QRect(10, 10, 191, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setText('{}'.format(directory))

        self.retranslateUi(TestQFileDialog)
        QtCore.QMetaObject.connectSlotsByName(TestQFileDialog)

    def retranslateUi(self, TestQFileDialog):
        _translate = QtCore.QCoreApplication.translate
        TestQFileDialog.setWindowTitle(_translate("TestQFileDialog", "Dialog"))
        self.toolButtonOpenDialog.setText(_translate("TestQFileDialog", "..."))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    TestQFileDialog = QtWidgets.QDialog()
    ui = Ui_TestQFileDialog()
    ui.setupUi(TestQFileDialog)
    TestQFileDialog.show()
    sys.exit(app.exec_())

Tried to include print in the _open_file_dialog function, and it printed the directory path. However, it was not returned and kept in the directory variable.

Any advice will be much appreciated.

Thanks and regards,

Arnold

2

There are 2 answers

0
arnold On BEST ANSWER

Found the answer, the .setText method should be included in the _open_file_dialog function.

Therefore, the final code would look like this:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_TestQFileDialog(object):
    def _open_file_dialog(self):
        directory = str(QtWidgets.QFileDialog.getExistingDirectory())
        self.lineEdit.setText('{}'.format(directory))

    def _set_text(self, text):
        return text

    def setupUi(self, TestQFileDialog):
        TestQFileDialog.setObjectName("TestQFileDialog")
        TestQFileDialog.resize(240, 320)

        self.toolButtonOpenDialog = QtWidgets.QToolButton(TestQFileDialog)
        self.toolButtonOpenDialog.setGeometry(QtCore.QRect(210, 10, 25, 19))
        self.toolButtonOpenDialog.setObjectName("toolButtonOpenDialog")
        self.toolButtonOpenDialog.clicked.connect(self._open_file_dialog)

        self.lineEdit = QtWidgets.QLineEdit(TestQFileDialog)
        self.lineEdit.setEnabled(False)
        self.lineEdit.setGeometry(QtCore.QRect(10, 10, 191, 20))
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(TestQFileDialog)
        QtCore.QMetaObject.connectSlotsByName(TestQFileDialog)

    def retranslateUi(self, TestQFileDialog):
        _translate = QtCore.QCoreApplication.translate
        TestQFileDialog.setWindowTitle(_translate("TestQFileDialog", "Dialog"))
        self.toolButtonOpenDialog.setText(_translate("TestQFileDialog", "..."))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    TestQFileDialog = QtWidgets.QDialog()
    ui = Ui_TestQFileDialog()
    ui.setupUi(TestQFileDialog)
    TestQFileDialog.show()
    sys.exit(app.exec_())
0
Amarnath Reddy Surapureddy On

With QWidget

from PyQt5.QtWidgets import QWidget, QFileDialog,QDialog,QToolButton,QLineEdit,QVBoxLayout ,QGridLayout

class Ui_TestQFileDialog(QWidget):

    def __init__(self,parent=None) -> None:
        super(Ui_TestQFileDialog,self).__init__(parent)
        self.setupUi()
        self.show()

    def _open_file_dialog(self,lineeditr):
        directory = str(QFileDialog.getExistingDirectory())
        lineeditr.setText('{}'.format(directory))
    def _open_file_dialog1(self):
        directory = str(QFileDialog.getExistingDirectory())
        self.lineEdit1.setText('{}'.format(directory))

    def _set_text(self, text):
        return text

    def setupUi(self):

        # ------------------------- first set  ------------------------------------------------------
        TestQFileDialog = QDialog()
        # TestQFileDialog.setObjectName("TestQFileDialog")
        # TestQFileDialog.resize(240, 320)

        self.toolButtonOpenDialog = QToolButton(TestQFileDialog)
        # self.toolButtonOpenDialog.setGeometry(QtCore.QRect(210, 10, 25, 19))
        # self.toolButtonOpenDialog.setObjectName("toolButtonOpenDialog")
        

        self.lineEdit = QLineEdit(TestQFileDialog)
        self.lineEdit.setEnabled(False)
        # self.lineEdit.setGeometry(QtCore.QRect(10, 10, 191, 20))
        # self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.textChanged.connect(self.prine_la)
        self.toolButtonOpenDialog.clicked.connect(lambda: (self._open_file_dialog(self.lineEdit)))

        # ----------------------------- second set  --------------------------------------------------------
        TestQFileDialog1 = QDialog()
        # TestQFileDialog1.setObjectName("TestQFileDialog")
        # TestQFileDialog1.resize(240, 320)

        self.toolButtonOpenDialog1 = QToolButton(TestQFileDialog1)
        # self.toolButtonOpenDialog1.setGeometry(QtCore.QRect(210, 10, 25, 19))
        # self.toolButtonOpenDialog1.setObjectName("toolButtonOpenDialog")
        

        self.lineEdit1 = QLineEdit(TestQFileDialog1)
        self.lineEdit1.setEnabled(False)
        self.lineEdit1.setPlaceholderText("Enter the gain value in between 0.0 to 23.05934920")
        # self.lineEdit1.setGeometry(QtCore.QRect(10, 10, 191, 20))
        # self.lineEdit1.setObjectName("lineEdit1")
        self.toolButtonOpenDialog1.clicked.connect(lambda: (self._open_file_dialog(self.lineEdit1)))


        self.retranslateUi(TestQFileDialog,self.toolButtonOpenDialog,"TestQFileDialog","data")
        self.retranslateUi(TestQFileDialog1,self.toolButtonOpenDialog1,"TestQFileDialog1","data1")
        QtCore.QMetaObject.connectSlotsByName(TestQFileDialog)
        QtCore.QMetaObject.connectSlotsByName(TestQFileDialog1)


        grid_layout = QGridLayout()
        grid_layout.addWidget(self.lineEdit,0,0)
        grid_layout.addWidget(self.toolButtonOpenDialog,0,1)

        grid_layout.addWidget(self.lineEdit1,1,0)
        grid_layout.addWidget(self.toolButtonOpenDialog1,1,1)

        ve_box= QVBoxLayout()
        # ve_box.addWidget(self.lineEdit)
        # ve_box.addWidget(self.toolButtonOpenDialog)
        # ve_box.addWidget(self.lineEdit1)
        # ve_box.addWidget(self.toolButtonOpenDialog1)
        ve_box.addLayout(grid_layout)
        self.setLayout(ve_box)

    def prine_la(self):
        print(self.lineEdit.text())
    def retranslateUi(self, TestQFileDialog,tool,dialogue:str,d: str):
        _translate = QtCore.QCoreApplication.translate
        TestQFileDialog.setWindowTitle(_translate(dialogue, d))
        tool.setText(_translate(dialogue, "..."))
        print("ANR .S")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_TestQFileDialog()
    
    sys.exit(app.exec_())