Search code examples
pythonpython-2.7pyqtmayapymel

Get text from FileDialog Python\Qt


I'm pretty new with Qt in Python, and I need to know how could I get from a file dialog one directory path and show it in a text box inside my ui.

class ControllerLibraryUI(QtWidgets.QDialog):
    def __init__(self):
        super(ControllerLibraryUI, self).__init__()
        self.setWindowTitle('Controller Window')
        self.buildUI()

    def buildUI(self):
        layout = QtWidgets.QVBoxLayout(self)
        textWidget = QtWidgets.QWidget()
        textLayout = QtWidgets.QHBoxLayout(textWidget)
        layout.addWidget(textWidget)
        self.saveNameField = QtWidgets.QTextEdit()
        textLayout.addWidget(self.saveNameField)
        btnWidget = QtWidgets.QWidget()
        btnLayout = QtWidgets.QHBoxLayout(btnWidget)
        layout.addWidget(btnWidget)
        importBtn = QtWidgets.QPushButton('Import')
        importBtn.clicked.connect(self.load)
        btnLayout.addWidget(importBtn)
        closeBtn = QtWidgets.QPushButton('Close')
        closeBtn.clicked.connect(self.close)
        btnLayout.addWidget(closeBtn)

    def load(self ):
            filename = QtWidgets.QFileDialog.getOpenFileName()[0]
            print filename
    def showUI():
        try:
            ui.close()
        except:
            ui = ControllerLibraryUI()
            ui.show()
            return ui

ui = showUI()

This is the basic concept.


Solution

  • for the file dialog :

    directory = QtGui.QFileDialog.getExistingDirectory(self, 'Select directory')
    

    and then you assign the directory variable to the setText method from your text box